- Home /
Concurrency and editor scripts and the play mode
Update:
I solved the issue and detailed the whole automated smoke test process here: Answer to Continuous Integration with Unity?
Original post:
This question is related to this one: Programmatically playing a scene for a single frame
The core issue I want to address is that it seems impossible to stop a running game from a seperate thread. I use an editor script to setup a scene and run it using
EditorApplication.isPlaying = true
I manage to signal the end of a frame but I can't access EditorApplication from a seperate thread (error at runtime). This is normal as with most game engines some things must stay synchronized over the main thread
If I try to block the editor script (OnGUI/Update) this results in a deadlock of the whole application
If I start the playmode paused and try to do just one step (EditorApplication.Step()) I never get the signal (i.e.
yield return new WaitForEndOfFrame();
never returns)
Are there any other ideas to try? Otherwise I will stick to having to run an abitrary number of frames as prescribed in the linked Q&A.
Answer by Bunny83 · May 25, 2011 at 02:52 PM
You can't and shouldn't access any unity api from another thread. The Unity api only works in the main thread (or the thread that fires your events). You can use threads to do your own stuff but you can't use the api. The only thing that works so far is Debug.Log() ;) but i'm not sure if it's even thread safe
I discovered that Unity needs some frames until everything is initialized. That's not a constant amount. It depends on many things like: is the gameview visible or not, is "maximize on run" on. I would run at least 20-50 frames. When pressing "play" unity saves the current state of the scene so it can revert the changes made at runtime.
http://forum.unity3d.com/threads/17721-Unity-threads from Aras on the matter.
Yeah, you are right. I posted this question in an effort to highlight one aspect of the issue I linked to. If WaitForEndOfFrame would work in conjunction with Step() I wouldn't have to try employing seperate threads at all.. I'll give you the karma for your answer, through :-)
If you need to execute your code at edit-time you can use the delegate "update"
http://unity3d.com/support/documentation/ScriptReference/EditorApplication-update.html
As far as i remember it is called 100 times per second.
Now I only need to figure out how to do all this in batchmode. The batchmode quits when the static method quits. If I don't use -quit on the command line the playmode still seems not to trigger, but I need to play around a bit more to be sure - and maybe file another question :D
Your answer
Follow this Question
Related Questions
Is there a limit to Test Runner play mode test duration? 2 Answers
DestroyImmediate(component.gameObject) destroys component but not gameObject 1 Answer
TestRunner playmode timeout (?) 3 Answers
Play mode testing stalled 0 Answers
Enter play mode from editor script after baking is complete 0 Answers