Creating Behavior Tree At EditorTime using code

mrtokyo

New member
Hi,

I've looked at the previous examples of creating behavior tree at runtime, but my usecase is to be able to create an ExternalBehaviorTree asset at editorTime and generate the tree using code at editor time.

The Tree doesn't seem to save after using code similar to what you have shown in https://opsive.com/forum/index.php?threads/how-to-build-a-behaviourtree-at-runtime.1477/#post-7178 this post, probably since it's not happening at runtime. I also probably need to save the graph data somehow, so similar to how you save the graph using the editor tool i suppose

Is there some documentation i can refer to on creating it at editor time. My usecase is to generate a behavior tree based by parsing external data like that on an excel sheet, and have that behavior tree saved inside of unity as an ExternalBehaviorTree asset
 
Last edited:
Okay, found out how to do it. Below is an example for anyone who is interested in doing the same thing. Just make sure your script is inside of Editor/ folder and you will be able to import BehaviorDesigner.Editor

using BehaviorDesigner.Runtime; using BehaviorDesigner.Editor; using BehaviorDesigner.Runtime.Tasks; using Sirenix.OdinInspector; using UnityEditor; using UnityEngine; [CreateAssetMenu(fileName = "Ability", menuName = "Scriptable Objects/AutomateBtScript", order = 1)] public class AutomateBtScript : ScriptableObject { [Button] public void CreateBehaviorTree() { var behaviorTree = ScriptableObject.CreateInstance<ExternalBehaviorTree>(); string path = AssetDatabase.GetAssetPath(this).Replace(".asset", "Behavior.asset"); AssetDatabase.CreateAsset(behaviorTree, path); AssetDatabase.SaveAssets(); } [Button] public void UpdateTree() { string path = AssetDatabase.GetAssetPath(this).Replace(".asset", "Behavior.asset"); ExternalBehaviorTree loadedTree = AssetDatabase.LoadAssetAtPath<ExternalBehaviorTree>(path); EntryTask entryTask = new EntryTask(); entryTask.NodeData = new NodeData(); entryTask.NodeData.Offset = new Vector2(301, 202); entryTask.ID = 1; loadedTree.BehaviorSource.EntryTask = entryTask; Sequence sequence = new Sequence(); sequence.ID = 2; sequence.NodeData = new NodeData(); sequence.NodeData.Offset = new Vector2(1,122); Idle idle = new Idle(); idle.NodeData = new NodeData(); idle.NodeData.Offset = new Vector2(1,200); idle.ID = 3; sequence.AddChild(idle,0); loadedTree.GetBehaviorSource().RootTask = sequence; JSONSerialization.Save(loadedTree.GetBehaviorSource()); AssetDatabase.SaveAssets(); } }
So this is a simple way to do it. Mostly I was missing the node data, and the ExternalBehaviorTree has to exist otherwise it doesn't have the BehaviorSource, (which you can always new up and store in the tree anyway)
 
Top