Button Attribute
Overview
The ButtonAttribute class is a custom attribute that can be used in Unity to draw a clickable button in the Unity Inspector window. When this attribute is applied to a method within a MonoBehaviour script, it allows the method to be invoked directly from the Inspector by clicking the button, without the need to play the scene or write additional editor scripting.
Usage
To use ButtonAttribute, you simply place it above a MonoBehaviour method. This attribute takes an optional ButtonMode parameter that controls when the button is active.
Warning
The method must not have any parameters and can not be static.
Tip
Use ButtonAttribute.Mode.Play if you want the Button to only work in Play mode.
Tip
Use ButtonAttribute.Mode.Editor if you want the Button to only work in Edit mode.
Example:

using CoreFramework.Attributes;
using UnityEngine;
public class ButtonExample : MonoBehaviour
{
[Button(ButtonAttribute.Mode.Play)]
public void PlayModeButton()
{
Debug.Log($"{name}: {GetType().Name} : Play Mode Only");
}
[Button(ButtonAttribute.Mode.Editor)]
public void EditModeButton()
{
Debug.Log($"{name}: {GetType().Name} : Edit Mode Only");
}
[Button]
public void PlayAndEditModeButton()
{
Debug.Log($"{name}: {GetType().Name} : Play Mode and Edit Mode");
}
}
public class MyComponent : MonoBehaviour
{
[Button]
private void ResetValues()
{
// Reset some values here
}
[Button(ButtonAttribute.ButtonMode.Editor)]
private void InitializeSettings()
{
// Some initialization logic that should only run in the editor
}
}
In this example, the ResetValues method will have a button in the Inspector that is clickable both in Play and Editor modes, while InitializeSettings will only be clickable in Editor mode.
Properties
Mode
public readonly ButtonMode Mode;
Read-only property that gets the mode in which the button should be active. The mode can be Editor, Play, or Both, determining whether the button can be clicked while Unity is in Editor mode, Play mode, or both.
Constructor
public ButtonAttribute(ButtonMode mode = ButtonMode.Both)
Initializes a new instance of the ButtonAttribute class with an optional ButtonMode parameter. If no parameter is given, the button defaults to being active in both Editor and Play modes.
Enums
ButtonMode
Defines the modes in which the button is operational.
Editor
Editor
The button will only work in the Unity Editor mode when Unity is not in Play Mode.
Play
Play
The button will only work when Unity is in Play mode.
Both
Both
The button will work in both Editor and Play Mode.
Notes
- The attribute must target a method and cannot be used on properties or fields.
- To make the button visible, the method must not have any parameters, can not be static and needs to be decorated with the
ButtonAttribute. - The functionality of rendering the button in the Inspector and invoking the method is handled by custom editor scripting,
AttributeMonoBehaviourInspector : Editor,AttributeScriptableObjectInspector : AttributeMonoBehaviourInspectorandButtonAttributeHelper.- These scripts are automatically used for drawing all classes that derive from
MonoBehavioursandScriptableObjectsunless you have created aCustome Editor Script.csharp [CustomEditor(typeof(MonoBehaviour), true, isFallback = true)] [CanEditMultipleObjects] public class AttributeMonoBehaviourInspector : Editor
- These scripts are automatically used for drawing all classes that derive from
See Also
- Unity's custom editor scripting: CustomEditor
- Unity's manual on extending the editor: Extending the Editor