Executing a script in edit mode

I have created a custom inspector for a script I made, and within this custom inspector there is a button which I want to use to “Test run” the script while in edit mode. The behavior I need is exactly like that of a particle system when it is selected during edit mode (so I know this HAS to be possible).

Is there a way of achieving the above mentioned behavior?

By the way, I have tried using the [ExecuteInEditMode] attribute, but this only seems to call the update method when something in the editor changes or I manually move the object. I could be using it wrong, but there isn’t much to it unless I completely missed something. If anybody has a link to a good example of this being used, I would appreciate it if it was linked here.

After a little more research I found out that placing and selecting a Particle system into the scene will cause my script’s update method to get constantly called while in edit mode. Does anybody know how unity achieves this behavior? They must be calling some method that forces the editor to update all scripts with the [ExecuteInEditMode] attribute.

Alright I found a workaround for you which I think might work, is also elegant and might have the same result as the Particle Emitter in unity . Just write a CustomEditor for your component and define an OnEnable() and OnDisable() method on it. In the OnEnable you add a CallbackFuntion delegate on the EditorApplication.update event. And in the OnDisable you remove that CallbackFunction. Something like this:

public class MyScriptEditor : Editor
    private void CallbackFunction()
        // Call the update of the MyScript

    void OnEnable()
        EditorApplication.update += CallbackFunction;

    void OnDisable()
        EditorApplication.update -= CallbackFunction;

So as long as you have selected the right GameObject, it’s update will get called. I tried with the Debug.Log and got updates regularly.
Try it and see if it works. You might also want to call the Awake() or Start() in the OnEnable() method (if needed)

Cheers! And let me know if this does the job for you.

I don’t know if it’s possible but can’t you just call the Update() of the script in the Update() of the CustomEditor script? Either make the method public or use the sendMessage() to the corresponding GameObject.

So as long as the CustomEditor is visible (which is when you’ve selected a GameObject that has your script attached to it), your script will also execute it’s Update() function. Of course you can tweak that afterwards as you like

Hope this helps, if this works let me know alright!


In the Scripting API, it was mentioned that [ExecuteInEditMode] will only call update if something has been changed, because there are no frames running in edit mode, but the regular UnityEngine.MonoBehavior.Update() is invoked each frame, and if are really no frames the regular Update() can’t really be invoked by Unity. See this. And talking about Particles, they are simulated which means that while running, they’re in their little local play mode with nothing else in it.