MonoBehaviour.runInEditMode - Is it what I think it is? How do I use?

I need one specific script to run continuously in edit mode for a period of time, without having to change things on the scene for it to Update.

Would runInEditMode be what I need for that?

And if so, how am I supposed to use it?

What I’ve tried so far is setting it to true … using [ExecuteInEditMode] and placing “this.runInEditMode = true” in Update()… and also setting it true from an EditorScript just in case, so yeah, trying random things at this point.
I’ve debugged “this.runInEditMode” and confirmed it’s true. Yet still ONLY updates when I perform changes on scene.

For my first attempt I created the following script and attached it to a cube. When the cube is selected, or any changes are made to the scene, it is updating. There is very little information on this available so I’m guessing it’ll be trial and error.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[ExecuteInEditMode]
public class Foo : MonoBehaviour {

    // Use this for initialization
    void Start () {
        this.runInEditMode = true;
    }

    // Update is called once per frame
    void Update () {
        this.transform.Rotate(new Vector3(1.0f, 0.0f, 0.0f));
    }
}
1 Like

Hey! And this is working? Very interesting!! Maybe I’m doing something too weird, I can’t manage to update continuously. But it’s good to know that’s how it should work, Thank you!!

For my second attempt I abandoned the boolean. It’s constantly updating, but it’s constantly spewing an error too.

Random Test Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpinSprite : MonoBehaviour {
    void Update () {
        transform.Rotate(new Vector3(0.0f, 0.0f, 1.0f));
    }
}

Editor Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class ForceUpdate : EditorWindow {
    [MenuItem("Window/Force Update Window")]
    private static void Init()
    {
        ForceUpdate window = (ForceUpdate)EditorWindow.GetWindow<ForceUpdate>();
        window.position = new Rect(0, 0, 25, 25);
        window.Show();
    }

    private void Update()
    {
        foreach (MonoBehaviour mb in FindObjectsOfType<MonoBehaviour>())
        {
            mb.SendMessage("Update");
        }
    }
}

Here is the error message. Repeated every frame from the looks of it.

For the record all testing was done under 2017.1f3.

1 Like

Eep! So weird. I wonder if I should stick to this route or try a whole new attempt for what I want to do. :-p

I can think of…

Abuse OnRenderObject

I actually used the OnRenderObject detour approach already.

Abuse EditorApplication.delayCall

  • Decorate MonoBehaviour with the ExecuteInEditMode attribute

  • In the MonoBehaviour Awake() call, add the Update method to EditorApplication.delayCall

  • In the Update() method, add itself to EditorApplication.delayCall again

Never used this approach, but could work and sounds simpler.

2 Likes

Amazing tips!

Hey, what is an UNITY_EDITOR block? Is it using #if UNITY_EDITOR? :-\

Yes, that’s what I was referring to.

1 Like

One more question :-0

How do I add the Update method to EditorAplication.delayCall?