- Home /
downloading data via coroutine in Update()
I've written a coroutine that fetches a text file with a number of url's in them to images and then goes about downloading those images and puts them in a texture2d array. This works fine if i call on the coroutine in Start(), however what i want the coroutine to do is download the first image in the list (of urls) in Start() and then depending on user interaction i want to download each next image in the list, this user interaction is registered in Update(), but if the coroutine is called upon in Update() nothing happens, no data is transferred at all... Could anyone help me out here? I'd be grateful for it.
Its quite a lot of code, but lots of it unrelated to the problem in this function. All the rest is for creating puzzle tiles out of a single image so i left that all out. Anyway i just tested this as written down below and again it works if called upon in Start(), but not Update().
So i want to call on this coroutine in update, so that i can download a new image in the list based on user interaction.
So here it goes:
public IEnumerator loadPuzzleTiles_index(int puzzelNR)
{
// download list url's puzzle images
yield return puzzleTiles_index_www;
// download image in list
if (puzzleTiles_index_www.isDone)
{
puzzleTiles_indexList = puzzleTiles_index_www.text;
puzzleTiles_indexArr = puzzleTiles_indexList.Split('\n');
puzzleTiles_puzzleIndex_www = new WWW[puzzleTiles_indexArr.Length];
puzzleTiles_puzzleIndex_texture = new Texture2D[1];
puzzleTiles_puzzleIndex_texture[0] = new Texture2D(832,664,TextureFormat.ARGB32,false);
puzzleTiles_puzzleIndex_www[puzzelNR] = new WWW(puzzleTiles_indexArr[puzzelNR]);
yield return puzzleTiles_puzzleIndex_www[puzzelNR];
if (puzzleTiles_puzzleIndex_www[puzzelNR].isDone)
{
puzzleTiles_puzzleIndex_texture[0] = puzzleTiles_puzzleIndex_www[puzzelNR].texture;
}
}
}
Coroutines are not my strong point since I only use them in pretty standard ways, but Line 6 concerns me. What happens if you change line 6 to:
while (!puzzleTiles_index_www.isDone) {
yield return null;
}
And make sure you are only calling this coroutine once from Update().
Excellent, looks like this is working! Thank you so much. I'd like to make it possible that the application not almost completely freezes more or less when the data is being downloaded. Is that doable you think?