Skip to content

Editor commands and initialization attributes

↑
Unity 6 reference

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.

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

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

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

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

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

Unity C# | ResettableHealth.cs
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.

Unity C# | Editor/ProjectTools.cs
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.

Unity C# | RuntimeStartup.cs
using UnityEngine;
public static class RuntimeStartup
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void Initialize()
{
Debug.Log("Runtime initialized before the first scene loads.");
}
}