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; //Attach this component on a gameobject that has a BehaviourTreeOwner already attached. 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); //Assign the BehaviourTree to the owner and enable it var owner = GetComponent<BehaviourTreeOwner>(); owner.StartBehaviour(bt); } } |
© Paradox Notion 2014-2024. All rights reserved.