Unity UnityWebRequest Coroutine

时间:2016-12-25 12:23:25

标签: unity3d unity5 coroutine

我试图弄清楚如何在Unity Coroutine中正确使用UnityWebRequest,我尝试了这种方式,但我没有得到结果:

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;
        }
    }
}

但是,例如我的TestNewRoutineGivesException看起来像这样,那么它可以工作

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

它将返回&#34; 100&#34;

1 个答案:

答案 0 :(得分:1)

更新 :(截至2018年末)使用UnityWebRequest代替WWW

试试这个:

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);
    }
}