public void DrawDebugView() { if (BehaviorManager.GetInstance().CurBehaviorTree == null) { return; } if (GUILayout.Button($"清空执行记录")) { BehaviorManager.treePathList.Clear(); BehaviorManager.GetInstance().ClearDebugState(); } float offset = 55f; GUILayout.BeginArea(new Rect(0f, 20f, this.mWidth, Screen.height - offset)); this.mTreeScrollPos = GUI.BeginScrollView(new Rect(0f, 0f, this.mWidth, Screen.height - offset), this.mTreeScrollPos, new Rect(0f, 0f, this.mWidth - 20f, BehaviorManager.treePathList.Count * 22), false, false); for (int i = 0; i < BehaviorManager.treePathList.Count; i++) { if (GUILayout.Button($"frame{i}")) { BehaviorManager.GetInstance().ClearDebugState(); BehaviorManager.GetInstance().SetDebugState(BehaviorManager.GetInstance().CurBehaviorTree, BehaviorManager.treePathList[i]); } } GUI.EndScrollView(); GUILayout.EndArea(); }
private void AddNodeMenuCallback(object obj) { string nodeType = (string)obj; var nodeProto = BehaviorManager.GetInstance().GetNodeTypeProto(nodeType); var nodeData = BehaviorManager.GetInstance().CreateNode((int)BehaviorManager.GetInstance().CurTree.Id, nodeProto.name); var node = CreateNode(nodeData, MousePosToGraphPos(mMousePos)); }
//有待优化 private void ChangeNodeType(object obj) { string nodeType = (string)obj; var nodeProto = BehaviorManager.GetInstance().GetNodeTypeProto(nodeType); var nodeData = BehaviorManager.GetInstance().CreateNode((int)BehaviorManager.GetInstance().CurTree.Id, nodeProto.name); var oldNode = mSelectedNode; var newNode = new NodeDesigner(nodeData); if (oldNode == RootNode) { newNode.NodeData.nodeId = RootNode.NodeData.nodeId; RootNode = newNode; var oldTree = BehaviorManager.GetInstance().CurTree; var newTree = new BehaviorTreeData(oldTree.Id); newTree.classify = oldTree.classify; newTree.Root = nodeData; BehaviorManager.GetInstance().CurTree = newTree; } else { int idx = oldNode.Parent.Children.IndexOf(oldNode); oldNode.Parent.AddChild(newNode, idx); oldNode.Parent.RemoveChild(oldNode); } foreach (var child in oldNode.Children) { newNode.AddChild(child); } BehaviorManager.GetInstance().ResetTreeId(); Game.Scene.GetComponent <EventComponent>().Run(EventIdType.BehaviorTreeAfterChangeNodeType); }
public void Init() { NodeData.Proto = BehaviorManager.GetInstance().GetNodeTypeProto(NodeData.name); string[] arr = NodeData.Proto.style.Split('/'); if (arr.Length > 1 && arr[1] == "value") { mShowValue = true; } string style = arr.Length > 0? arr[0] : ""; if (style == "") { switch (NodeData.Classify) { case "装饰节点": style = "green"; break; case "复合节点": style = "blue"; break; case "条件节点": style = "orange"; break; } switch (NodeData.name) { case "Sequence": style = "blue"; break; case "Selector": style = "green"; break; default: style = "default"; break; } } mBoxTex = BehaviorDesignerUtility.GetTexture(style); mBoxHighLight = BehaviorDesignerUtility.GetTexture("HighLight"); mBoxSelectHighLight = BehaviorDesignerUtility.GetTexture("SelectHighLight"); if (mBoxTex == null) { mBoxTex = BehaviorDesignerUtility.GetTexture("default"); } if (mBoxTex == null) { Log.Info("mBoxTex null " + NodeData.nodeId); } Width = mBoxTex.width / 2; Height = mBoxTex.height / 2; mLeftConnectTex = BehaviorDesignerUtility.GetTexture("LeftConnect"); mRightConnectTex = BehaviorDesignerUtility.GetTexture("RightConnect"); }
public void onSelect(bool flag) { isSelected = flag; if (flag) { BehaviorManager.GetInstance().SelectNode(this.NodeData); } }
public void ResetId() { this.nodeId = BehaviorManager.GetInstance().AutoNodeId(); foreach (var child in children) { child.ResetId(); } }
///菜单相关 public void PopMenu() { var menu = new GenericMenu(); menu.AddItem(new GUIContent("创建子节点"), false, this.PopUpCreate); menu.AddItem(new GUIContent("替换当前节点"), false, this.PopUpReplace); string selectNodeName = BehaviorManager.GetInstance().selectNodeName; string selectNodeType = BehaviorManager.GetInstance().selectNodeType; if (mSelectedNode != null && selectNodeName != "") { if (selectNodeType != NodeClassifyType.Root.ToString() && mSelectedNode.NodeData.Children.Count < mSelectedNode.NodeData.Proto.child_limit) { menu.AddItem(new GUIContent(string.Format($"新建{selectNodeName}")), false, this.CreateNode); } else { menu.AddDisabledItem(new GUIContent("新建")); } if (mSelectedNode.NodeData.Proto.classify == NodeClassifyType.Root.ToString()) { string menuName = string.Format($"替换成{selectNodeName}"); if (selectNodeType == NodeClassifyType.Root.ToString()) { menu.AddItem(new GUIContent(menuName), false, this.ChangeNodeType); } else { menu.AddDisabledItem(new GUIContent(menuName)); } } else { string menuName = string.Format($"替换成{selectNodeName}"); NodeClassifyType value = (NodeClassifyType)Enum.Parse(typeof(NodeClassifyType), selectNodeType); int count = ExportNodeTypeConfig.NodeTypeCountDict[value]; if (selectNodeType == NodeClassifyType.Root.ToString() || (count == 0 && mSelectedNode.NodeData.Proto.child_limit > 0)) { menu.AddDisabledItem(new GUIContent(menuName)); } else { menu.AddItem(new GUIContent(menuName), false, this.ChangeNodeType); } } } else { menu.AddDisabledItem(new GUIContent("新建")); menu.AddDisabledItem(new GUIContent("替换")); } menu.AddItem(new GUIContent("自动排序"), false, this.AutoSort); menu.AddItem(new GUIContent("复制"), false, this.CopyNode); menu.AddItem(new GUIContent("剪切"), false, this.CutNode); menu.AddItem(new GUIContent("粘贴"), false, this.PasteNode); menu.AddItem(new GUIContent("删除节点"), false, this.RemoveNode); menu.ShowAsContext(); }
public void Run(BehaviorTree tree, List <long> pathList) { if (BehaviorManager.GetInstance().BehaviorTreeConfig != null && tree.behaviorTreeConfig.name == BehaviorManager.GetInstance().BehaviorTreeConfig.name) { BehaviorManager.treePathList.Add(pathList); BehaviorManager.GetInstance().ClearDebugState(); BehaviorManager.GetInstance().SetDebugState(tree, pathList); } }
public void onCreateNode(params object[] list) { string name = (string)list[0]; Vector2 pos = (Vector2)list[1]; var nodeProto = BehaviorManager.GetInstance().GetNodeTypeProto(name); var nodeData = BehaviorManager.GetInstance().CreateNode((int)BehaviorManager.GetInstance().CurTree.Id, nodeProto.name); CreateNode(nodeData, pos); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); BehaviorTreeConfig config = target as BehaviorTreeConfig; if (GUILayout.Button("打开行为树")) { BehaviorManager.GetInstance().OpenBehaviorEditor(config.gameObject); } EditorUtility.SetDirty(config); }
/// 事件相关 public NodeDesigner onCreateTree(params object[] list) { if (BehaviorManager.GetInstance().CurTree == null) { Log.Error($"CurTree can not be null"); return(null); } RootNode = new NodeDesigner(BehaviorManager.GetInstance().CurTree.Root); CalcGraphRect(); return(RootNode); }
public GameObject BehaviourTreeField(string desc, GameObject value) { EditorGUILayout.BeginHorizontal(); value = (GameObject)EditorGUILayout.ObjectField(desc, value, typeof(GameObject), false); if (value.GetComponent <BehaviorTreeConfig>() != null && GUILayout.Button("打开行为树")) { BehaviorManager.GetInstance().OpenBehaviorEditor(value); SetToolBar(2); } EditorGUILayout.EndHorizontal(); return(value); }
private void ClearNodes() { BehaviorManager.GetInstance().selectNodeName = ""; mEnumNodeTypeSelection = 0; mSearchNode = ""; foreach (FoldoutFolder folder in mNodeFoldout.Folders) { foreach (FoldoutNode node in folder.Nodes) { node.Hide = false; } } }
public void RemoveNode() { if (mSelectedNode == null) { return; } if (mSelectedNode.Parent != null) { mSelectedNode.Parent.RemoveChild(mSelectedNode); return; } mDetachedNodes.Remove(mSelectedNode); BehaviorManager.GetInstance().ResetTreeId(); }
public void PasteNode() { if (mCutNode != null && mCutNode != mSelectedNode) { ConnectNode(mCutNode, mSelectedNode); } if (mCopyNode != null && mCopyNode != mSelectedNode) { var data = BehaviorManager.GetInstance().CopyNode(mCopyNode.NodeData); BehaviorManager.GetInstance().ResetTreeId(); var node = CreateNode(data, Vector2.zero); ConnectNode(node, mSelectedNode); } }
public NodeDesigner CreateNode(BehaviorNodeData nodeData, Vector2 pos) { NodeDesigner node = new NodeDesigner(nodeData); node.Pos = pos == Vector2.zero? CenterPosInBorder() : pos; if (mSelectedNode != null) { mSelectedNode.AddChild(node); mSelectedNode.AutoSort(); } else { mDetachedNodes.Add(node); } BehaviorManager.GetInstance().ResetTreeId(); Game.Scene.GetComponent <EventComponent>().Run(EventIdType.BehaviorTreeCreateNode, node); return(node); }
private void UpdateList() { mNodeFoldout = new FoldoutFolder("所有节点", SelectNodeFolderCallback); mNodeFoldout.Fold = true; foreach (var kv in BehaviorManager.GetInstance().Classify2NodeProtoList) { string classify = kv.Key; var nodeTypeList = kv.Value; FoldoutFolder folder = mNodeFoldout.AddFolder(classify, SelectNodeFolderCallback); folder.Fold = true; mNodeCount++; foreach (var nodeType in nodeTypeList) { folder.AddNode(classify, nodeType.name + " (" + nodeType.describe + ")", SelectNodeCallback); mNodeCount++; } } }
public BehaviorNodeData(string proto_name) { name = proto_name; this.Proto = BehaviorManager.GetInstance().GetNodeTypeProto(proto_name); if (this.Proto == null) { this.Proto = BehaviorManager.GetInstance().GetNodeTypeProto("Unknow"); return; } mClassify = this.Proto.classify; foreach (var args_desc in this.Proto.new_args_desc) { args_dict.SetKeyValueComp(args_desc.type, args_desc.name, args_desc.value); } foreach (BehaviorNodeData child in children) { AddChild(child); } }
private void DrawValueView() { if (mCurBehaviorNode == null || mCurBehaviorNode.Proto == null) { return; } if (GUILayout.Button("保存行为树")) { BehaviorManager.GetInstance().SaveAll(); } ClientNodeTypeProto proto = mCurBehaviorNode.Proto; GUILayout.Space(10f); GUILayout.Label("节点ID:" + mCurBehaviorNode.nodeId); GUILayout.Label("节点名:" + mCurBehaviorNode.name); GUILayout.Label("说明:"); GUILayout.Label(proto.describe); GUILayout.Label("描述:"); mCurBehaviorNode.describe = EditorGUILayout.TextArea(mCurBehaviorNode.describe, GUILayout.Height(50f)); DrawAllValue(proto); }
public void HandleEvents() { var e = Event.current; switch (e.type) { case EventType.KeyUp: if (e.keyCode == KeyCode.Escape || (e.keyCode == KeyCode.S && e.control)) { BehaviorManager.GetInstance().SaveAll(); } else if (e.keyCode == KeyCode.F4) { BehaviorManager.GetInstance().SaveAll(); } break; case EventType.MouseDown: break; } }
private void DrawNodeFunctions(float offset) { Rect boxRect = new Rect(0f, Screen.height - offset + 15f, this.mWidth, 200f); GUILayout.BeginArea(boxRect); BehaviorManager.GetInstance().selectNodeName = ""; if (mCurNode != null) { string[] arr = mCurNode.Text.Split(' '); string name = arr[0]; BehaviorManager.GetInstance().selectNodeName = name; BehaviorManager.GetInstance().selectNodeType = mCurNode.folderName; if (mCurNode.folderName != NodeClassifyType.Root.ToString()) { if (GUILayout.Button("新建")) { Game.Scene.GetComponent <EventComponent>().Run(EventIdType.BehaviorTreePropertyDesignerNewCreateClick, name, Vector2.zero); } } if (mCurNode.folderName != NodeClassifyType.Root.ToString() || (mCurNode.folderName == NodeClassifyType.Root.ToString() && mCurBehaviorNode.IsRoot())) { if (GUILayout.Button("替换")) { Game.Scene.GetComponent <EventComponent>().Run(EventIdType.BehaviorTreeReplaceClick, name, Vector2.zero); } } if (GUILayout.Button("保存")) { BehaviorManager.GetInstance().SaveAll(); } var node = BehaviorManager.GetInstance().GetNodeTypeProto(name); GUILayout.Label("节点名:" + node.name); GUILayout.Label("描述:" + node.describe); } GUILayout.EndArea(); }
public List <string> GetCanCreateList() { List <string> result = new List <string>(); foreach (KeyValuePair <string, List <ClientNodeTypeProto> > kv in BehaviorManager.GetInstance().Classify2NodeProtoList) { string classify = kv.Key; List <ClientNodeTypeProto> nodeProtoList = kv.Value; nodeProtoList.Sort(CompareShowName); if (classify == NodeClassifyType.Root.ToString()) { continue; } foreach (var node in nodeProtoList) { if (mSelectedNode != null && mSelectedNode.NodeData.Children.Count < mSelectedNode.NodeData.Proto.child_limit) { result.Add(node.name); } } } return(result); }
public List <string> GetCanRepalceList() { List <string> result = new List <string>(); if (mSelectedNode != null) { if (mSelectedNode.NodeData.Proto.classify == NodeClassifyType.Root.ToString() || BehaviorManager.GetInstance().CurTree.Root.nodeId == mSelectedNode.NodeData.nodeId) { List <ClientNodeTypeProto> list = BehaviorManager.GetInstance().Classify2NodeProtoList[NodeClassifyType.Root.ToString()]; foreach (ClientNodeTypeProto nodeType in list) { result.Add(nodeType.name); // menu.AddItem(new GUIContent(string.Format("{0}/{1}", "替换为", nodeType.name)), false, new GenericMenu.MenuFunction2(this.ChangeNodeType), nodeType.name); } } else { // NodeChildLimitType nodeChildLimitType = mSelectedNode.NodeData.IsLeaf() ? NodeChildLimitType.All : NodeChildLimitType.WithChild; List <ClientNodeTypeProto> canSubtituteList = BehaviorManager.GetInstance().AllNodeProtoList; canSubtituteList.Sort(CompareShowName); foreach (ClientNodeTypeProto nodeType in canSubtituteList) { if (nodeType.classify == NodeClassifyType.Root.ToString()) { continue; } // menu.AddItem(new GUIContent(string.Format("{0}/{1}", "替换为", nodeType.name)), false, new GenericMenu.MenuFunction2(this.ChangeNodeType), nodeType.name); if (mSelectedNode.NodeData.Proto.child_limit <= nodeType.child_limit) { result.Add(nodeType.name); } } } } return(result); }
private object InputEnumFieldValue(NodeFieldDesc desc) { string oldValue = mCurBehaviorNode.args_dict.GetTreeDictValue(desc.type, desc.name)?.ToString(); string[] enumValueArr = BehaviorManager.GetInstance().GetCanInPutEnvKeyArray(mCurBehaviorNode, desc); if (enumValueArr.Length == 0) { enumValueArr = new string[1] { BTEnvKey.None }; } else if (string.IsNullOrEmpty(oldValue)) { oldValue = enumValueArr[0]; } int oldSelect = IndexInStringArr(enumValueArr, oldValue); string label = desc.name + (desc.desc == ""? "" : $"({desc.desc})") + $"({desc.envKeyType})"; EditorGUILayout.LabelField(label); int selection = EditorGUILayout.Popup("", oldSelect, enumValueArr); string newValue = enumValueArr[selection]; return(newValue); }
public void Draw() { foreach (var child in this.Children) { //先画子节点,让线条在最低层 BehaviorDesignerUtility.DrawConnection(this.RightPos, child.LeftPos); child.Draw(); } //左链接 var tex = mLeftConnectTex; var rect = new Rect(Pos.x - Width / 2 - tex.width / 6, Pos.y - tex.height / 4, tex.width / 2, tex.height / 2); GUI.DrawTexture(rect, tex); //右链接 if (NodeData.Proto.child_limit > 0) { tex = mRightConnectTex; rect = new Rect(Pos.x + Width / 2 - tex.width / 6 * 2, Pos.y - tex.height / 4.1f, tex.width / 2, tex.height / 2); GUI.DrawTexture(rect, tex); if (NodeData.Children.Count > 0) { if (DesignerData.fold) { GUI.Label(new Rect(Pos.x + Width / 2 - 5f, Pos.y - 9f, tex.width, tex.height), "+"); } else { GUI.Label(new Rect(Pos.x + Width / 2 - 8f, Pos.y - 9f, tex.width, tex.height), "—"); } } } rect = new Rect(Pos.x - Width / 2, Pos.y - Height / 2, Width, Height); GUI.DrawTexture(rect, mBoxTex); if (isSelected) { GUI.DrawTexture(rect, mBoxSelectHighLight); } else if (BehaviorManager.GetInstance().IsHighLight(this.NodeData)) { GUI.DrawTexture(rect, mBoxHighLight); } GUIStyle style = new GUIStyle(); style.normal.background = null; style.normal.textColor = new Color(1, 1, 1); style.fontSize = 15; if (!this.isSelected) { style.clipping = TextClipping.Clip; } GUI.Label(new Rect(Pos.x - Width / 2 + 5f, Pos.y - Height / 3, Width - 10f, Height / 2), NodeData.Proto.name, style); style.normal.textColor = new Color(0.8f, 0.8f, 0.8f); style.fontSize = 12; style.wordWrap = true; string deprecatedDesc = NodeData.Proto.isDeprecated? $"({NodeData.Proto.deprecatedDesc})" : ""; GUI.Label(new Rect(Pos.x - Width / 2 + 6f, Pos.y, Width - 12f, Height / 2.1f), NodeData.describe + deprecatedDesc, style); tex = null; switch (NodeData.NodeDeubgState) { case DebugState.True: tex = BehaviorDesignerUtility.GetTexture("Execute"); break; // case DebugState.False: // tex = BehaviorDesignerUtility.GetTexture("False"); // break; // case DebugState.Error: // tex = BehaviorDesignerUtility.GetTexture("Error"); // break; } if (tex != null) { GUI.DrawTexture(rect, tex); // string time = System.DateTime.Now.ToString(); // GUI.Label(new Rect(Pos.x + Width / 2, Pos.y -20, Width - 12f, Height / 2.1f), time, style); } }
private void ChangeNodeType() { ChangeNodeType(BehaviorManager.GetInstance().selectNodeName); }
public void OnDestroy() { BehaviorManager.GetInstance().Clear(); }
private void CreateNode() { var nodeProto = BehaviorManager.GetInstance().GetNodeTypeProto(BehaviorManager.GetInstance().selectNodeName); var nodeData = BehaviorManager.GetInstance().CreateNode((int)BehaviorManager.GetInstance().CurTree.Id, nodeProto.name); var node = CreateNode(nodeData, MousePosToGraphPos(mMousePos)); }