Editor commands and initialization attributes
↑At a glance
Section titled “At a glance”Use method-level attributes for small Inspector actions, Editor menu commands and initialization hooks. The most important distinction is where the method runs: in the Unity Editor or in the built game.
Quick reference
Section titled “Quick reference”| Attribute | What it does | Applies to and requires |
|---|---|---|
[ContextMenu("Title")] |
Run an instance method from the component context menu. | Method · UnityEngine |
[MenuItem("MyMenu/Action")] |
Add a command to the Unity Editor menu bar. | Static method · UnityEditor |
[InitializeOnLoadMethod] |
Run a static Editor method after Unity loads or reloads assemblies. | Static method · UnityEditor |
[RuntimeInitializeOnLoadMethod] |
Call a static method during runtime initialization. | Static method · UnityEngine |
How to choose
Section titled “How to choose”[ContextMenu("Title")]
Section titled “[ContextMenu("Title")]”Run an instance method from the component context menu.
Use it when: Run a quick validation or reset command from a component.
Be aware: Suitable for editor conveniences such as resetting or testing component state.
Reference: Unity documentation
[MenuItem("MyMenu/Action")]
Section titled “[MenuItem("MyMenu/Action")]”Add a command to the Unity Editor menu bar.
Use it when: Provide a reusable tool from a Tools menu in the Unity Editor.
Be aware: Editor-only API. Put code in an Editor folder or Editor-only assembly.
Reference: Unity documentation
[InitializeOnLoadMethod]
Section titled “[InitializeOnLoadMethod]”Run a static Editor method after Unity loads or reloads assemblies.
Use it when: Register editor tooling after assembly reload without user interaction.
Be aware: This is Editor initialization, not game start or player build initialization.
Reference: Unity documentation
[RuntimeInitializeOnLoadMethod]
Section titled “[RuntimeInitializeOnLoadMethod]”Call a static method during runtime initialization.
Use it when: Set up runtime services before or during scene initialization.
Be aware: Use an explicit RuntimeInitializeLoadType when the initialization phase matters.
Reference: Unity documentation
Two kinds of commands
Section titled “Two kinds of commands”using UnityEngine;
public sealed class ResettableHealth : MonoBehaviour{ [SerializeField] private int health = 100;
[ContextMenu("Restore Full Health")] private void RestoreFullHealth() { health = 100; }}The method belongs to the component and appears in its Inspector context menu.
using UnityEditor;using UnityEngine;
public static class ProjectTools{ [MenuItem("Tools/HCSTech/Print Project Path")] private static void PrintPath() { Debug.Log(Application.dataPath); }
[InitializeOnLoadMethod] private static void SetupOnEditorLoad() { // Keep startup logic small and idempotent. }}Place the second file under Editor/ or in an Editor-only asmdef. The compiler must not include UnityEditor references in a player assembly.
using UnityEngine;
public static class RuntimeStartup{ [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] private static void Initialize() { Debug.Log("Runtime initialized before the first scene loads."); }}