[InitializeOnLoad] for editor load only

Hi all,

I’ve been trying to use [InitializeOnLoad] attribute, to run some code when the editor launches. However, I’m finding two main issues:

  1. The static class is constructed twice (seems to be a known issue)
  2. The static class is constructed when I hit Run, which seems to be by design.

I can’t find any way to ignore the second construction on editor launch, which might not be an issue.

More importantly I cannot find a way to ignore the construction when hitting Run I have tried testing for Application.isPlaying, but this returns false, as I assume we’re running very early in the initialization. According to the API, “when Run is pressed … the Unity runtime is intialised and this is treated as a Load.”, so I guess I’m looking for a way to detect this state.

FYI. I’ve also tried looking into [InitializeOnLoadMethod] and [RuntimeInitializeOnLoadMethod] but to no avail.

Maybe this comes too late.
I just got the same problem and found an answer:
You can use SessionState to store a key in it to avoid multiple invokes of the method.

[InitializeOnLoad]
public static class ProjectOpenEvent
{
    private const string k_ProjectOpened = "ProjectOpened";

    static ProjectOpenEvent()
    {
        if (!SessionState.GetBool(k_ProjectOpened, false))
        {
            SessionState.SetBool(k_ProjectOpened, true);
            Debug.Log("[Debug]: project opened");
            
            // DO WHAT YOU WANT
        }
    }
}

EditorApplication.isPlayingOrWillChangePlaymode might be what you’re looking for.

[InitializeOnLoad]
public class Test
{
static Test()
{
const string k_ProjectOpened = “ProjectOpened”;
if (!SessionState.GetBool(k_ProjectOpened, false) && EditorApplication.isPlayingOrWillChangePlaymode == false)
{
SessionState.SetBool(k_ProjectOpened, true);
//code here
}
}
}
None of the answers helped me so I combined them and it worked…

For anyone that comes across this, the above methods are completely wrong. It’s running twice because the type is constructed both for play mode and edit mode. To properly handle this, you should be using the event that is being passed to you like so:

using System;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public static class Program
{
    static Program() => EditorApplication.playModeStateChanged += playMode => (playMode switch
    {
        PlayModeStateChange.EnteredPlayMode => () => Debug.Log("play enter"),
        PlayModeStateChange.ExitingPlayMode => () => Debug.Log("play exit"),
        _ => (Action)(() => { }) // ignore other states
    })();
    // the above cast on the default case lets us treat the return as an Action
    // so we wrap the switch expression in parenthesis and invoke using `()`
    // you could also call .Invoke() if you prefer
    // (this allows you to run the switch expression without an assignment)
}

If you prefer you can use a normal switch:

switch (playMode)
{
    case PlayModeStateChange.EnteredPlayMode:
        Debug.Log("play enter");
        break;
    case PlayModeStateChange.ExitingPlayMode:
        Debug.Log("play exit");
        break;
    default: return;
}

You could also set it to a method group:

EditorApplication.playModeStateChanged += OnPlayModeChanged;

static void OnPlayModeChanged(PlayModeStateChange playMode)
{
    // switch here
}