How would I get a script to execute only in editor, but not at runtime?

I’ve been working with getting paper 2.5D working. This is what I’m working with:

(skip to 3 minutes 30 seconds if you watch)

The thing is, when I build it or just hit play in the editor, it runs the script. I want two scripts that run separately.

So after looking on google.com the first thing I was trying was to use [ExecuteInEditMode]. With a script attached to the object this will run when play is hit also… So I tried #if (UNITY_EDITOR). That didn’t work. I tried if (Editor.Application.isPlaying) { } to isolate the code, that didn’t work.

Then I tried making an editor script with [ExecuteInEditMode] and trying public override OnInspectorGUI() to try to get some kind of Debug.Log(“Hello world”); script going. That didn’t work.

I tried in the editor script this as well:

using UnityEngine;
using System.Collections;
using UnityEditor;

[ExecuteInEditMode]
public class CustomInspector : EditorWindow {

    void Update() {
        Debug.Log ("Hello there");

    }
}

That’s not doing anything. I don’t care if it updates inconsistently, because it’s the editor after all. It was working when the script was attached to the object, but I don’t want it to run at runtime and I don’t know how to make it so it doesn’t.

    if (Application.isEditor && !Application.isPlaying) {
        // stuff
    }

–Eric

2 Likes

Eric5h5’s solution should work but has the potential to create excessive calls if you’re doing this a lot. Better to use platform dependent conditionals to compile out the code entirely.

public class YourMonoBehaviour : MonoBehaviour
{
// you can compile out entire functions::
#if UNITY_EDITOR
    public void Start() { Debug.Log("This is called."); }
#endif

// or specific parts:
    public void Update()
    {
      Debug.Log("Called in the editor and runtime.");

#if UNITY_EDITOR
    Debug.Log("Only called in the editor.");
#endif
    }
}

Don’t wrap the conditional in parenthesis like you did. Here’s a list of Unity defined conditionals: Unity - Manual: Conditional compilation

2 Likes

Alright, gonna try both and see what happens…

@GroZZleR

Okay, so Eric5h5’s solution worked. I don’t know why but the #if UNITY_EDITOR #endif way of doing it doesn’t work for me, it will still play when played.

It would run in play mode because it’s still in the editor; it’s not really possible to have a #define for “is editor but not playing”. (Unless you wanted your code to compile every time you hit play.) But you could combine #if UNITY_EDITOR with Application.isPlaying.

–Eric

1 Like

That makes sense. Alright, fair enough. Thanks guys.

Ok, so is it OK to use the below to make sure it only runs in the editor and does not cause excessive calls in the build?

#if UNITY_EDITOR

if (Application.isEditor && Application.isPlaying) {
// stuff
}
#endif

Probably only need the second part of the if statement, as there’s no reason to check editor twice :slight_smile:

1 Like

you are correct :). Thanks

No problem :slight_smile: