1. 克隆源码
https://github.com/Tencent/behaviac/
2. 编译生成BehaviacDesigner.exe
3. 找到并打开BehaviacDesigner.exe(先不急着填弹出的路径workspace 设置框)
4. 新建一个Unity 空工程,并在此处下载behaviac unitypackage
5. Unity中导入该Unitypackage
6. 在Unity 工程Assets中创建Resources/behaviac/exported 文件夹
7. 回到第三步,设置工作路径如下
8. 创建一个行为树命名为Agent,创建一个agent的派生类叫Player(之后会生成一个Player.cs脚本里面包含的待实现的行为接口), 仿照下面完善该行为树,在对应节点上右键,添加断点。最后导出
注意,这个行为树中的两个wait()对调试很关键,其中数字是单位是毫秒,可以让整个树在这里等待一定时间,而这个时间内正好可以用来手动连接服务器进行调试。如果没有这个wait,那么一旦在unity中点击运行,行为树逻辑已经跑完,而BehaviaDesigner都没来得及连接到Unity.
9.查看Unity工程中的文件树
10. Unity创建一个空物体,命名为behaviac, 挂载Player.cs脚本
11. 用下面的Player.cs替换你的Player.cs
// -------------------------------------------------------------------------------
// THIS FILE IS ORIGINALLY GENERATED BY THE DESIGNER.
// YOU ARE ONLY ALLOWED TO MODIFY CODE BETWEEN '///<<< BEGIN' AND '///<<< END'.
// PLEASE MODIFY AND REGENERETE IT IN THE DESIGNER FOR CLASS/MEMBERS/METHODS, ETC.
// -------------------------------------------------------------------------------using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;///<<< BEGIN WRITING YOUR CODE FILE_INIT///<<< END WRITING YOUR CODEpublic class Player : behaviac.Agent
///<<< BEGIN WRITING YOUR CODE Player
///<<< END WRITING YOUR CODE
{
#region 格式化的内容,直接拷贝behaviac.EBTStatus _status = behaviac.EBTStatus.BT_RUNNING;private static string ExportedFilePath{get{string relativePath = "/Resources/behaviac/exported";if (Application.platform == RuntimePlatform.WindowsEditor){return Application.dataPath + relativePath;}else if (Application.platform == RuntimePlatform.WindowsPlayer){return Application.dataPath + relativePath;}else{return "Assets" + relativePath;}}}private bool InitPlayer(){behaviac.Debug.LogWarning("InitPlayer");bool bRet = this.btload("Agent");if (bRet){this.btsetcurrent("Agent");}return bRet;}private bool InitBehavic(){behaviac.Debug.LogWarning("InitBehavic");behaviac.Workspace.Instance.FilePath = ExportedFilePath;behaviac.Workspace.Instance.FileFormat = behaviac.Workspace.EFileFormat.EFF_xml;return true;}void Awake(){InitBehavic();InitPlayer();}void Update(){if (_status == behaviac.EBTStatus.BT_RUNNING){behaviac.Debug.LogWarning("Update");behaviac.Workspace.Instance.Update();//要想能调试,这句必须有_status = this.btexec();}}#endregionpublic void drift(){///<<< BEGIN WRITING YOUR CODE driftUnityEngine.Debug.Log("drift");///<<< END WRITING YOUR CODE}public bool IsInCollision(){
///<<< BEGIN WRITING YOUR CODE IsInCollisionreturn false;
///<<< END WRITING YOUR CODE}public void moveForward(){///<<< BEGIN WRITING YOUR CODE moveForwardUnityEngine.Debug.Log("moveForward");
///<<< END WRITING YOUR CODE}///<<< BEGIN WRITING YOUR CODE CLASS_PART///<<< END WRITING YOUR CODE}///<<< BEGIN WRITING YOUR CODE FILE_UNINIT///<<< END WRITING YOUR CODE
12. 点击Unity 运行,紧接着按照下图步骤在BehaviacDesigner端连接Unity(所有操作默认即可)
13. 最多等待20s, 可以看到BehaviacDesigner端断点被击中(断点从紫色变成黄色)
这里让这个断点重复50次,如果想让断点继续执行下一步,就点击左上角蓝色小三角
tips:
- F9可以直接在节点上打断点,多次按F9就是取消断点
- 代码中IsInCollision() 中return false;换成return true; 行为树逻辑就走了上面drfit() 而不是moveForward(). 可以自己试试。下面截图是我跑出来的效果