A visual graph editor facilitating the creation of simple and complex AI behaviors
- A dynamic, user-friendly editor window
- Support for fundamental node transitions
- Easily create custom nodes and transitions
- Quick and easy integration to other
MonoBehaviour
components, enabling dynamic field modifications for nodes and transitions
This repository is installed as a package for Unity.
Open Window
>Package Manager
.- Click
+
. - Select Add Package from git URL.
- Paste
https://github.com/Tramshy/trashy-behavior-graph.git
. - Click Add.
NOTE: To do this you need Git installed on your computer.
- Create a new
BehaviorPanel
scriptable object using the context menu. - Double click scriptable object, or click
Window -> Behavior Graph Editor
and select scriptable object. - Press
Space
, orRight click -> Create Nodes
, to open search window for nodes. Right click
any node for more options, like creating transitions.- Select any node or edge to see values in the inspector to the left.
- Create a new script and inherit
BehaviorData
. - Variables that can be accessed, and used by, behavior graph should be created using the generic
DataField<T>
class. - Add logic as usual.
- Select the desired
BehaviorPanel
and drag in the new class, before clickingLink Behavior Data
. - You can now see all the serialized fields in the blackboard of the graph editor!
- To use fields, select the desired node or edge.
- Click on a variable in the blackboard and then click on the variable you wish to link it to in the inspector.
- You can unlink fields by just clicking them again.
Nodes
- In the context menu under
Behavior Graph
chooseCustom Node Script
. - This creates a script which inherits from the
Node
class. Node
contains a Start, Update and Exit method, use as necessary.Node
also contains an enum,Statuses
, which contain Success, Running and Failure. EachNode
has aCurrentStatus
, there are base transitions which exits a node depending on whether or not the node'sCurrentStatus
is in Success or Failure.- All fields that you wish to serialize to the inspector, that should be linked to the blackboard, should be of the generic type
DataField<T>
, variables that are not of this type will be shown in the inspector and can still be used within the node, but it cannot be linked to the blackboard fields. - Search for your new node in the graph editor and use as you please! Note: If you want your node to be considered a base node by the system, use the
BaseBehaviorElement
attribute in your node.
Transitions
- In the context menu under
Behavior Graph
chooseCustom Transition Script
. - This creates a script which inherits from the
NodeTransitionObject
class. - The
Condition
method will tell the system to switch nodes when it returnstrue
. - You also need to use
DataField<T>
here in the same way as you would forNode
objects. - Transitions can by default only be used once per node; however, if your new transition should be able to be used by the same node multiple times, then use the
AllowMultipleTransition
attribute. - Right click a node and you can find your new transition within the context menu, under the custom section! Note: You can also use the
BaseBehaviorElement
attribute here to get your new transition to show up under the base section.
Triggers
- In the context menu under
Behavior Graph
chooseCustom Trigger Script
. - This creates a script which inherits from the
TriggerTransition
class. - The
Condition
method will tell the system to switch nodes when it returnstrue
. - Triggers are not checked automatically, instead you will have to use the
CallTrigger
method when you want to attempt a switch. This will then switch if theCondition
returns true. - The
BaseBehaviorElement
andAllowMultipleTransition
attributes can be used for Triggers as well.
- Add a
BehaviorTree
component to the game object, also add the class that derivesBehaviorData
if linking between blackboard is used. - Reference your
BehaviorPanel
in thePanel
field of theBehaviorTree
.
Triggers act very similar to regular transitions, the big difference is that triggers will not be checked automatically. This means you will need to get a reference to the triggers you wish to call. For this you can use the GetNode
method in a Panel
and then the GetTrigger
, or GetTriggers
, method in the specific node. When you have your reference just use the CallTrigger
method to attempt a switch.
There are several base triggers that you can use by default. Most of them are self explanitory, but the SimpleTrigger
can be quite unclear what it does without looking in the code. Its Condition
always returns true. This means that whenever you use the CallTrigger
method, it will switch.
This package is licensed under the MIT License. For more information read: LICENSE
.
The tool is surprisingly performant, but it works using a lot of reflection, and cloning of Scriptable Objects
in some cases, which is inherently quite costly.
You can use the ReadOnlyInspector
attribute on the fields you intend to link, this will make it very clear what fields you need to link.
When you try to link integers, floats and similar fields, you may run into an issue that prevents you from clicking the field in the inspector. If you try to click on the field in the inspector, it will instead result in you dragging the value of the field and will not link the fields. To fix this, all you need to do is click at the very edge of the field or use the ReadOnlyInspector
attribute.
Due to the fact that the system uses Scriptable Objects
as nodes and transitions, you may have to use the [NonSerialized]
attribute to avoid Unity from serializing the runtime data and thus overriding your default values for variables. This is not a problem for all fields, but if you notice the issue just use the attribute.