A very simple behavior tree implementation including a creator and runtime codes
A very simple behavior tree implementation, including:
Basic behavior tree functions in C# but independent of any Unity Engine codes;
A behavior tree can run totally without a GameObject;
A behavior tree creator with Unity Editor in which binary format behavior tree files can be generated;
Types of nodes, node parameters and blackboard data can be easily extended;
Debugging system including logger interfaces and node info fomatters;
The project is created with Unity 2018.3.6f, any other older or newer versions are not tested at the moment;
Create new node classes simply by extending the provided base types like NodeAction
, NodeComposite
, NodeCondition
or NodeDecorator
:
using EasyBehaviorTree;
[System.Serializable]
public class NodeTest : NodeAction
{
// ...
}
Implement or override the methods:
public class NodeTest : NodeAction
{
public override void Cleanup()
{
// your cleanup logic
}
protected override BTState ExecuteTask(float deltaTime)
{
// your execute logic
// ...
return BTState.Success;
}
}
Add NodeParam
attribute to a field of your class if you want it to be exposed and modified in Creator:
public class NodeTest : NodeAction
{
[NodeParam]
private int foo;
[NodeParam]
private float bar;
// ...
}
If your node field has a type that is not included in NodeParamConfig
, you can simply add the type name in NodeParamConfig.asset and click Regenerate
:
Custom icons can be assigned to your node classes if you add a NodeIcon
attribute to the class with the path of icon asset as argument:
[System.Serializable]
[NodeIcon("Assets/icon_for_node_test.png")]
public class NodeTest : NodeAction
{
// ...
}
Create an empty prefab with the same name as your behavior tree;
Create new empty gameobjects under the root of your prefab and a NodeDefine
component should be attached to every gameobject in the hierarchy:
The structure of the prefab will be the the structure of your behavior tree, that means every gameobject with NodeDefine
will be converted to a corresponding behavior tree node;
Modify params of the nodes, change their names, or just add some description text in Unity inspector window;
Save the prefab and right click it, and then select 'CreateTreeAsset' to create a behavior tree asset:
The following is a prefab named BT_Hero
in sample directory:
and the tree information with DefaultFormatter
:
```
root(EasyBehaviorTree.NodeRepeater) {repeatTimes=100,ignoringFailure=False,oncePerTick=True}
-sel(EasyBehaviorTree.NodeSelector) {}
--seq(EasyBehaviorTree.NodeSequence) {}
---hasTar(HasTarget) {}
---sel(EasyBehaviorTree.NodeSelector) {}
----seq(EasyBehaviorTree.NodeSequence) {}
-----inRange(InAttackRange) {}
-----seq(EasyBehaviorTree.NodeSequence) {}
------atk(AttackTarget) {}
------wait(EasyBehaviorTree.NodeActionWait) {time=1}
----move(ApproachTarget) {}
--find(FindTarget) {}
```
Load a binary behavior tree asset with BehaviorTree.LoadBehaviorTree
to create a behavior tree instance:
// string fullPath = ...
BehaviorTree behaviorTree = BehaviorTree.LoadBehaviorTree(fullPath);
Restart
should be called before you tick your behavior tree:
behaviorTree.Restart();
Tick
should be called every frame (or driven by other systems) and the delta time should be passed as argument;
void Update()
{
behaviorTree.Tick(Time.deltaTime);
}
You can listen the events like OnBehaviorTreeCompleted
etc.;
behaviorTree.OnBehaviorTreeCompleted +=
(bt,st) => {
Debug.Log("Tree completed with state = " + st);
};
Run scene 'Sample' for more details:
and hope you enjoy it :smile: