Fun with coroutines

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.

No direct help on your original question, but I’ve tended to find that code with critical things embedded in deep loops or coroutines can often be rewritten to avoid this situation. If you can post your script, I might be able to suggest ways you can alter the code so that including or omitting coroutines is easier (then again, I might not, but it’s worth a try :wink: ).

Thanks!

Here is a link to the latest Whirld library as a Unity project:
http://s3.amazonaws.com/dat.marsxplr.com/whirld/WhirldB2.zip

The WhirldUTW.js file is the one that needs to contain the coroutines. Being able to yield is essential for this file, as it has to be able to download large terrain, mesh, texture, etc files from the internet without freezing the game that called it.