- Home /
Start/Stop Playmode from editor script
Is it possible to Start / Stop the player in the editor from an editor script ?
Answer by Bunny83 · Aug 31, 2011 at 12:27 PM
Well, you can also set EditorApplication.isPlaying ;)
edit
Since there were complaints that "EditorApplication" can't be used in runtime scripts, here's how you can use it:
//C#
public static class AppHelper
{
#if UNITY_WEBPLAYER
public static string webplayerQuitURL = "http://google.com";
#endif
public static void Quit()
{
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#elif UNITY_WEBPLAYER
Application.OpenURL(webplayerQuitURL);
#else
Application.Quit();
#endif
}
}
Use this class from anywhere like this:
AppHelper.Quit();
UT way of thinking surprises me over and over :)
In my way of thinking the "isX" property should be exclusively a getter.
And methods would be more appropriate for playing and stopping the app - like EditorApplication.stop();
But that's just me ;)
Answer by Sigil · Aug 31, 2011 at 06:05 AM
I didn't see a direct call, but this worked in my editor script (dll to be particular):
EditorApplication.ExecuteMenuItem("Edit/Play");
Answer by salat · Jul 26, 2013 at 05:50 AM
isPlaying work only in Editor scripts. in runtime use Debug.Break() to pause (not same as Debug.DebugBreak()) http://docs.unity3d.com/Documentation/ScriptReference/Debug.Break.html
Answer by Istador · Dec 18, 2013 at 09:42 PM
Debug.Break() is pausing the game, not stopping it.
Sadly you can't build a standalone version with "UnityEditor.EditorApplication.isPlaying = false;" in your code, because the class is unknown to the standalone version.
If you don't want to comment out the line everytime you want to produce a standalone build, you can use reflection to dynamically check for the class and change its property:
using System;
using System.Reflection;
//only works in a standalone build, ignored in editor and webplayer
Application.Quit();
//only works in editor
//casts "UnityEditor.EditorApplication.isPlaying = false;" dynamically using reflection
Type t = null;
foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) {
t = a.GetType("UnityEditor.EditorApplication");
if(t != null){
t.GetProperty("isPlaying").SetValue(null, false, null);
break;
}
}
How about just using conditional compilation? $$anonymous$$uch cleaner, and considerably cheaper than reflection...
#if UNITY_EDITOR
Editor.IsPlaying = ....
#endif
This worked perfectly for me ... many thanks ... if I could package up all the saved clicks and mouse moves and convert them into bitcoins for you, I would! ;)
Answer by FiveFingers · May 18, 2013 at 06:01 PM
Application.isPlaying is read only play state. Accessing the menu works, but only on a editor script. Not on a runtime one. EditorApplicatio can't be used there.