0

Is it possible (even in a hacky way) to call EditorApplication functions from another thread? More specifically I want to exit play mode from another thread (not main Unity thread).

My use case is, I'm trying to write a small snippet that detects endless loops while in the editor, and breaks out of them in case of detection. So far the "best" I found is killing the process, but this doesn't really help.

Ron
  • 1,806
  • 3
  • 18
  • 31
  • Take a look at this answer https://stackoverflow.com/a/41333540/8628766 it may be overkill for what you want to do, but it is the most proper way to go around it.. There are easier and (way) more sketchy ways though if you want to keep it simple... – Remy Aug 20 '19 at 12:39
  • hey @remy_rm - I've read said solution and wrote something similar in the past, but I need to actually call `EditorApplication` from a different thread, I can't forward it to the main thread, as it's stuck in an endless loop – Ron Aug 20 '19 at 12:43
  • 1
    Ah yeah I see, sorry I misunderstood. Though there is no way to call it without switching to the main thread I think i've seen something that does what you want before. I'll post back here if I manage to find it – Remy Aug 20 '19 at 12:46
  • Haven't been able to find it, only things I came across were IEnumerartor based solutions, which inherently make infinite loops not a thing (well they're still a thing, but since you don't lose control you can always break away from them). Is there a specific reason you're not using your loops inside a coroutine? – Remy Aug 21 '19 at 08:12
  • Would being able to press a "kill switch" manually to break out of an infinite loop suffice for you? (This way you don't even have to call EditApplication to stop your game as you regain full unity control) Or does it actually need to detect the infinite loop itself? – Remy Aug 21 '19 at 09:49
  • btw something like this already exists ;) https://assetstore.unity.com/packages/tools/utilities/panic-button-40167 maybe someone willing to invest the money (it's really not much) could find out how it was done ^^ – derHugo Jan 15 '20 at 07:27
  • @derHugo I know that one, it doesn't work since Unity 2018. Thanks though – Ron Jan 15 '20 at 09:10

1 Answers1

0

You cannot. Unity thread is not like normal programs, in the sense that it is frame-based. You can ask Unity main thread to run methods, by setting a bool that is being checked at every frame or by setting an <Action> Or <Task> Queue, like it is explained in the link suggested by remy_rm in the comment above.

But these hacks don’t run methods on unity main thread, they just gracefully ask Unity main thread itself to run it. The difference, while subtle in normal cases, becomes vital with your problem. You want to call a method on the main thread to kill it when it’s stuck on an endless loop, but isn’t that case, Unity’s main thread will never reach the point in Update() where it’s supposed to kill itself. You basically send a letter to someone who is never coming back home, and he will never read it if he is stuck somewhere and cannot reach home.

Best way, that springs in my mind, in those cases, is to attach a debugger and stop the thread from it.

kefren
  • 1,042
  • 7
  • 12
  • Yeah I was hoping to find some way to call directly the native C++ code that stops playmode, or somehow throw an exception on the main thread – Ron Aug 20 '19 at 13:25
  • @Ron [This](https://assetstore.unity.com/packages/tools/utilities/panic-button-40167) exists. I don't know how its written, but it will do what you want. – Draco18s no longer trusts SE Aug 20 '19 at 13:36
  • @Draco18s It does not work with Unity 2018 (which I assume also means 2019) – kefren Aug 20 '19 at 13:37
  • @kefren Fair enough. Its just been an asset that's been in my peripheral as something I might want, but never got around to it. Didn't check to see if it was still current. – Draco18s no longer trusts SE Aug 20 '19 at 15:06