# ToolbarItem attribute

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="/files/1etgHFJ6an1dfsX3xXwV" 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;
        }
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://renownedgames.gitbook.io/ai-tree/api/user-scripting/toolbaritem-attribute.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
