This is obviously an advanced topic, but it is very possible to create a graph completely in code. In the example below, a simple BehaviourTree is created.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | using UnityEngine; using NodeCanvas.Framework; using NodeCanvas.BehaviourTrees; using NodeCanvas.Tasks.Actions; public class Example : MonoBehaviour { void Awake() { //Create the Behaviour Tree var bt = ScriptableObject.CreateInstance<BehaviourTree>(); //Sequencer root var sequencer = bt.AddNode<Sequencer>(); //First ActionNode var action1 = sequencer.AddChild<ActionNode>(); action1.task = Task.Create<Wait>(bt); //Second ActionNode var action2 = sequencer.AddChild<ActionNode>(); action2.task = Task.Create<Wait>(bt); //Start the behaviour tree with a target agent (this in this case), //and an optional parent blackboard (null in this case, so only the local graph blackboard is used. //Update mode determines when the graph updates (can be Manual). bt.StartGraph(this, null, Graph.UpdateMode.NormalUpdate); } } |
© Paradox Notion 2014-2025. All rights reserved.