Unity/Windows Forms Focusing Issue

I’ve been working on an Editor Extension that will let people run the NUnit gui application from inside Unity so that it can be used to run unit tests on all of a person’s scripts. After some tweaking to the NUnit source code, I’ve pretty much pulled it off, but there’s one quirk I would like to try and fix:

The problem is that NUnit gui is a windows forms application, and when I create it, Unity seems to consider it an external application, so switching focus to NUnit gui steals focus from Unity and my EditorApplication.update() functions are no longer called until I switch focus from NUnit gui back to Unity. This is a problem for me because the part of the logic that actually runs the unit tests needs to be run from the main Unity thread in order for non-thread safe function calls like Resources.Load() to work correctly. I already figured out how to implement communication between the threads, but if my Unity update() functions are never called when NUnit gui has focus then I have to manually switch focus between them to get Unity to run the tests, then switch back to NUnit gui to see the results, which is annoying, especially if switching focus to Unity causes my NUnit gui to disappear behind the Unity editor window.

The ideal solution would be to have NUnit gui somehow recognized as a child window of the Unity editor (I’m skeptical about whether or not this is even possible). At the very least I would like to try and find a way to swap focus between them automatically, so that Unity gets focus immediately when you decide to run tests, and focus switches back after the test run is complete. Anyone have any ideas?

I actually found the answer to my own question, over dinner I thought about trying to poke Unity by forcing it to go through an update cycle programmaticaly and see if that was possible/would solve my problem. After some searching I found EditorApplication.Step() does the trick, best of all it doesn’t seem to take focus away from NUnit gui and just runs my tests behind the scenes, while NUnit gui gets real-time test results by means of event call-backs to show to the user. I can’t believe this problem had such an easy solution :stuck_out_tongue:

I just found out my solution yesterday was incorrect, since calling EditorApplication.Step from outside the main Unity thread is also illegal. I have no idea why it was working yesterday, from what I understand I should have gotten an exception. Nevertheless I have discovered another equally painless solution, setting: Application.runInBackground = true just before opening my NUnit gui window will cause update functions to continue running even when Unity loses focus. I have noticed however that having this property set to true and making code changes seems to trip up Unity and cause it to freeze when it needs to recompile, so I set the property back to false when my NUnit gui window is closed (can probably set it up to trigger any time NUnit gui loses focus to make it even safer).