I’ve been trying to use [InitializeOnLoad] attribute, to run some code when the editor launches. However, I’m finding two main issues:
The static class is constructed twice (seems to be a known issue, linked below)
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.
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;
}
not necromancing, but helping out, made use of @unity_gAMhN-EHnIqG0w and @lizhgong’s code to make the code get only executed in editor and when leaving exiting play mode to editor:
a timer that runs only on editor:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Drawing.Printing;
using System.ComponentModel.Composition;
[InitializeOnLoad]
public class BUH_Operator
{
private static int ticks = 0;
private static int required_ticks = 100;
public static void OnTick()
{
Debug.Log("Ticking...");
}
static void OnEditorUpdate()
{
ticks++;
if(ticks >= required_ticks)
{
OnTick();
ticks = 0;
}
}
static BUH_Operator()
{
// gets called only on editor but not when exiting play mode to editor
if (EditorApplication.isPlayingOrWillChangePlaymode == false)
{
Init();
}
// to run the init/method after exiting play mode
EditorApplication.playModeStateChanged += EditorApplication_playModeStateChanged;
}
// code that gets executed only on editor
static void Init()
{
EditorApplication.update += OnEditorUpdate;
Debug.Log("BUH Operator - Executed");
}
private static void EditorApplication_playModeStateChanged(PlayModeStateChange obj)
{
// only allow PlayModeStateChange.EnteredEditMode to pass
if (obj == PlayModeStateChange.EnteredPlayMode || obj == PlayModeStateChange.ExitingEditMode || obj == PlayModeStateChange.ExitingPlayMode)
{
return;
}
// execute init when exiting play mode to editor
Init();
}
}