Editor Coroutines
Like what your seeing?
Support us as a GitHub Sponsor and get instant access to all our assets, exclusive tools and assets, escalated support and issue tracking and our gratitude. These articles are made possible by our GitHub Sponsors ... become a sponsor today!
Introduction
As you may know by default coroutines don't work in the Unity editor.
Why?
StartCoroutine is part of GameObject and depends on the Update loop of the simulation. Unity has released a package that adds the feature into the Editor scripts but its "preview" and wonky at the moment ... and its pretty easy to do your self so this article will help with that.
Approach
The approach is simple enough, we will register an event handler on Unity's EditorApplication.update
event; this event gets called on update so will work like the GameObject update loop does ... mostly.
Next we will handle that event with our own little take on iterating coroutines, this means we will be tracking each coroutine and iterating over them all updating them as required on each "update" of the editor application.
Finally we need our own StartCoroutine(IEnumerator handle)
method for the event handler to work on
Solution
Now you could create a static class or another global solution to hold these methods but we tend to create these in each editor script such that debugging is a bit easier and scoped to each script on its own.
The example I will show here assumes your creating this in your editor script as opposed to some global solution.