Free Behavior Tree designer for Unity.
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.
- Unity 2019.4 or later.
- Supports constructing behavior tree by GraphView.
- Supports visualizing active node in runtime.
- Easily add original behaviors(Action,Conditional,Composite).
- Download and install unity package.
Also the package can also be imported with Package Manager, by adding the following entry in yourPackages/manifest.json
:
{
"dependencies": {
...
"com.github.yoshidan.unibt": "https://github.com/yoshidan/UniBT.git?path=Assets/UniBT/Scripts"
}
}
-
Add behaviors and set parameters.
-
Finally press save button on tool bar of the editor window. (If invalid node found the color of the node become red.)
-
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
returnedStatus.Success
. - The yellow node means that last
Update
returnedStatus.Running
.
- The red node means that last
-
you can save the GameObject with
UniBT.BehaviorTree
as prefab.
UnitBT.BehaviorTree
updates child nodes inUpdate
timing when the UpdateType isUpdateType.Auto
.- If you want to update at any time, change UpdateType to
UpdateType.Manual
and callBehaviorTree.Tick()
; - Only
UniBT.BehaviorTree
is theMonoBehavior
. Each node is just a C# Serializable class.
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 . |
I have prepared several built in Composite Node.
- 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. |
- 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. |
- Updates all child nodes.
- Returns running if any child node returns running.
- Returns failure if any child node returns failure.
- Otherwise, returns success.
- 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.
- 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. |
- here is the example scene.
- Create C# Script and extends
UniBT.Action
- Override
OnUpdate
and return status(Success/Running/Failure). - Override
Awake
called byUniBT.BehaviorTree.Awake
if needed. - Override
Start
called byUniBT.BehaviorTree.Start
if needed. - Override
Abort
to reset field or any state when the parent condition changed.. - Action has Node
gameObject
field withUniBT.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 C# Script and extends
UniBT.Conditional
- Override
IsUpdatable
and return result(true/false). whenIsUpdatable
returns update child. - Override
OnAwake
called byUniBT.BehaviorTree.Awake
if needed. - Override
OnStart
called byUniBT.BehaviorTree.Start
if needed. - Conditional Node has
gameObject
field withUniBT.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 C# Script and extends
UniBT.Composite
- Override
OnUpdate
and return status(Success/Running/Failure). - Override
OnAwake
called byUniBT.BehaviorTree.Awake
if needed. - Override
OnStart
called byUniBT.BehaviorTree.Start
if needed. - To abort the running node when the condition changed override
Abort
. - Composite Node has
gameObject
field withUniBT.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;
}
}
}