【问题标题】:Get variable from WWW call in Unity从 Unity 中的 WWW 调用中获取变量
【发布时间】:2014-05-21 21:13:09
【问题描述】:

我有一个返回 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 始终为空。我不明白。我调用了协程,等待结果,然后更新全局变量,但似乎我从另一个类调用它的那一刻,它还没有更新。我应该怎么办?我花了一天的时间来寻找答案......

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    答案

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

    1. 在更新方法中。像这样:

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

    【讨论】:

    • 太棒了,伙计!我使用 isDone 标志等待,但你这样做的方式是最优雅的!
    • 很高兴你喜欢它 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 2018-07-30
    • 1970-01-01
    • 1970-01-01
    • 2012-06-15
    • 2014-04-14
    • 1970-01-01
    相关资源
    最近更新 更多