So as the title says. ExecuteInEditMode executes static constructor in three cases:
When project is opened
When editor state changed to playmode
And when scripts recompilation finished.
PlayMode is easily discarded with EditorApplication.isPlayingOrWillChangePlaymode, but I can’t figure out how to disable execution after recompilation… To be clear, I need to execute some code only when Editor launched, once
Okay I figured it out. To get OnUnityEditorOpened event:
We need create some empty ScriptableObject type.
Every time in ExecuteInEditMode constructor we’ll check “is our SO in memory?” with Resources.FindObjectsOfTypeAll.
If we’ll find our SO in memory - it isn’t the first time we got this event.
Otherwise we’ve got our event! Here we need to instantiate our SO and set hide flags to DontSave (this way SO will live in memory without any references to it and we’ll be able to find it later)