Skip to content
/ UniBT Public

Free GraphView-based Behavior Tree designer for Unity.

License

Notifications You must be signed in to change notification settings

yoshidan/UniBT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UniBT

License: MIT GitHub release Github all releases

Free Behavior Tree designer for Unity.

Motivation

I know some designers like Behavior Designer or Arbor3.
They have many features but not for free. I will provide easily customizable and free designer.

Supported version

  • Unity 2019.4 or later.

Features

  • Supports constructing behavior tree by GraphView.
  • Supports visualizing active node in runtime.
  • Easily add original behaviors(Action,Conditional,Composite).

Get Started

  1. Download and install unity package.
    Also the package can also be imported with Package Manager, by adding the following entry in your Packages/manifest.json:
{
  "dependencies": {
    ...
    "com.github.yoshidan.unibt": "https://github.com/yoshidan/UniBT.git?path=Assets/UniBT/Scripts"
  }
}
  1. Add UniBT.BehaviorTree component for any GameObject.

  2. Open Graph Editor button opens GraphView for Behavior Tree.

  3. Add behaviors and set parameters.

  4. Finally press save button on tool bar of the editor window. (If invalid node found the color of the node become red.)

  5. Run the unity application. you can see node status in the editor window.

    • The red node means that last Update returned Status.Failure`.
    • The green node means that last Update returned Status.Success.
    • The yellow node means that last Update returned Status.Running.
  6. you can save the GameObject with UniBT.BehaviorTree as prefab.

How It Works

  • UnitBT.BehaviorTree updates child nodes in Update timing when the UpdateType is UpdateType.Auto.
  • If you want to update at any time, change UpdateType to UpdateType.Manual and call BehaviorTree.Tick();
  • Only UniBT.BehaviorTree is the MonoBehavior. Each node is just a C# Serializable class.

Core Behavior Node

Name Description
Composite Node It has one or more child nodes and controls which child node to update.
Action Node This is the leaf node. It execute action such as follow player, attack, escape or others you define.
Conditional Node It has one child node and check the condition whether child is updatable. when having no child, Conditional Node is the leaf node like Action Node.

Conditional Node has following parameter.

Name Description
dontReEvaluateOnRunning true: don't re evaluate the condition if the previous status is Status.Running.

Built In Composite Node

I have prepared several built in Composite Node.

Sequence

  • Updates the child nodes in order from the top.
  • Returns failure immediately if the child node returns failure.
  • Returns running immediately and calls the child at the next update timing if the child node returns running.
  • Returns success if all child nodes return success.

Sequence has following parameter.

Name Description
abortOnConditionChanged true: Aborts the running node when a node with a higher priority than the running node becomes infeasible. Specifically, the execution result of Conditional.CanUpdate, which is a descendant of a node with a higher priority than the running node, is used.

Selector

  • Updates the child nodes in order from the top.
  • Returns success immediately if the child node returns success.
  • Returns running immediately and calls the child at the next update timing if the child node returns running.
  • Returns failure if all child nodes return failure.

Selector has following parameter.

Name Description
abortOnConditionChanged true: Aborts the running node when a node with a higher priority than the running node becomes executable. Specifically, the execution result of Conditional.CanUpdate, which is a descendant of a node with a higher priority than the running node, is used.

All

  • Updates all child nodes.
  • Returns running if any child node returns running.
  • Returns failure if any child node returns failure.
  • Otherwise, returns success.

Random

  • The child nodes are elected and executed according to the probability based on the uniform distribution.
  • Select one for each update. However, if the running status is returned during the last update, the node will continue to run.

Rotator

  • Updates the child nodes in order. Unlike Sequencer, one child node is executed by one update instead of executing all child nodes by one update.
  • For example, if there are three child nodes, the first Update will execute the top node, the next Update will execute the second node, and the next Update will execute the third node.
  • The next run will run the top node again.
  • If a child node returns a running state, it exits without executing subsequent child nodes, and the child node continues to run on the next update.

Rotator has following parameter.

Name Description
resetOnAbort It is a flag whether to return the next execution target node from the top when the execution condition of the ancestor Conditional Node changes and the running node is interrupted.

Demo Scene

  • here is the example scene.

Create Behaviors

Create Action

  • Create C# Script and extends UniBT.Action
  • Override OnUpdate and return status(Success/Running/Failure).
  • Override Awake called by UniBT.BehaviorTree.Awake if needed.
  • Override Start called by UniBT.BehaviorTree.Start if needed.
  • Override Abort to reset field or any state when the parent condition changed..
  • Action has Node gameObject field with UniBT.BehaviorTree attached.
  • Private [SerializeField] field and public field can be set on Behavior Tree editor window.
public class Wait : Action
{
    [SerializeField] 
    private float waitTime;

    private float elapsedTime = 0.0f;

    protected override Status OnUpdate()
    {
        elapsedTime += Time.deltaTime;
        if (elapsedTime < waitTime)
        {
            return Status.Running;
        }

        elapsedTime = 0.0f;
        return Status.Success;
    }

    // abort when the parent conditional changed on previous status is running.
    public override void Abort()
    { 
        elapsedTime = 0.0f;
    }
}

Create Conditional

  • Create C# Script and extends UniBT.Conditional
  • Override IsUpdatable and return result(true/false). when IsUpdatable returns update child.
  • Override OnAwake called by UniBT.BehaviorTree.Awake if needed.
  • Override OnStart called by UniBT.BehaviorTree.Start if needed.
  • Conditional Node has gameObject field with UniBT.BehaviorTree attached.
  • Private [SerializeField] field and public field can be set on Behavior Tree editor window.
public class IsHateGt: Conditional
{
    [SerializeField] 
    private int threshold;

    private Enemy enemy;

    protected override void OnAwake()
    {
        enemy = gameObject.GetComponent<Enemy>();
    }

    protected override bool IsUpdatable()
    {
        return enemy.Hate > threshold;
    }
}
  • Conditional Node can be leaf node like Action Node.

  • Conditional Node can be branch node.

Create Composite

  • Create C# Script and extends UniBT.Composite
  • Override OnUpdate and return status(Success/Running/Failure).
  • Override OnAwake called by UniBT.BehaviorTree.Awake if needed.
  • Override OnStart called by UniBT.BehaviorTree.Start if needed.
  • To abort the running node when the condition changed override Abort.
  • Composite Node has gameObject field with UniBT.BehaviorTree attached.
  • Private [SerializeField] field and public field can be set on Behavior Tree editor window.
public class Random : Composite
{
    private NodeBehavior runningNode;

    protected override Status OnUpdate()
    {
        // proceed to update same node when the previous status is running
        if (runningNode != null)
        {
            return HandleStatus(runningNode.Update(), runningNode);
        }

        // update random children
        var result = UnityEngine.Random.Range(0, Children.Count);
        var target = Children[result];
        return HandleStatus(target.Update(), target);
    }

    private Status HandleStatus(Status status, NodeBehavior updated)
    {
        //save running node for next update.
        runningNode = status == Status.Running ? updated : null;
        return status;
    }

    // abort when the parent conditional changed on previous status is running.
    public override void Abort()
    {
        if (runningNode != null)
        {
            runningNode.Abort();
            runningNode = null;
        }
    }
}

About

Free GraphView-based Behavior Tree designer for Unity.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages