Start/Stop Playmode from editor script

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

1 Like

Nonsens... Application.isPlaying is read only

–

9 Answers

9

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

That's even better, thanks !

–

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

–

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

EditorApplication.ExecuteMenuItem("Edit/Play");

Sounds good, I'll try this

–

It worked ! Thanks you

–

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

Do you know what Debug.DebugBreak() does?

–

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

That's not necessary at all ;) I'll update my answer.

–

How about just using conditional compilation? Much 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! ;)

–

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.

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

@Pawcijo

no you cant.

–

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