> For the complete documentation index, see [llms.txt](https://renownedgames.gitbook.io/ai-tree/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://renownedgames.gitbook.io/ai-tree/api/user-scripting/toolbaritem-attribute.md).

# ToolbarItem attribute

Instruction to create custom item in tracker window toolbars.

In the AI Tree you can easily modify editor windows by using scripting API. In this page we will show how add custom button to behaviour tree window toolbar.

{% hint style="info" %}
We will show you an example of a function that will collect all the necessary windows. Let's move the **Auto Layout** function to the behaviour tree window itself.

Keep in mind that the following code uses editor assemblies and dependencies, if you use them in a runtime build, do not forget to wrap this sections of code with a `UNITY_EDITOR` define.
{% endhint %}

Create a static method in any of your scripts, it can be any class and add \[ToolbarItem] attribute.

```csharp
[ToolbarItem("Auto Layout", typeof(BehaviourTreeWindow), ToolbarItemLayout.Right)]
public static void AutoLayout()
{
    BehaviourTreeWindow window = TrackerWindow.GetOrCreateTracker<BehaviourTreeWindow>();
    BehaviourTreeUtility.AutoLayout(window);
}
```

Let's look at each line in more detail.

***

```csharp
[ToolbarItem("Auto Layout", typeof(BehaviourTreeWindow), ToolbarItemLayout.Right)]
```

1. **"Auto Layout"** - name of item.
2. **typeof(BehaviourTreeWindow)** - the type of toolbar window that we are modifying.
3. **ToolbarItemLayout.Right** - layout side where you want to add item.

***

```csharp
public static void AutoLayout()
```

Method must be static, with/without any of access modifiers and with `void` return type.

***

```csharp
BehaviourTreeWindow window = TrackerWindow.GetOrCreateTracker<BehaviourTreeWindow>();
```

Get the first available behaviour tree window if there is one, otherwise create new one.

***

```csharp
BehaviourTreeUtility.AutoLayout(window);
```

Automatically create new missing tools and dock to previously finded ot created behaviour tree window.

***

Full method one more time and result

```csharp
[ToolbarItem("Auto Layout", typeof(BehaviourTreeWindow), ToolbarItemLayout.Right)]
public static void AutoLayout()
{
    BehaviourTreeWindow window = TrackerWindow.GetOrCreateTracker<BehaviourTreeWindow>();
    BehaviourTreeUtility.AutoLayout(window);
}
```

<figure><img src="https://2203000093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FGOxTM7Aj9oiVaIINrEkE%2Fuploads%2FmxfVpEmHZRIDHyOBpFs4%2Fimage.png?alt=media&amp;token=4eefde47-39e9-440a-9f9c-d15af00f7570" alt="" width="563"><figcaption><p>Custom "Auto Layout" button.</p></figcaption></figure>

***

{% hint style="warning" %}
Keep in mind that shown above is only used as an example. For example, it may incorrectly handle cases when multiple behaviour tree windows are open. Since `GetOrCreateTracker` returns the first window from the set, it does not guarantee focus.

For this cases you can use following code.
{% endhint %}

```csharp
[ToolbarItem("Auto Layout", typeof(BehaviourTreeWindow), ToolbarItemLayout.Right)]
public static void AutoLayout()
{
    foreach(var tracker in TrackerWindow.ActiveTrackers)
    {
        if(tracker is BehaviourTreeWindow window && window == EditorWindow.focusedWindow)
        {
            BehaviourTreeUtility.AutoLayout(window);
            break;
        }
    }
}
```
