BehaviourTreeEditor for Unity

repository·main·Indexed 43 days ago

https://github.com/thekiwicoder0/unitybehaviourtreeeditor

A Unity package (v0.4.0) providing a visual editor built with UI Toolkit for creating and managing Behaviour Trees. It allows developers to design AI logic using Action, Composite, and Decorator nodes, featuring a centralized Blackboard for data sharing and runtime debugging with color-coded state visualization. Supports custom node creation via C# inheritance from ActionNode, CompositeNode, and DecoratorNode.

Tokens
2.4K
Snippets
4
Records
15
Agent score
6%

What's inside BehaviourTreeEditor

  1. How Behaviour Tree nodes work

    main

    A behaviour tree executes from a root node and traverses down through its children. Each node returns one of three states:

    • success
    • failure
    • running

    When a child returns running, the tree traversal typically stops for the current frame, and the state bubbles back up to the root. Parent nodes interpret these return states to decide the next course of action.

    Nodes are categorized into three main types:

    1. Action Nodes: Leaf nodes with no children. They represent the agent's capabilities (e.g., MoveToPosition, Wait).
    2. Composite Nodes: Control flow nodes that can have multiple children (e.g., Sequencer, Selector).
    3. Decorator Nodes: Nodes with exactly one child used to modify or hide the child's return state (e.g., Inverter, Repeat).
  2. How the Blackboard works for data sharing

    main

    The Blackboard is a centralized data store that allows Nodes to pass information to each other across the behaviour tree. For example, one node can write a target position to the blackboard, and a subsequent node can read that position to move to it.

    Managing Blackboard Keys

    • Adding Keys: In the Blackboard view, enter a unique name in the Name field and select a type from the Type dropdown.
    • Deleting Keys: Right-click on a node's value in the Blackboard view and select delete.
    • Blackboard Key Overrides: You can override specific key values for individual GameObjects. Select the GameObject containing a BehaviourTreeInstance component, add a new override, select the desired key from the dropdown, and enter the override value.
  3. Configure node properties in the Inspector

    main

    The Node Inspector View displays all public properties of the currently selected node.

    Tips:

    • To ensure properties are displayed, select the middle of the node (over the node's title).
    • All nodes include a description field. You can set this in the inspector to provide context that appears under the node's title in the tree.
  4. Install BehaviourTreeEditor via Unity Package Manager

    main

    To install the BehaviourTreeEditor, use the Unity Package Manager to add the package from a git URL.

    Requirements:

    • Minimum Unity version: 2023.2
    • A working installation of git (if you encounter the error No 'git' executable was found, install it from https://git-scm.com/download/win).

    Steps:

    1. In Unity, open the Package Manager.
    2. Select the Add Package From git URL... command.
    3. Paste the following URL: https://github.com/thekiwicoder0/UnityBehaviourTreeEditor.git
    https://github.com/thekiwicoder0/UnityBehaviourTreeEditor.git
  5. Debug behaviour trees during runtime

    main

    While in Play Mode, you can select a GameObject to analyze its current behaviour tree state via color coding in the editor:

    • Red: The node failed.
    • Green: The node succeeded.
    • Yellow: The node is currently running.
    • No Highlight: The node was aborted and returned to its default state.
  6. Access and use the Behaviour Tree Editor

    main

    The editor interface can be opened via the Unity menu: Window -> AI -> BehaviourTree...

    Main Panels:

    • TreeView: Used for creating and organizing nodes.
    • Inspector View: Displays public properties of the selected node.
    • Blackboard View: Manages shared data used by nodes.
    • Assets Menu: Accesses project assets.
  7. Navigate and manipulate the Tree View

    main

    The TreeView canvas allows for visual organization of your logic.

    Node Operations:

    • Adding Nodes: Right-click on the canvas and select from the Actions, Composites, or Decorators sub-menus. You can type to filter nodes.
    • Linking Nodes: Drag the output of a parent node to the input of a child node. Note: Only CompositeNode types can have multiple children.
    • Selection: Drag nodes to move them. Use box selection for multiple nodes. Double-click a parent node to select its entire subtree.

    Keyboard Shortcuts:

    KeyCodeAction
    DELDeletes the currently selected nodes
    AFrames all nodes on the canvas
    OFrames the canvas origin
    [Frames the parent node of the current selection
    ]Frames the child node of the current selection
    CTRLSelects the sub hierarchy of this node
    CTRL + DDuplicates the selected nodes
    CTRL + CCopys the selected nodes
    CTRL + VPastes the selected nodes (must deselect first)
    Left ClickSelect Node
    Right ClickCreate Node Context menu
  8. Use NodeProperty<T> to read/write blackboard values from Nodes

    main

    To interact with the blackboard from within a custom Node, add a public NodeProperty<T> variable to your class. Use the .Value property to perform read and write operations.

    In the Node Inspector, you can then select the specific Blackboard key from a dropdown. Alternatively, you can select [Value] to set a fixed default value directly on the node, bypassing the blackboard for that specific parameter.

    [System.Serializable]
    public class TestNode : ActionNode
    {
        public NodeProperty<int> myInt;
    
        ... 
    
        protected override State OnUpdate() {
            int value = myInt.Value;
            Debug.Log("Value:" + value);
            myInt.Value = value + 1;
            return State.Success;
        }
    }
  9. Create custom node scripts

    main

    To extend the editor, you can create custom nodes by inheriting from one of the three base C# classes:

    • ActionNode
    • CompositeNode
    • DecoratorNode

    Methods of creation:

    1. Manual: Write a standard C# script inheriting from one of the base types.
    2. Template: Right-click in the TreeView canvas and use the context menu to generate a new script from a template. Templates are located at: Assets/TheKiwiCoder/BehaviourTree/ScriptTemplates.
  10. Declaring a new BlackboardKey type

    main

    The Blackboard automatically includes all common Unity types. To register a custom type so it appears in the Blackboard Type dropdown, create a class that inherits from BlackboardKey<T>.

    [System.Serializable]
    public class MyTypeKey : BlackboardKey<MyType> {
    
    }
    [System.Serializable]
    public class MyTypeKey : BlackboardKey<MyType> {
    
    }