Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Update class in editor but not when playing

Discussion in 'Scripting' started by IgorAherne, Jul 22, 2017.

  1. IgorAherne

    IgorAherne

    Joined:
    May 15, 2013
    Posts:
    393
    There can be a time when you want monobehavior (or any class) to receive Update callbacks in editor.
    For that you can use [ExecuteInEditMode]

    However, sometimes this gets on the way, - you might want to use Start() only during the game, but it's ran after you exit play mode. Other horrible things might happen.

    Sometimes things like EditorApplication.isPlayingOrWillChangePlayMode are not always working like you would expect, especially during the serialization.

    It can be hard to detect when the editor stopped playing, to re-hook your functions back, etc

    Here is code which allows any class (including Monobehavior) to receive update callbacks in the editor specifically when not playing.
    Code (CSharp):
    1.  
    2. //Igor Aherne  www.facebook.com/igor.aherne
    3. public class MyClass{        
    4. #region update in editor NOT in play mode
    5. #if UNITY_EDITOR
    6.         [UnityEditor.Callbacks.DidReloadScripts]
    7.         public static void OnRecompiledScripts() {
    8.             UnityEditor.EditorApplication.playmodeStateChanged -= PlayModeStateChanged;
    9.             UnityEditor.EditorApplication.playmodeStateChanged += PlayModeStateChanged;
    10.  
    11.             PlayModeStateChanged();
    12.         }
    13.  
    14.  
    15.  
    16.         public static void PlayModeStateChanged() {
    17.             Debug.Log("playmodeChanged");
    18.             if (UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode) {
    19.                 toggle_allEditorUpdateHooks(false);
    20.                 return;
    21.             }
    22.  
    23.             //make sure all FSMs are getting updates
    24.             toggle_allEditorUpdateHooks(true);
    25.         }
    26.  
    27.  
    28.         private static void toggle_allEditorUpdateHooks(bool isTrue) {
    29.             MyClass[] myClasses = Component.FindObjectsOfType<MyClass>();
    30.             foreach (MyClass myClass in myClasses) {
    31.                 myClass.toggle_EditorUpdateHook(isTrue);
    32.             }//end foreach
    33.         }
    34.  
    35.  
    36.         public void toggle_EditorUpdateHook(bool isOn) {
    37.             UnityEditor.EditorApplication.update -= UpdateInEditor;
    38.  
    39.             if (isOn) {
    40.                 UnityEditor.EditorApplication.update += UpdateInEditor;
    41.             }
    42.         }
    43.  
    44.  
    45.         private void UpdateInEditor() {
    46.              Debug.Log("running in editor, every frame, but not while playing the game")
    47.         }
    48. #endif
    49. #endregion
    50. }
    51.  
     
    Last edited: Aug 25, 2017
    Baste likes this.