Search Unity

[Solved] Run an editor script exactly once, when Unity launches

Discussion in 'Editor & General Support' started by nickfourtimes, Oct 27, 2020.

  1. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    196
    I'm trying to run a little editor script exactly once, when Unity is launched. It looks something like this:

    Code (CSharp):
    1. [InitializeOnLoad]
    2. static class MyStartupCode {
    3.  
    4.     /// constructor
    5.     static MyStartupCode() {
    6.         // my code
    7.     }
    8.  
    9. }
    This does indeed run when the editor is first opened. However, it's also run every time my code recompiles (I'm assuming some assemblies are being reloaded/the static class is being reinitialised).

    I've seen someone mention using
    if (!EditorApplication.isPlayingOrWillChangePlaymode)
    , but this only prevents the code from running when I enter/exit Play mode.

    Can I prevent this code from being run every time my code compiles?
     
    Last edited: Oct 30, 2020
  2. hawken

    hawken

    Joined:
    Aug 22, 2013
    Posts:
    607
    have you tried using a boolean?
     
    nickfourtimes likes this.
  3. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    196
    Yeah, I tried adding something like
    static bool hasRunOnce = false;
    to the class, and then setting it to "true" once the code runs. But it seems that the whole class is reinitialised when my code is recompiled... so it always reverts back to the default "false" value, and then my code runs again.
     
  4. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,065
    As far as I know, there is no callback for when the editor starts, and as you've already experienced, static variables will be deleted and recreated sooner or later (when the assembly reloads due to recompile or potentially enter play mode depending on your settings).

    So, you could use
    UnityEditor.SessionState
    to check if you've already initialized. This will persist for the entire duration while the editor is open.

    Code (CSharp):
    1. [InitializeOnLoad]
    2. static class MyStartupCode
    3. {
    4.     static MyStartupCode ()
    5.     {
    6.         if (!SessionState.GetBool("FirstInitDone", false))
    7.         {
    8.             // Startup code here...
    9.             Debug.Log("First Init.");
    10.          
    11.             SessionState.SetBool("FirstInitDone", true);
    12.         }
    13.     }
    14. }
     
    Last edited: Oct 28, 2020
  5. nickfourtimes

    nickfourtimes

    Joined:
    Oct 13, 2010
    Posts:
    196
    Hey, that's it – it works! I was considering a workaround using
    EditorPrefs
    but this is much more straightforward. Thanks so much!
     
unityunity