Version 3.3.0.1

Odin has a dedicated attribute overview with examples

ButtonAttribute class

Namespace: Sirenix.OdinInspector
Assembly: Sirenix.OdinInspector.Attributes
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | AttributeTargets.All, AllowMultiple = false, Inherited = false)]
[Conditional("UNITY_EDITOR")]
public class ButtonAttribute : ShowInInspectorAttribute, _Attribute

Buttons are used on functions, and allows for clickable buttons in the inspector.

Inheritance
Example

The following example shows a component that has an initialize method, that can be called from the inspector.

public class MyComponent : MonoBehaviour
{
	[Button]
	private void Init()
	{
		// ...
	}
}
Example

The following example show how a Button could be used to test a function.

public class MyBot : MonoBehaviour
{
	[Button]
	private void Jump()
	{
		// ...
	}
}
Example

The following example show how a Button can named differently than the function it's been attached to.

public class MyComponent : MonoBehaviour
{
	[Button("Function")]
	private void MyFunction()
	{
		// ...
	}
}

Constructors

ButtonAttribute()
Creates a button in the inspector named after the method.
public ButtonAttribute()
ButtonAttribute(ButtonSizes)
Creates a button in the inspector named after the method.
public ButtonAttribute(ButtonSizes size)
Parameters
ButtonSizes size

The size of the button.

ButtonAttribute(ButtonSizes, ButtonStyle)
Creates a button in the inspector named after the method.
public ButtonAttribute(ButtonSizes size, ButtonStyle parameterBtnStyle)
Parameters
ButtonSizes size

The size of the button.

ButtonStyle parameterBtnStyle

Button style for methods with parameters.

ButtonAttribute(ButtonStyle)
Creates a button in the inspector named after the method.
public ButtonAttribute(ButtonStyle parameterBtnStyle)
Parameters
ButtonStyle parameterBtnStyle

Button style for methods with parameters.

ButtonAttribute(SdfIconType)
Creates a button in the inspector with a custom icon.
public ButtonAttribute(SdfIconType icon)
Parameters
SdfIconType icon

The icon to be displayed inside the button.

ButtonAttribute(SdfIconType, IconAlignment)
Creates a button in the inspector with a custom icon.
public ButtonAttribute(SdfIconType icon, IconAlignment iconAlignment)
Parameters
SdfIconType icon

The icon to be displayed inside the button.

IconAlignment iconAlignment

The alignment of the icon that is displayed inside the button.

ButtonAttribute(SdfIconType, String)
Creates a button in the inspector with a custom icon.
public ButtonAttribute(SdfIconType icon, string name)
Parameters
SdfIconType icon

The icon to be displayed inside the button.

System.String name

Custom name for the button.

ButtonAttribute(Int32)
Creates a button in the inspector named after the method.
public ButtonAttribute(int buttonSize)
Parameters
System.Int32 buttonSize

The size of the button.

ButtonAttribute(Int32, ButtonStyle)
Creates a button in the inspector named after the method.
public ButtonAttribute(int buttonSize, ButtonStyle parameterBtnStyle)
Parameters
System.Int32 buttonSize

The size of the button.

ButtonStyle parameterBtnStyle

Button style for methods with parameters.

ButtonAttribute(String)
Creates a button in the inspector with a custom name.
public ButtonAttribute(string name)
Parameters
System.String name

Custom name for the button.

ButtonAttribute(String, ButtonSizes)
Creates a button in the inspector with a custom name.
public ButtonAttribute(string name, ButtonSizes buttonSize)
Parameters
System.String name

Custom name for the button.

ButtonSizes buttonSize

Size of the button.

ButtonAttribute(String, ButtonSizes, ButtonStyle)
Creates a button in the inspector with a custom name.
public ButtonAttribute(string name, ButtonSizes buttonSize, ButtonStyle parameterBtnStyle)
Parameters
System.String name

Custom name for the button.

ButtonSizes buttonSize

Size of the button.

ButtonStyle parameterBtnStyle

Button style for methods with parameters.

ButtonAttribute(String, ButtonStyle)
Creates a button in the inspector with a custom name.
public ButtonAttribute(string name, ButtonStyle parameterBtnStyle)
Parameters
System.String name

Custom name for the button.

ButtonStyle parameterBtnStyle

Button style for methods with parameters.

ButtonAttribute(String, Int32)
Creates a button in the inspector with a custom name.
public ButtonAttribute(string name, int buttonSize)
Parameters
System.String name

Custom name for the button.

System.Int32 buttonSize

Size of the button in pixels.

ButtonAttribute(String, Int32, ButtonStyle)
Creates a button in the inspector with a custom name.
public ButtonAttribute(string name, int buttonSize, ButtonStyle parameterBtnStyle)
Parameters
System.String name

Custom name for the button.

System.Int32 buttonSize

Size of the button in pixels.

ButtonStyle parameterBtnStyle

Button style for methods with parameters.

Fields

DirtyOnClick
Whether the containing object or scene (if there is one) should be marked dirty when the button is clicked. True by default. Note that if this is false, undo for any changes caused by the button click is also disabled, as registering undo events also causes dirtying.
public bool DirtyOnClick
DisplayParameters

Whether to display the button method's parameters (if any) as values in the inspector. True by default.

If this is set to false, the button method will instead be invoked through an ActionResolver or ValueResolver (based on whether it returns a value), giving access to contextual named parameter values like "InspectorProperty property" that can be passed to the button method.

public bool DisplayParameters
Expanded
If the button contains parameters, you can disable the foldout it creates by setting this to true.
public bool Expanded
Icon
The icon to be displayed inside the button.
public SdfIconType Icon
Name
Use this to override the label on the button.
public string Name
Style
The style in which to draw the button.
public ButtonStyle Style

Properties

ButtonAlignment
The alignment of the button represented by a range from 0 to 1 where 0 is the left edge of the available space and 1 is the right edge. ButtonAlignment only has an effect when Stretch is set to false.
public float ButtonAlignment { get; set; }
ButtonHeight
Gets the height of the button. If it's zero or below then use default.
public int ButtonHeight { get; set; }
DrawResult
If the button has a return type, set this to false to not draw the result. Default value is true.
public bool DrawResult { get; set; }
DrawResultIsSet
public bool DrawResultIsSet { get; }
HasDefinedButtonAlignment
public bool HasDefinedButtonAlignment { get; }
HasDefinedButtonHeight
public bool HasDefinedButtonHeight { get; }
HasDefinedButtonIconAlignment
public bool HasDefinedButtonIconAlignment { get; }
HasDefinedIcon
public bool HasDefinedIcon { get; }
HasDefinedStretch
public bool HasDefinedStretch { get; }
IconAlignment
The alignment of the icon that is displayed inside the button.
public IconAlignment IconAlignment { get; set; }
Stretch
Whether the button should stretch to fill all of the available space. Default value is true.
public bool Stretch { get; set; }