I’m struggling to wrap my head around how to implement threading in Unity.
I have in may game a place where I check a web server to see if there are game level xml data updates and then download the new xml files. I’ve got all this working using the WWW class and leveraging yield. Here is a code fragment:
...
var www : WWW = new WWW (myURL);
// Wait for download to complete
yield www;
// put xml into a new string varible
var myXMLData : String = www.data;
// do stuff with myXML data
...
Again, I’ve got all this working just fine – files are downloaded and processed successfully.
On the game screen, I have a sprite gameObject of a simple spiral image that I rotate on the Z axis to show the user that the code is working. I have a simple Update function to rotate the sprite continuously:
function Update() {
myWaitIconSpriteImage.transform.Rotate(0, 0, Time.deltaTime * -60);
}
My problem is during the the download in the WWW class the rotation stalls and does nothing. I am aware that is is because during the downloading and processing of the file, no other code can execute since it’s all happening in the same frame.
Right. So I am trying to see about implementing the sprite rotation in its own simple thread but I’m stuck. I’ve read that the Unity API is not thread safe – but I am not sure that the “Unity API” covers or not cover to see about how I can have a simple sprite rotation not freeze.
I’m coding using UnityScript/Javascript. Any thoughts on how I can do this or any other method to achieve what I’m trying to do?
Thanks,
Manny