Behave1.4行為樹插件
下載地址:http://pan.baidu.com/s/1i4uuX0L
安裝插件和使用
我們先來看看插件的安裝和基本使用方法,新建一個Unity3D項目,這里我使用的是5.0以上的版本:
導入插件
第一步當然是導入下載好的插件包了,操作略過...
創建資源
首先我們創建一個名為BT的文件夾,點擊Project面板的create來創建相應的資源:
我們會在Asset目錄下生成一個NewBehaveLibrary0.asset的文件,我們重命名為MyBehaveLibrary后將其拖入新建的BT文件夾中即可。
編輯資源
我們選中創建的資源,在Inspector中可以看到如下的界面:
我們點擊Edit library后會彈出兩個窗口,需要注意的是有一個窗口被另一個完全覆蓋了,拖拽一下就可以看到了。點擊behave browse面板的create創建一個collection,再點擊一次create創建一個tree我們就有了一個行為樹了。
接下來我們選中新創建的NewTree1在behave editor面板就可以編輯這個行為樹了。
我們將面板下方的Action拖拽到面板中並在Inspector修改該節點的名稱為MyAction:
接下來我們在頂部中間的方塊拉出一條線,連線到我們的MyAction頂上,這樣就把這個節點添加到該行為樹上了。
好了到目前為止,我們的編輯就算是完成了。
編譯資源
到目前為止,我們的資源還不能在項目中直接使用,我們需要先對其進行編譯,選中我們的library,在Inspector中會出現選項:
我們點擊一下Build library debug就可以編譯出在項目中可以使用的資源了,如下:
編輯代碼
編輯完成后,接下來我們該怎么使用行為樹呢?看官們請接着看。
首先我們創建一個類Demo1Agent:
1 using Behave.Runtime; 2 using UnityEngine; 3 using System.Collections; 4 using Tree = Behave.Runtime.Tree; 5 6 public class Demo1Agent : IAgent { 7 8 public void Reset (Tree sender) { 9 10 } 11 12 public int SelectTopPriority (Tree sender, params int[] IDs) { 13 return 0; 14 } 15 16 public BehaveResult Tick (Tree sender, bool init) { 17 Debug.Log("調用系統的Tick方法"); 18 return BehaveResult.Success; 19 } 20 21 public BehaveResult TickMyActionAction(Tree sender) { 22 Debug.Log("我的action"); 23 return BehaveResult.Success; 24 } 25 }
我們創建了一個實現了IAgent的類,這個類會和一個行為樹進行綁定,這里需要注意的是Tick和TickMyActionAction方法,在一個行為樹中,同一時刻只有一個節點會執行,而我們的“MyAction”執行時,會尋找TickMyActionAction方法進行調用,這里的方法命名有一個規則:“Tick”+節點名稱+“Action”,如果行為樹找到了對應當前節點名稱的方法就會進行調用,否則會調用Tick方法。
接下來我們創建一個類Demo1:
1 using UnityEngine; 2 using System.Collections; 3 using Behave.Runtime; 4 using Tree = Behave.Runtime.Tree; 5 6 public class Demo1 : MonoBehaviour { 7 8 Tree m_Tree; 9 Demo1Agent agent; 10 11 void Start () { 12 agent = new Demo1Agent (); 13 m_Tree = BLMyBehaveLibrary.InstantiateTree(BLMyBehaveLibrary.TreeType.NewCollection1_NewTree1, agent); 14 } 15 16 void Update () { 17 m_Tree.Tick(); 18 } 19 }
這個類就比較簡單了,我們通過BLMyBehaveLibrary創建一個和Demo1Agent的實例綁定的行為樹,其中BLMyBehaveLibrary和NewCollection1_NewTree1都是我們之前編輯好編譯生成的行為樹代碼,每幀調用行為樹的Tick方法即可。
調試行為樹
我們在場景中添加一個GameObject並綁定Demo1即可,運行就可以看到“我的Action”的輸出了。
節點詳解
目前behave提供了下面6個節點,我們來具體的看看:
Action
動作節點,執行核心邏輯的節點,該節點我們之前已經使用過了,主要用來實現我們需要的邏輯功能。
這里主要說一下Tick方法的3個返回值:
1 namespace Behave.Runtime 2 { 3 public enum BehaveResult 4 { 5 Running, 6 Success, 7 Failure 8 } 9 }
- Running表示當前節點需要繼續運行,下一次Tick時仍然會執行該節點。
- Success表示節點執行完畢,而且執行成功。
- Failure表示節點執行完畢,而且執行失敗。
Decorator
裝飾節點只會有一個子節點,作用為通過判斷來控制是否執行其子節點,我們來看看裝飾節點的使用方法:
首先我們創建一個新的行為樹,並添加一個裝飾節點和一個動作節點:
我們新建一個Demo2Agent:
1 using Behave.Runtime; 2 using UnityEngine; 3 using System.Collections; 4 using Tree = Behave.Runtime.Tree; 5 6 public class Demo2Agent : IAgent { 7 8 private bool _shoulDo; 9 10 public void Reset (Tree sender) { 11 12 } 13 14 public int SelectTopPriority (Tree sender, params int[] IDs) { 15 return 0; 16 } 17 18 public BehaveResult Tick (Tree sender, bool init) { 19 Debug.Log("調用系統的Tick方法"); 20 return BehaveResult.Success; 21 } 22 23 public BehaveResult TickMyDecoratorDecorator(Tree sender) { 24 _shoulDo = !_shoulDo; 25 if (_shoulDo) { 26 Debug.Log("執行子節點"); 27 return BehaveResult.Success; 28 } else { 29 Debug.Log("不執行子節點"); 30 return BehaveResult.Failure; 31 } 32 } 33 34 public BehaveResult TickMyActionAction(Tree sender) { 35 Debug.Log("我的action"); 36 return BehaveResult.Success; 37 } 38 }
這里要注意的是,裝飾節點的命名為:“Tick”+節點名稱+“Decorator”,不是Action了。
接下來編譯我們的行為樹,添加一個類Demo2:
1 using UnityEngine; 2 using System.Collections; 3 using Behave.Runtime; 4 using Tree = Behave.Runtime.Tree; 5 6 public class Demo2 : MonoBehaviour { 7 8 Tree m_Tree; 9 Demo2Agent agent; 10 11 void Start () { 12 agent = new Demo2Agent (); 13 m_Tree = BLMyBehaveLibrary.InstantiateTree(BLMyBehaveLibrary.TreeType.NewCollection1_NewTree2, agent); 14 } 15 16 void Update () { 17 m_Tree.Tick(); 18 } 19 }
添加到場景就可以查看效果了。
Sequence
順序節點會讓連接它的子節點從左到右依次執行, 每個節點從底部的線決定了順序,擺放的位置無關緊要,如果第一個子節點返回失敗,則整個節點返回失敗。如果該子節點返回成功,則會自動往右邊一個子節點執行,如果該節點返回runing,則會重新開始運行。
我們創建一個新的行為樹,如下:
我們新建一個Demo3Agent:
1 using Behave.Runtime; 2 using UnityEngine; 3 using System.Collections; 4 using Tree = Behave.Runtime.Tree; 5 6 public class Demo3Agent : IAgent { 7 8 public GameObject cube; 9 public float time; 10 11 float nowTime = 0; 12 13 public void Reset (Tree sender) { 14 15 } 16 17 public int SelectTopPriority (Tree sender, params int[] IDs) { 18 return 0; 19 } 20 21 public BehaveResult Tick (Tree sender, bool init) { 22 Debug.Log("調用系統的Tick方法"); 23 return BehaveResult.Success; 24 } 25 26 public BehaveResult TickMyAction1Action(Tree sender) { 27 Debug.Log("我的action1"); 28 if (cube.transform.position.x >= 3.0f) { 29 cube.transform.position = new Vector3(3.0f, 0, 0); 30 return BehaveResult.Success; 31 } 32 cube.transform.Translate(new Vector3(2.0f * time, 0, 0)); 33 return BehaveResult.Running; 34 } 35 36 public BehaveResult TickMyAction2Action(Tree sender) { 37 Debug.Log("我的action2"); 38 if (nowTime >= 1.0f) { 39 nowTime = 0; 40 cube.transform.localEulerAngles = new Vector3(0, 0, 0); 41 return BehaveResult.Success; 42 } 43 cube.transform.Rotate(new Vector3(360.0f * time, 0, 0)); 44 nowTime += time; 45 return BehaveResult.Running; 46 } 47 48 public BehaveResult TickMyAction3Action(Tree sender) { 49 Debug.Log("我的action3"); 50 if (cube.transform.position.x <= 0) { 51 cube.transform.position = new Vector3(0, 0, 0); 52 return BehaveResult.Success; 53 } 54 cube.transform.Translate(new Vector3(-2.0f * time, 0, 0)); 55 return BehaveResult.Running; 56 } 57 }
我們再創建一個Demo3:
1 using UnityEngine; 2 using System.Collections; 3 using Behave.Runtime; 4 using Tree = Behave.Runtime.Tree; 5 6 public class Demo3 : MonoBehaviour { 7 8 public GameObject cube; 9 10 Tree m_Tree; 11 Demo3Agent agent; 12 13 void Start () { 14 agent = new Demo3Agent (); 15 agent.cube = cube; 16 m_Tree = BLMyBehaveLibrary.InstantiateTree(BLMyBehaveLibrary.TreeType.NewCollection1_NewTree3, agent); 17 } 18 19 void Update () { 20 agent.time = Time.deltaTime; 21 m_Tree.Tick(); 22 } 23 }
我們在場景里添加一個Cube,把Demo3拖入並綁定Cube就可以查看效果了;
這里的效果是Cube先向前移動,然后旋轉一下再向后移動,如此反復。
Selector
選擇節點子節點會從左到右依次執行,和順序節點不同的是,選擇節點的子節點返回成功,則整個節點返回成功,如果子節點返回失敗,則運行右邊的一個子節點,並繼續運行,一直到運行到子節點的末尾一個,則返回失敗,下一次選擇節點再被觸發的時候,又從最左邊 第一個節點開始運行。
我們創建一個新的行為樹,如下:
我們新建一個Demo4Agent:
1 using Behave.Runtime; 2 using UnityEngine; 3 using System.Collections; 4 using Tree = Behave.Runtime.Tree; 5 6 public class Demo4Agent : IAgent { 7 8 public GameObject cube; 9 public float time; 10 11 float nowTime = 0; 12 13 public void Reset (Tree sender) { 14 15 } 16 17 public int SelectTopPriority (Tree sender, params int[] IDs) { 18 return 0; 19 } 20 21 public BehaveResult Tick (Tree sender, bool init) { 22 Debug.Log("調用系統的Tick方法"); 23 return BehaveResult.Success; 24 } 25 26 public BehaveResult TickMyAction1Action(Tree sender) { 27 Debug.Log("我的action1"); 28 if (nowTime >= 1.0f) { 29 nowTime = 0; 30 cube.transform.position = new Vector3(0, 0, 0); 31 //失敗則執行下一個節點 32 if (Random.value > 0.5f) { 33 return BehaveResult.Failure; 34 } 35 return BehaveResult.Success; 36 } 37 cube.transform.Translate(new Vector3(2.0f * time, 0, 0)); 38 nowTime += time; 39 return BehaveResult.Running; 40 } 41 42 public BehaveResult TickMyAction2Action(Tree sender) { 43 Debug.Log("我的action2"); 44 if (nowTime >= 1.0f) { 45 nowTime = 0; 46 cube.transform.localEulerAngles = new Vector3(0, 0, 0); 47 //失敗則執行下一個節點 48 if (Random.value > 0.5f) { 49 return BehaveResult.Failure; 50 } 51 return BehaveResult.Success; 52 } 53 cube.transform.Rotate(new Vector3(360.0f * time, 0, 0)); 54 nowTime += time; 55 return BehaveResult.Running; 56 } 57 58 public BehaveResult TickMyAction3Action(Tree sender) { 59 Debug.Log("我的action3"); 60 if (nowTime >= 1.0f) { 61 nowTime = 0; 62 cube.transform.localScale = new Vector3(1, 1, 1); 63 //失敗則執行下一個節點 64 if (Random.value > 0.5f) { 65 return BehaveResult.Failure; 66 } 67 return BehaveResult.Success; 68 } 69 cube.transform.localScale = new Vector3(nowTime, nowTime, nowTime); 70 nowTime += time; 71 return BehaveResult.Running; 72 } 73 }
我們再創建一個Demo4:
1 using UnityEngine; 2 using System.Collections; 3 using Behave.Runtime; 4 using Tree = Behave.Runtime.Tree; 5 6 public class Demo4 : MonoBehaviour { 7 8 public GameObject cube; 9 10 Tree m_Tree; 11 Demo4Agent agent; 12 13 void Start () { 14 agent = new Demo4Agent (); 15 agent.cube = cube; 16 m_Tree = BLMyBehaveLibrary.InstantiateTree(BLMyBehaveLibrary.TreeType.NewCollection1_NewTree4, agent); 17 } 18 19 void Update () { 20 agent.time = Time.deltaTime; 21 m_Tree.Tick(); 22 } 23 }
我們的小正方形會先運行Action1,然后有50%的機會進入Action2,進入Action2后又有50%的機會進入Action3。
Parallel
並行節點會從左到右觸發它所有的子節點工作,對於並行節點有兩個重要的設置,一個是子節點完成,一個是組件完成,子節點完成參數決定了子節點的返回值該如何處理:
- 如果並行節點設置為成功或者失敗,那么無論它返回的是成功還是失敗,子節點的輸出都標記為完成。
- 如果並行節點設置為成功,那么子節點的輸出只在返回成功時候才標記為完成。在觸發所有的子節點工作后,一個子節點返回失敗就會讓整個並行組件返回失敗。
- 並行節點設置為失敗的話也會同理,子節點只有在返回完成時,它才會返回失敗。
我們創建一個新的行為樹,如下:
選擇並行節點可以看到屬性:
這里的意思是:所有的子節點都返回成功則退出該節點。
我們新建一個Demo5Agent:
1 using Behave.Runtime; 2 using UnityEngine; 3 using System.Collections; 4 using Tree = Behave.Runtime.Tree; 5 6 public class Demo5Agent : IAgent { 7 8 public GameObject cube; 9 public float time; 10 11 float nowTime = 0; 12 13 public void Reset (Tree sender) { 14 15 } 16 17 public int SelectTopPriority (Tree sender, params int[] IDs) { 18 return 0; 19 } 20 21 public BehaveResult Tick (Tree sender, bool init) { 22 Debug.Log("調用系統的Tick方法"); 23 return BehaveResult.Success; 24 } 25 26 public BehaveResult TickMyAction1Action(Tree sender) { 27 Debug.Log("我的action1"); 28 if (cube.transform.position.x >= 3.0f) { 29 cube.transform.position = new Vector3(3.0f, 0, 0); 30 return BehaveResult.Success; 31 } 32 cube.transform.Translate(new Vector3(2.0f * time, 0, 0)); 33 return BehaveResult.Running; 34 } 35 36 public BehaveResult TickMyAction2Action(Tree sender) { 37 Debug.Log("我的action2"); 38 if (nowTime >= 1.0f) { 39 nowTime = 0; 40 cube.transform.localEulerAngles = new Vector3(0, 0, 0); 41 return BehaveResult.Success; 42 } 43 cube.transform.Rotate(new Vector3(360.0f * time, 0, 0)); 44 nowTime += time; 45 return BehaveResult.Running; 46 } 47 48 public BehaveResult TickMyAction3Action(Tree sender) { 49 Debug.Log("我的action3"); 50 if (cube.transform.position.x <= 0) { 51 cube.transform.position = new Vector3(0, 0, 0); 52 return BehaveResult.Success; 53 } 54 cube.transform.Translate(new Vector3(-2.0f * time, 0, 0)); 55 return BehaveResult.Running; 56 } 57 }
我們再創建一個Demo5:
1 using UnityEngine; 2 using System.Collections; 3 using Behave.Runtime; 4 using Tree = Behave.Runtime.Tree; 5 6 public class Demo5 : MonoBehaviour { 7 8 public GameObject cube; 9 10 Tree m_Tree; 11 Demo5Agent agent; 12 13 void Start () { 14 agent = new Demo5Agent (); 15 agent.cube = cube; 16 m_Tree = BLMyBehaveLibrary.InstantiateTree(BLMyBehaveLibrary.TreeType.NewCollection1_NewTree5, agent); 17 } 18 19 void Update () { 20 agent.time = Time.deltaTime; 21 m_Tree.Tick(); 22 } 23 }
運行游戲:會看見小正方形會旋轉着前進,因為並行是所有子節點每幀都會被調用到,當兩個子節點都返回成功時退出該節點。小正方形恢復到原點。