Start/Stop Playmode from editor script

Is it possible to Start / Stop the player in the editor from an editor script ?

Well, you can also set EditorApplication.isPlaying :wink:

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();

I didn’t see a direct call, but this worked in my editor script (dll to be particular):

EditorApplication.ExecuteMenuItem("Edit/Play");

isPlaying work only in Editor scripts.
in runtime use Debug.Break() to pause (not same as Debug.DebugBreak())

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;
  }
}

EditorApplication.EnterPlaymode();

See: Unity - Scripting API: EditorApplication.EnterPlaymode

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.

you can also make a Debug.LogError(“test”) and make the editor pause on error

I had to make an autoit script and execute it from app, its not purrfect, but it works most of the time

$status = 1
While $status = 1
    If WinActive("[REGEXPTITLE:(.*Unity.*)]") Then
        Send("^p")
        $status = 0
    Else
       WinActivate("[REGEXPTITLE:(.*Unity.*)]")
    EndIf
WEnd

In Unity 5.x u can change Application.isPlaying to false and it works :slight_smile:

@Pawcijo