AceLand Unity Packages
  • Home
  • Getting Started
    • Installation
    • Project Settings
    • Architecture Graph
    • Development Level
  • Tutorial
    • Create Your Package
    • Create Project Settings
  • Packages
    • Library
      • Change Log
      • Editor Tools
      • Mono
        • Follow Object
        • Singleton
      • Attributes
        • Conditional Show
        • Inspector Button
        • ReadOnly Field
      • Build Leveling
      • CVS
      • DataTools
      • Disposable Object
      • Extensions
      • Json
      • Kalman Filter
      • Optional
      • Project Setting
      • Serialization Surrogate
      • Utils
    • Event Driven
      • Change Log
      • Event Bus
      • Signal
    • Input
      • Change Log
    • Memento Service
      • Change Log
    • Node Framework
      • Change Log
      • Mono Node
    • Node For Mono (dev)
    • Player Loop Hack
      • Change Log
    • Pool
      • Change Log
    • Security (dev)
    • States
      • Change Log
    • Task Utils
      • Change Log
    • WebRequest
      • Change Log
Powered by GitBook
On this page
  1. Packages
  2. Library
  3. Attributes

Inspector Button

Adding instance button to the inspector

Inspector Button is not created by us. I got this function around 2019 from somewhere and I can't find the source now. I had made a little bit arrangement on it.

[InspectorButton("Do Stuffs")]
private void InspectorButton()
{
    // do wonderful stuffs
}

[InspectorButton(Expanded = true)]
private void InspectorButtonWithValue(int value)
{
    // do wonderful stuffs with value
}

The usage can be changed by adding parameters.

// enable or disable (default: DisableInPlayMode)
[InspectorButton(Mode = InspectorButtonMode.DisabledInPlayMode)]
[InspectorButton(Mode = InspectorButtonMode.EnabledInPlayMode)]
[InspectorButton(Mode = InspectorButtonMode.AlwaysEnabled)]

// set button name (default: function name)
[InspectorButton(name: "My faviourite name")]

// expanded (default: false), work on function with parameter
[InspectorButton(Expanded = true)]

If you are modifying serialized values, please add undo data to Unity Editor, and set your object dirty for serializing.

[InspectorButton]
private void ModifyPosition()
{
    #if UNITY_EDITOR
    UnityEditor.Undo.RecordObject(this, "Modify Position");
    UnityEditor.Undo.RecordObject(transform, "Modify Position");
    #endif
    
    transform.position = Vector3.one;
    
    #if UNITY_EDITOR
    UnityEditor.EditorUtility.SetDirty(this);
    #endif
}

Last updated 1 month ago