我必须从互联网上拿出许多图片,并把它们显示在正确的所有者名字旁边。我有一个纹理数组,以及相应的所有者ID。
首先,我必须使用所有者的ID调用一个API函数来获取其图片的URL,然后在该URL中获取图片。
下面是c#统一伪代码:
void function() {
for (int i=0; i<10; i++) {
start_fetch_url(picID[i],CallBack); // fetch URL of picID, via call back
}
}
// when url is received this is called, then fetch pic at that URL
IEnumerator CallBack(string url)
{
WWW fetchPic= new WWW(url);
while (fetchPic.isDone) {
yeild return new WaitForSeconds(0.1f);
}
texture[????]=fetchPic.texture; '// <--- here is problem, which texture
}我不能更改start_fetch_url API,因为我没有编写它,也没有访问它的权限。
start_fetch_url还可以访问互联网,从远程数据库获取基于ID的url。
问题: 1. start_fetch_url按随机顺序将URL返回给回调函数,因此我无法判断url用于哪种纹理。
谢谢
发布于 2015-07-04 10:28:49
如果您不能更改start_fetch_url,并且无法根据传递给您的协同服务的url推断用户id,我认为唯一的方法是按顺序下载图像。
您可以通过将用户索引保存在一个类变量中来实现这一点,并且只有在您的coroutine被调用时才转到下一张图片。
var index = 0;
function load_next()
{
//we've reached the end
if( index == 10 )
{
return;
}
start_fetch_url(picID[i],CallBack);
}
// when url is received this is called, then fetch pic at that URL
IEnumerator CallBack(string url)
{
WWW fetchPic= new WWW(url);
while (fetchPic.isDone)
{
yield return new WaitForSeconds(0.1f);
}
texture[index]=fetchPic.texture;
//continue with the fetching
index++;
load_next();
}注意:您不需要通过轮询来检查下载是否已经结束,只需调用yield return fetchPic;即可。如果您需要显示一个进度条,那么您需要像现在一样使用时间。
https://stackoverflow.com/questions/31213138
复制相似问题