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