unity coroutines at editor time
• unity and general
Did you ever try to do non-blocking WWW-requests in Unity without being in playmode ? Then you know about the problems here:
- WWW uses coroutines to be used non-blocking
- Coroutines do not work outside the playmode
- Triggering a coroutine in the editor using Update only works with an Editor-Window
The solution is twofold:
- Use EditorApplication.update to ‘tick’ in non-playmode
- Trigger coroutine manually
Pseudocode says more than a thousand words:
This does the trick and is not widely known unfortunately. Using EditorApplication.update we can register arbitrary callbacks that get called periodically in Editor-Mode.
Additionally understanding unity coroutines is necessary to understand what we are doing here:
A coroutine is nothing but a resumable method that is identified by the return type IEnumerator
and the usage of yield return xyz;
in its body.
Under the hood it works by ‘someone’ calling MoveNext of its IEnumerable interface. Usually this ‘someone’ is Unity internally. But since that does not happen outside the playmode we can force it to still do the same by calling it manually.
Final note: EditorApplication.timeSinceStartup is great for actual timing in the registered update-callback.