Unity UnityWebRequest Coroutine

5.7k views Asked by At

I'm trying to figure out how to properly use UnityWebRequest within a Unity Coroutine and I tried this way but I don't get results:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.Networking;

public class rest : MonoBehaviour
{
    public Coroutine<T> StartCoroutine<T>(IEnumerator coroutine)
    {
        Coroutine<T> coroutineObj = new Coroutine<T>();
        coroutineObj.coroutine = base.StartCoroutine(coroutineObj.InternalRoutine(coroutine));
        return coroutineObj;
    }

    public class Coroutine<T>
    {
        public T Value
        {
            get
            {
                if (e != null)
                {
                    throw e;
                }
                return returnVal;
            }
        }
        private T returnVal;
        private Exception e;
        public Coroutine coroutine;

        public IEnumerator InternalRoutine(IEnumerator coroutine)
        {
            while (true)
            {
                try
                {
                    if (!coroutine.MoveNext())
                    {
                        yield break;
                    }
                }
                catch (Exception e)
                {
                    this.e = e;
                    yield break;
                }
                object yielded = coroutine.Current;
                if (yielded != null && yielded.GetType() == typeof(T))
                {
                    returnVal = (T)yielded;
                    yield break;
                }
                else
                {
                    yield return coroutine.Current;
                }
            }
        }
    }

    IEnumerator Start()
    {
        var routine = StartCoroutine<int>(TestNewRoutineGivesException());
        yield return routine.coroutine;
        try
        {
            Debug.Log(routine.Value);
        }
        catch (Exception e)
        {
            Debug.Log(e.Message);
            Debug.Break();
        }
    }

    IEnumerator TestNewRoutineGivesException()
    {
        yield return null;
        yield return new WaitForSeconds(5f);
        UnityWebRequest www = UnityWebRequest.Get("http://localhost:3000/api/players");
        www.downloadHandler = new DownloadHandlerBuffer();
        yield return www.Send();
        while (!www.downloadHandler.isDone) yield return new WaitForEndOfFrame();

        if (www.isError)
        {
            Debug.Log(www.error);
        }
        else
        {
            string results = www.downloadHandler.text;
            yield return results;
        }
    }
}

but if for example my TestNewRoutineGivesException looks like this, then it works

IEnumerator TestNewRoutineGivesException()
{
    yield return null;
    yield return new WaitForSeconds(5f);
    yield return new 100;
}

it will return "100"

1

There are 1 answers

1
Nika Kasradze On

UPDATE: (as of late 2018) use UnityWebRequest instead of WWW class


try this:

public class rest : MonoBehaviour
{
    void Start()
    {
        Coroutine routine = StartCoroutine(TestNewRoutineGivesException());
    }

    IEnumerator TestNewRoutineGivesException()
    {
        UnityWebRequest www = UnityWebRequest.Get("http://localhost:3000/api/players");
        www.downloadHandler = new DownloadHandlerBuffer();
        yield return www.Send();
        while (!www.downloadHandler.isDone)
            yield return new WaitForEndOfFrame();

        if (www.isError)
        {
            Debug.Log(www.error);
        }
        else
        {
            string results = www.downloadHandler.text;
            DoSomethingWithTheCoroutineResult(result);
        }

        yield break;
    }

    void DoSomethingWithTheCoroutineResult(string result)
    {
        Debug.Log("Successfully got the result: " + result);
    }
}