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.
Create a static method in any of your scripts, it can be any class and add [ToolbarItem] attribute.
[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.
[ToolbarItem("Auto Layout", typeof(BehaviourTreeWindow), ToolbarItemLayout.Right)]
"Auto Layout" - name of item.
typeof(BehaviourTreeWindow) - the type of toolbar window that we are modifying.
ToolbarItemLayout.Right - layout side where you want to add item.
public static void AutoLayout()
Method must be static, with/without any of access modifiers and with void
return type.
BehaviourTreeWindow window = TrackerWindow.GetOrCreateTracker<BehaviourTreeWindow>();
Get the first available behaviour tree window if there is one, otherwise create new one.
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
[ToolbarItem("Auto Layout", typeof(BehaviourTreeWindow), ToolbarItemLayout.Right)]
public static void AutoLayout()
{
BehaviourTreeWindow window = TrackerWindow.GetOrCreateTracker<BehaviourTreeWindow>();
BehaviourTreeUtility.AutoLayout(window);
}

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.
[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;
}
}
}
Last updated