I’ve written a coroutine (Ienumerator) that downloads a bunch of images of a webserver, and it works as expected. The one problem i have though is that while the download is in progress the application itself is temporarily halted until the transfer is completed. Is it possible to run a data transfer in the background while not completely halting the application itself? And if it is, what are the specifics that i should look into? I have done several searches on the subject, but didn’t come up with anything helpful unfortunately.
You have to show the code.
Its really a generic function using the WWW class, which is called once in Update… but here you go.
Please use code tags. It makes the posts a lot more readable.
How do you start the coroutine? Are you using StartCoroutine?
I’m sorry, thought putting it in quotes would automatically format the code.
Yes, i am using StartCoroutine. Basically i just set a bool to true, then check if that bool is set to true, start the coroutine using StartCoroutine and subsequently set the bool to false on the next line inside the if statement.
I’m not setting the bool to true inside Update obviously, that wouldn’t make any sense in this context.
public IEnumerator loadPuzzleTiles_index(int puzzelNR)
{
// download list url's puzzle images
while (!puzzleTiles_index_www.isDone) {
yield return null;
}
// 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;
}
}
}
Here is the much more readable version. Could you add some comments in the code to explain what you are trying to achieve and where the coroutine does not work as expected.
I think i might probably have chosen not exactly the right approach to explain my question. I found this Unity, events, delegates, coroutines, async url data download example | while searching google. And i think it might be a solution to my problem but besides the code there isn’t really any documentation included, so i’m trying to figure out what its exactly about. Its this comment in the code that caught my eye:
// let’s animate the cube so that we can show that the load
// of the image via URL is not blocking
Perhaps what i’m asking makes more sense now?