从Unity中的WWW调用中获取变量

时间:2014-05-21 07:45:32

标签: c# unity3d

我有一个返回XML数据的PHP函数。我正在使用C#Unity将数据检索到字符串变量。我做到了。

在课程Requestwww中我有

public static text;
public IEnumerator WaitForRequest(WWW www)
    {
       yield return www;
        // check for errors
        if (www.error == null)
        {   //Here I store result in a global variable
            text=www.text;
            Debug.Log("WWW Ok!: " + text);
        }
        else
        {
            Debug.Log("WWW Error: " + www.error);
        }


    }

    public  IEnumerator getxml(string url )
   {

        WWW www = new WWW(url);
        if (www == null)
        {
            Debug.Log("www is null");
        }
        else {
            Debug.Log("www is not null");
        }

        yield return StartCoroutine(WaitForRequest(www));
        Debug.Log("xmltext here" + text);

    }

在另一个课程中,我调用getxml()来启动wwwrequest

Request wwwcall = gameObject.AddComponent<Requestwww>(); 
StartCoroutine(wwwcall.getxml("http://com.example/something.php")
//Here I tried to access the xmltext variable in Requestwww class
Debug.Log("retrived var"+Requestwww.text)
//But text seems tobe null

但看起来,从另一个类重试时,变量Requestwww.text始终为null。我不明白。我打电话给Coroutine,等待结果,然后更新全局变量,但似乎我从另一个类调用它的那一刻,它还没有更新。我该怎么办?我花了一天时间来寻找答案......

1 个答案:

答案 0 :(得分:0)

答案

这是因为您不等待StartCoroutine(wwwcall.getxml("http://com.example/something.php"));完成。如果在没有yield return的情况下调用StartCoroutine,它将立即返回。所以,解决方案是等待协程完成(或者去除协同程序并等待WWW完成)。您可以这样做:

  1. 在Update方法中。像这样:

    public class RequestWWW
    {
        public bool IsDone
        {
            get;
            private set;
        }
    
        public string Text
        {
            get;
            private set;
        }
    
        public string Error
        {
            get;
            private set;
        }
    
        public IEnumerator GetXMLCoroutine(string url)
        {
            WWW www = new WWW(url);
            yield return www;
            IsDone = true;
            Error = www.error;
            Text = www.text;
        }
    }
    
    public class YourClass: MonoBehaviour
    {
        private RequestWWW wwwcall;
    
        private void SomeMethod()
        {
            wwwcall = new RequestWWW();
            StartCoroutine(wwwcall.GetXMLCoroutine("http://ya.ru"));
        }
    
        public void Update()
        {
            if (wwwcall != null && wwwcall.IsDone)
            {
                Debug.Log(wwwcall.Error);
                Debug.Log(wwwcall.Text);
                wwwcall = null;
            }
        }
    }
    
  2. 使用回调。像这样:

    public static class RequestWWW
    {
        public delegate void RequestFinishedHandler(string error, string text);
    
        public static IEnumerator GetXMLCoroutine(string url, RequestFinishedHandler onFinish)
        {
            WWW www = new WWW(url);
            yield return www;
            if (onFinish != null)
                onFinish(www.error, www.text);
        }
    }
    
    public class YourClass: MonoBehaviour
    {
        private void SomeMethod()
        {
            StartCoroutine(RequestWWW.GetXMLCoroutine("http://ya.ru/", RequestFinished));
        }
    
        private void RequestFinished(string error, string text)
        {
            Debug.Log(error);
            Debug.Log(text);
        }
    }