Editor Scripting multi thread

Hello again. So i have a custom lightmap tool i put together. It works like a charm , multiple resolutions etc, but the thing is, when i use my custom tool, and choose to bake , everything stops , locks up, until the bake completes, and than you resume control and can go about work again.

What i would like to know , is how can i Coroutine this procedure, as i notice that the Unity Lightmap tool doesnt lock everything up, and as far as i can tell , this must be due to it using a seperate thread to run its method. I have looked into it , and it seems Coroutines dont work outside of runtime “Play” mode, so … how can this be done , or can it?

Im assuming there must be a way , as unitys doesnt lock up. Any ideas?

thanks ~Will.

You can use System.Threading.
Granted it could be painful due to Unity not being thread-safe, and so accessing certain variables and properties are going to have to be done on the main thread, the bulk of the work can be done on a separate thread.

Threading itself is a pretty large topic, but this thread has some links and sample code that may help get a starting point. (With the exception that the Job Scheduler itself won’t run as-is, due to no Coroutines in Editor, but I just added a workaround in comments).
http://forum.unity3d.com/threads/118918-hierarchy-of-yield-for-distributing-work-load-possible

Although in particular Threading in C# - Free E-book is a fantastic link to learn about .NET Threading tools; they work in both JS and C#, with the exception of some missing JS syntax, like lock.

Cool , thanks NTero , i wasnt aware that UnityScript had access to System.Threading. I will definately look into it , have used it before, back when i use to program small aps in .net , so hopefully shouldn’t be hard to adapt.

Thanks again m8,

JS, Boo, and C# all have access to the same .NET frameworks, which is one of the larger benefits of using .NET, and the only time a certain feature isn’t supported is because that language doesn’t have the syntax to request it (for example, List used to be unusable by JS/Boo because there was no syntax for generic parameters, but it could still use instances initialized in another language). All 3 languages compile to the same CIL code, and so all features are at least partially interoperable.

As a sidenote, if threading is too much refactoring, a quick fix to getting coroutines in Editor is to have EditorApplication.update call your IEnumerators .MoveNext function. This allows 1 frame yields (yield; or yield return null;) to work, but won’t handle special cases like waitforfixedupdate or wairforseconds.

That thread linked earlier shows an example of replacing StartCoroutine with EditorApplication.update (in the JobScheduler::Awake function).