给物体添加Behavior Tree组件,点击Open Behavior Designer进行行为树的编辑。
1.编辑器
第1-5个按钮用于选择要编辑的行为树
Referenced Behaviors可以查看当前行为树引用的行为树(可以通过Actions节点下的Behavior Tree Reference引用行为树)
- :删除当前行为树
+ :为当前物体添加一个新的行为树组件
Lock :锁定当前行为树,即使选择其他的游戏物体也不会改变当前的视图
2.生命周期
3.常用API
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using BehaviorDesigner.Runtime.Tasks;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks.Basic.UnityDebug;
[TaskCategory("Custom")]
[TaskDescription("AITest")]
public class AITest : Action {
public SharedString ss;
public string s;
public override void OnStart()
{
//1.同一行为树下的task之间的通信
BehaviorTree bt = GetComponent<BehaviorTree>();
var v = bt.FindTask<Log>();
v.text = "!!";
//2.访问shared变量
Debug.Log(bt.GetVariable("AA"));
bt.SetVariable("AA", ss);
Debug.Log(bt.GetVariable("AA"));
bt.SetVariableValue("AA", s);
Debug.Log(bt.GetVariable("AA"));
//3.访问global变量
Debug.Log(GlobalVariables.Instance.GetVariable("BB"));
GlobalVariables.Instance.SetVariableValue("BB", s);
Debug.Log(GlobalVariables.Instance.GetVariable("BB"));
}
}
4.
a.如何停止AI
直接将BehaviorTree组件禁用掉并不能停止AI。先要将预制的BehaviorTree组件禁用掉,然后将PauseWhenDisable勾选。此时通过脚本enabled行为树即可自由控制行为树的开启和关闭了。
b.如何引用另一棵行为树
在行为树编辑界面上,右键Save XXX,即可将当前行为树保存为一个文件。然后建一个Action节点,选择BehaviorTree Reference即可引用行为树。