I tend to avoid doing things "the unity way" , and end up writing a lot of custom systems. Now the Behavior package is pretty nice, but as usual I decided I wanted to roll my own that is minimalist and fits in perfectly with my existing ecosystem of tools.
After getting the core classes of selectors, sequences, and decorators done, it was time to start building leafs to carry out tasks, and I realized I had a frustrating problem.
I still really wanted a visual graph system to use to design my trees, but after some research, I couldn't find anything suitable. I also really didn't want to spend a weekend writing a node based editor in unity for this, and I certainly didn't want to write JSON by hand. Scriptable objects were out too, the serialization limit really makes them bad at nested data structures like this.
It was really looking like if I wanted a visual graph for creating tree definitions I was going to have to either buy something or build a custom tool.
Then, I had a flash of insight. A GameObject is a pretty perfect tree structure. So all I need was a set of empty game objects
GameObject: GoblinWarriorBehavior Tag: TreeDefinition
Child: Sequence
Child of Sequence: Move to Melee range
Child of Sequence: choose attack
Save as a prefab.
Drop the prefab into the AI mono behavior.
In my behavior tree class, accept a Blackboard and a game object with a tag of TreeDefinition.
In BuildTree, send each Gameobject name to BehaviorNodeFactory to the runtime behavior node.
Then I added a simple editor script that validated the name of the game object is valid for the factory to eliminate typos.
Lastly I can add a component to any Gameobject in the TreeDefinition that acts as a decorator. For example, if I want a repeater node to repeat exactly 5 times , just check for a decorator component attached to the prefab .
The upside of this approach.
Super light weight
Single BehaviorTree class.
Building behavior graphs is as easy as using the unity editor and building a prefab.
Custom editor script prevents typos and provides warnings if a name isn't present in the factory.
No scriptable objects.
No JSON
No third party tools , libraries, or paided assets
No weekend lost to building authoring tools.
No scrolling around graphs and connecting nodes.
If anyone is interested, I can post some code examples.