我有一個奇怪的問題,我需要幫助。動作回調與循環衝突
當我打印循環變量i,這是印刷I = 3。但是我必須是0,1或2
這是我的代碼:
IEnumerator Start()
{
itemCount = 3;
for (int i = 0; i < itemCount; i++)
{
StartCoroutine(TextureFromURL(textureURLString[i], (Sprite callback) =>
{
print("i = " + i);
AddItem();
GameObject.Find("pashaItem(Clone)").GetComponentInChildren<Text>().text = descriptions[u];
GameObject.Find("pashaItem(Clone)").transform.GetChild(1).GetComponent<Image>().sprite = callback;
GameObject.Find("pashaItem(Clone)").name = "pashaItem" + (u + 1).ToString();
}));
}
}
IEnumerator TextureFromURL(string url, Action<Sprite> callback)
{
Sprite s = new Sprite();
WWW textureWWW = new WWW(url);
yield return textureWWW;
if (!String.IsNullOrEmpty(textureWWW.error))
{
print("null ve ya bos");
}
else
{
Texture2D myTexture = textureWWW.texture;
s = Sprite.Create(myTexture, new Rect(0, 0, myTexture.width, myTexture.height), new Vector2(0.5f, 0.5f));
}
callback(s);
}