【发布时间】:2017-05-11 23:08:50
【问题描述】:
我目前正在研究统一,我正在尝试使用 WWW 类从 Web 下载数据。我的代码如下。
IEnumerator Awake()
{
WWW imgLinks = new WWW(imgConnection); //Here I am trying to download image links.
yield return imgLinks; //here image URL are supposed to be downloaded.
string imgLinkSring = imgLinks.text;
imgLinksArray = imgLinkSring.Split(';'); //and they are splitted by ";"
//---
string _imgURL = "No data.";
string _tag = "";
for (int i = 0; i < imgLinksArray.Length; i++)
{
if (imgLinksArray[i].Contains("tag:"))
{
_tag = GetdataValue(imgLinksArray[i], "tag:");
_imgURL = GetdataValue(imgLinksArray[i], "name:");
if (_imgURL.Contains("|")) _imgURL = _imgURL.Remove(_imgURL.IndexOf("|"));
if (_tag.Contains("|")) _imgURL = _imgURL.Remove(_imgURL.IndexOf("|"));
WWW imgTextures = new WWW(domainName + "showImage.php?name=" + _tag); //and here imageTextures are supposed to be downloaded by the URL's I downloade in the beginning.
yield return imgTextures;
tex = imgTextures.texture;
textureDatas2.Add(_tag, tex);
}
}
}
问题是,如果我的代码中没有 Update(),这可以正常工作。当我有 Update() 时,代码从 yield return imgLinks; 跳转到 Update 函数,并在 Update 中运行代码,然后完成 start 函数。
我想要的是,完成运行 Start 功能,然后开始运行 Update 功能。
我该怎么办?
【问题讨论】:
-
更新每帧运行一次。如果您的 start 方法产生并需要时间,那么 Update 绝对会在等待发生时被调用。
-
@Draco18s 那么我怎样才能实现我想要的呢?
-
您需要等待协程完成,然后才能对其数据进行任何操作。如果你正在对其执行每帧操作,则需要先检查数据是否已完全下载,如果没有,则不执行任何操作。
-
@Draco18s 是的,我知道但我该怎么做?
-
Check if the data exists first...嗯...if(textureDatas2.count > 0)?
标签: c# unity3d ienumerator