How to not lockup the main thread in editor script

I’m making an editor script to do some stuff to a bunch of our scenes.

Roughly:

  • Open scene
  • Save it as something else
  • Edit the new scene
    …over and over again.

It works fine, but it completely locks up the UI for that time. You can’t even see the levels opening and closing, and debug messages don’t come through until everything is done. And I completely understand that.

But how can I get around it? You can’t run coroutines in editor scripts as far as I can tell.
I tried using http://wiki.unity3d.com/index.php?title=CoroutineScheduler, which actually works occasionally. After each action I put yield return 0; which updates the UI.
The problem is that most of the time it doesn’t work. I can’t figure out why, but it’ll get to a yield statement and basically just stop.
I’ve confirmed that I do carry on running _scheduler.UpdateAllCoroutines, so that isn’t the issue.

So is there a better way to give the Unity UI enough time to show the new scenes opening etc before I carry on what I’m doing?

Thanks.

p.s. A lot of what I’m doing requires it to be on the main thread, so I can’t just throw everything into the background.

You can run corountines from editor scripts with this. You can also try running the work in a background thread. (see examples). Since you need access to the main unity thread using the coroutine stuff would be your best bet. Using a background task would require you to use the Task.RunOnMain extension method to run on the main thread from the background.

For my localization tool, I did not want to include my task library (to keep my tools as modular and dependency free as possible) so I used this script to scheduled coroutines from my editor window.

https://github.com/NVentimiglia/Unity3d-Localization/blob/master/Foundation%20Localization/Assets/Foundation/Localization/Editor/EditorCoroutine.cs