Search Unity

Resolved Unity Editor Scripting Workflow

Discussion in 'Editor & General Support' started by Blenderik, Apr 20, 2023.

  1. Blenderik

    Blenderik

    Joined:
    May 14, 2013
    Posts:
    146
    I'm trying to make scripts run continously in the editor, but the more I try the more I get the feeling, this was not intended by Unity.
    if I do this:
    Code (CSharp):
    1. static MyBehaviour m_instance;
    2. void Awake(){ //Start isn't working either
    3.    m_instance = this;
    4. }
    It works on loading the scene. But whenever I change some code the m_instance is reset to null. As are all variables that are not exposed in the editor and specifically set dirty when changed by a script. Is there a workaround?
    I tried:
    - Assign it in Update(). Apart from me resenting this because it's so inefficient, it only works if you move the mouse over the scene window. Not if you move it over the Inspector.
    - using OnApplicationFocus(bool focus) but in the editor this only gets triggered when the user clicks into the game window and then away from it. Not on ALT Tab.
    - I even made a Coroutine, but that gets paused when the mouse isn't over the scene view. I found Task.Await(ms), which would technically work, but it's an aweful hack that I am trying to avoid.

    If you have general pointers on how to manage EditorScripts efficiently and reliably, I'd love to hear it.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    There is an editor coroutines package you can install via the Package Mangler.
     
    Blenderik likes this.
  3. villevli

    villevli

    Joined:
    Jan 19, 2016
    Posts:
    87
    Based on a quick test OnEnable() seems to get called after script compilation & domain reload for
    ExecuteInEditMode scripts that are already loaded. So you could set your static and non serialized fields that have been reset by the domain reload there
    Code (CSharp):
    1. static MyBehaviour m_instance;
    2. void OnEnable()
    3. {
    4.     m_instance = this;
    5. }
     
    Blenderik likes this.
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    Is this meant to be an editor only script?

    Going to be a lot easier to use
    [InitializeOnLoad]
    in a static class and just subscribe to
    EditorApplication.update
    to have some code run continuously.
     
  5. Blenderik

    Blenderik

    Joined:
    May 14, 2013
    Posts:
    146
    I tried that, but the
    EditorApplication.update does not seem to get called, when I ALT + Tab back into Unity. And it doesn't trigger when I use one of my custom inspector buttons to make changes to the scene. I didn't mention it, but I was already using
    [InitializeOnLoad], but that got reset when I changed code and the inspector redraws.
     
  6. Blenderik

    Blenderik

    Joined:
    May 14, 2013
    Posts:
    146
  7. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,859
    It gets called when the Unity editor repaints, I believe. And when the editor isn't focused or not being interacted with, it's not updating to save on overhead.

    You could try toggling this on:
    upload_2023-4-24_22-11-33.png

    I believe it will cause the scene view to always repaints, this the editor updates.