I am writing a library (The Whirld project) with functions that can be called from an editor panel or a running game.
This library performs time consuming tasks such as downloading files off the internet, so it essential that it be able to yield when called from a running game.
Unfortunately, scripts running in the editor can not yield. I personally feel that it is incredibly unintuitive to be forced to write editor scripts that lock up the entire Unity editor while they are running - without even giving the user the option of terminating the script - but hey, technical limitations are technical limitations.
My first approach to this problem was to check if(Application.isPlaying) in my library, and yield only if this was the case. Logically, this would work - but for some strange reason, if a yield call is present in a script running in the editor - even if it is NEVER called - the script instantly returns nothing.
My second approach was to build a mini library to handle yields. In my main library, I check if(Application.isPlaying). If it is, I call a yield function in my mini library. If not, I just lock up the editor waiting for the task to complete…
The yield function in my mini library looks like this:
function WWWLoad(www : WWW, whirld : Whirld) {
Debug.Log("loading");
yield WWWWait(www, whirld);
Debug.Log("loaded");
}
function WWWWait(www : WWW, whirld : Whirld) {
while(![url]www.isDone[/url]) {
Debug.Log("waiting");
whirld.progress = [url]www.progress;[/url]
yield new WaitForSeconds(.1);
Debug.Log("waited");
}
}
Whenever it is run, it prints “loading”, “waiting”, and then gives an error about my main library attempting to access a www stream that hasn’t finished downloading. I think it is because my main library also needs to yield the call to the mini library - which it can’t do, because that will cause it to fail when running as an editor script.
At this point, the only option I see is for me to duplicate the entire code of my library into yielding and non yielding versions - which would be an incredible step backwards. Hopefully I am missing something, and someone will be able to show me a way around this apparent bizarre limitation of Unity scripting.