11. Creating New Toolbar Items

  09. Extending Power Inspector No Comments

It is possible to add your own toolbar items to the Power Inspector toolbar.

To do this create a new class that derives from ToolbarItem and has the ToolbarItemFor attribute with PowerInspectorToolbar as its target.

Then at the minimum you need to implement the OnRepaint method for drawing your item, and OnActivated method for reacting to being clicked and other methods of activation.

Example

using Sisus;
using Sisus.Attributes;
using UnityEngine;

[ToolbarItemFor(typeof(PowerInspectorToolbar), 40)]
public class MyToolbarItem : ToolbarItem
{
    private bool unfolded;

    /// <inheritdoc/>
    public override float MinWidth
    {
        get
        {
            return 64f;
        }
    }

    /// <inheritdoc/>
    public override float MaxWidth
    {
        get
        {
            return 64f;
        }
    }

    /// <inheritdoc/>
    protected override void Setup()
    {
        // You can setup your item here to make everything ready for drawing
    }

    /// <inheritdoc/>
    public override bool ShouldShow()
    {
        // Only show this button if there are drawers being drawn inside the inspector view
        return inspector.State.drawers.Length > 0;
    }

    /// <inheritdoc/>
    protected override void OnRepaint(Rect itemPosition)
    {
        GUI.Label(itemPosition, unfolded ? "Fold All" : "Unfold All", InspectorPreferences.Styles.ToolbarLabel);
    }

    /// <inheritdoc/>
    protected override bool OnActivated(Event inputEvent, bool isClick)
    {
        unfolded = !unfolded;
        var drawers = inspector.State.drawers;
        drawers.SetUnfolded(unfolded, true);

        // return true to consume the input event
        return true;
    }
}