//有待优化 private void ChangeNodeType(object obj) { string nodeType = (string)obj; NodeMeta nodeProto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(nodeType); BehaviorNodeData nodeData = BTEditor.Instance.CreateNode(nodeProto.name); NodeDesigner oldNode = mSelectedNode; NodeDesigner newNode = new NodeDesigner(nodeData); if (oldNode == RootNode) { newNode.NodeData.Id = RootNode.NodeData.Id; RootNode = newNode; BehaviorTreeData oldTree = BTEditor.Instance.CurTree; BehaviorTreeData newTree = new BehaviorTreeData() { classify = oldTree.classify, Root = nodeData }; BTEditor.Instance.CurTree = newTree; } else { int idx = oldNode.Parent.Children.IndexOf(oldNode); oldNode.Parent.AddChild(newNode, idx); oldNode.Parent.RemoveChild(oldNode); } foreach (NodeDesigner child in oldNode.Children) { newNode.AddChild(child); } BTEditor.Instance.ResetTreeId(); Game.EventSystem.Run(EventIdType.BehaviorTreeAfterChangeNodeType); }
public NodeMeta GetNodeMeta(string nodeName) { NodeMeta nodeMeta = null; this.nameNodeMetas.TryGetValue(nodeName, out nodeMeta); return(nodeMeta); }
private void CreateNode() { NodeMeta nodeProto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(BTEditor.Instance.selectNodeName); BehaviorNodeData nodeData = BTEditor.Instance.CreateNode(nodeProto.name); CreateNode(nodeData, MousePosToGraphPos(mMousePos)); }
private static BehaviorNodeConfig CreateNodeConfig(this BehaviorTreeConfig treeConfig, string name) { NodeMeta proto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(name); GameObject go = new GameObject() { name = name }; go.transform.parent = treeConfig.gameObject.transform; BehaviorNodeConfig node = go.AddComponent <BehaviorNodeConfig>(); node.name = name; node.describe = proto.describe; foreach (NodeFieldDesc args in proto.new_args_desc) { Type type = BTTypeManager.GetBTType(args.type); UnityEngine.Component comp = go.AddComponent(type); FieldInfo info = type.GetField("fieldName"); info.SetValue(comp, args.name); FieldInfo info1 = type.GetField("fieldValue"); info1.SetValue(comp, args.value); } return(node); }
private static int CompareShowName(NodeMeta nodeType1, NodeMeta nodeType2) { if (string.IsNullOrEmpty(nodeType1.name) || string.IsNullOrEmpty(nodeType2.name)) { Log.Error("字符串输入参数有误"); } return(String.CompareOrdinal(nodeType1.name, nodeType2.name)); }
public void onCreateNode(params object[] list) { string nodeName = (string)list[0]; Vector2 pos = (Vector2)list[1]; NodeMeta nodeProto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(nodeName); BehaviorNodeData nodeData = BTEditor.Instance.CreateNode(nodeProto.name); CreateNode(nodeData, pos); }
public static Dictionary <string, NodeMeta> ExportToDict() { Dictionary <string, NodeMeta> name2NodeProtoDict = new Dictionary <string, NodeMeta>(); List <Type> types = Game.EventSystem.GetTypes(); foreach (Type type in types) { NodeMeta proto = GetNodeTypeProtoFromType(type); if (proto == null) { continue; } name2NodeProtoDict.Add(proto.name, proto); } return(name2NodeProtoDict); }
public void RemoveUnusedArgs(NodeProto nodeProto) { NodeMeta proto = this.GetComponent <BTNodeInfoComponent>().GetNodeMeta(nodeProto.Name); List <string> unUsedList = new List <string>(); foreach (KeyValuePair <string, object> item in nodeProto.Args.Dict()) { if (!proto.new_args_desc.Exists(a => (a.name == item.Key))) { unUsedList.Add(item.Key); } } foreach (string item in unUsedList) { nodeProto.Args.Remove(item); } for (int i = 0; i < nodeProto.children.Count; i++) { RemoveUnusedArgs(nodeProto.children[i]); } }
public List <string> Filter(List <string> list, string text) { List <string> result1 = new List <string>(); string selectType; if (mEnumNodeTypeSelection == 0) { selectType = "All"; result1 = list; } else { selectType = Enum.GetName(typeof(NodeClassifyType), mEnumNodeTypeSelection - 1); foreach (string name in list) { NodeMeta proto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(name); if (selectType == proto.classify) { result1.Add(name); } } } if (string.IsNullOrEmpty(text)) { return(result1); } List <string> result2 = new List <string>(); foreach (string name in result1) { NodeMeta proto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(name); if (name.ToUpper().Contains(text.ToUpper()) || proto.describe.ToUpper().Contains(text.ToUpper())) { result2.Add(name); } } return(result2); }
private void DrawAllValue(NodeMeta proto) { List <NodeFieldDesc> paramFieldList = GetFieldDescList(proto.new_args_desc, typeof(NodeFieldAttribute)); List <NodeFieldDesc> inputFieldList = GetFieldDescList(proto.new_args_desc, typeof(NodeInputAttribute)); List <NodeFieldDesc> outputFieldList = GetFieldDescList(proto.new_args_desc, typeof(NodeOutputAttribute)); mFoldParam = EditorGUILayout.Foldout(mFoldParam, "参数"); if (mFoldParam) { DrawProp(proto.name, paramFieldList, NodeParamType.None); } mFoldInput = EditorGUILayout.Foldout(mFoldInput, "输入"); if (mFoldInput) { DrawProp(proto.name, inputFieldList, NodeParamType.Input); } mFoldOutput = EditorGUILayout.Foldout(mFoldOutput, "输出"); if (mFoldOutput) { DrawProp(proto.name, outputFieldList, NodeParamType.Output); } }
private void DrawValueView() { if (mCurBehaviorNode?.Proto == null) { return; } if (GUILayout.Button("保存行为树")) { BTEditor.Instance.SaveAll(); } NodeMeta proto = mCurBehaviorNode.Proto; GUILayout.Space(10f); GUILayout.Label("节点ID:" + mCurBehaviorNode.Id); GUILayout.Label("节点名:" + mCurBehaviorNode.Name); GUILayout.Label("说明:"); GUILayout.Label(proto.describe); GUILayout.Label("描述:"); mCurBehaviorNode.Desc = EditorGUILayout.TextArea(mCurBehaviorNode.Desc, GUILayout.Height(50f)); DrawAllValue(proto); }
private void DrawNodeFunctions(float offset) { Rect boxRect = new Rect(0f, Screen.height - offset + 15f, this.mWidth, 200f); GUILayout.BeginArea(boxRect); BTEditor.Instance.selectNodeName = ""; if (mCurNode != null) { string[] arr = mCurNode.Text.Split(' '); string name = arr[0]; BTEditor.Instance.selectNodeName = name; BTEditor.Instance.selectNodeType = mCurNode.folderName; if (mCurNode.folderName != NodeClassifyType.Root.ToString()) { if (GUILayout.Button("新建")) { Game.EventSystem.Run(EventIdType.BehaviorTreePropertyDesignerNewCreateClick, name, Vector2.zero); } } if (mCurNode.folderName != NodeClassifyType.Root.ToString() || (mCurNode.folderName == NodeClassifyType.Root.ToString() && mCurBehaviorNode.IsRoot())) { if (GUILayout.Button("替换")) { Game.EventSystem.Run(EventIdType.BehaviorTreeReplaceClick, name, Vector2.zero); } } if (GUILayout.Button("保存")) { BTEditor.Instance.SaveAll(); } NodeMeta node = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(name); GUILayout.Label("节点名:" + node.name); GUILayout.Label("描述:" + node.describe); } GUILayout.EndArea(); }
public static NodeMeta GetNodeTypeProtoFromType(Type type) { object[] nodeAttrs = type.GetCustomAttributes(typeof(NodeAttribute), false); if (nodeAttrs.Length == 0) { return(null); } object[] nodeDeprecatedAttrs = type.GetCustomAttributes(typeof(NodeDeprecatedAttribute), false); NodeAttribute nodeAttribute = nodeAttrs[0] as NodeAttribute; NodeDeprecatedAttribute nodeDeprecatedAttribute = null; if (nodeDeprecatedAttrs.Length != 0) { nodeDeprecatedAttribute = nodeDeprecatedAttrs[0] as NodeDeprecatedAttribute; } NodeMeta proto = new NodeMeta() { type = nodeAttribute.ClassifytType.ToString(), name = type.Name, describe = nodeAttribute.Desc }; if (nodeDeprecatedAttribute != null) { proto.isDeprecated = true; proto.deprecatedDesc = nodeDeprecatedAttribute.Desc; } proto.new_args_desc.AddRange(GetNodeFieldDesc(type, typeof(NodeInputAttribute))); proto.new_args_desc.AddRange(GetNodeFieldDesc(type, typeof(NodeOutputAttribute))); proto.new_args_desc.AddRange(GetNodeFieldDesc(type, typeof(NodeFieldAttribute))); proto.child_limit = NodeTypeCountDict[nodeAttribute.ClassifytType]; proto.classify = nodeAttribute.ClassifytType.ToString(); return(proto); }
private void DrawProp(string nodeName, List <NodeFieldDesc> fieldList, NodeParamType nodeParamType) { for (int i = 0; i < fieldList.Count; i++) { NodeFieldDesc desc = fieldList[i]; Type fieldType = NodeMetaHelper.GetFieldType(nodeName, desc.name); NodeMeta nodeMeta = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(nodeName); // 如果不存在这个参数,给一个默认的 if (!mCurBehaviorNode.Args.ContainsKey(desc.name)) { object obj = desc.value ?? BTTypeManager.GetDefaultValue(fieldType); mCurBehaviorNode.Args.Add(desc.name, obj); } object newValue = null; if (TypeHelper.IsStringType(fieldType)) { if (nodeParamType == NodeParamType.Input) { newValue = InputEnumFieldValue(desc); } else if (nodeParamType == NodeParamType.Output && nodeMeta.classify == NodeClassifyType.Root.ToString()) { newValue = ConstTextFieldValue(desc); } else { newValue = TextFieldValue(desc); } } else if (TypeHelper.IsFloatType(fieldType)) { newValue = FloatFieldValue(desc); } else if (TypeHelper.IsDoubleType(fieldType)) { newValue = DoubletFieldValue(desc); } else if (TypeHelper.IsIntType(fieldType)) { newValue = IntFieldValue(desc); } else if (TypeHelper.IsLongType(fieldType)) { newValue = LongFieldValue(desc); } else if (TypeHelper.IsBoolType(fieldType)) { newValue = BoolFieldValue(desc); } else if (TypeHelper.IsObjectType(fieldType)) { newValue = ObjectFieldValue(desc); } else if (TypeHelper.IsIntArrType(fieldType)) { newValue = IntArrFieldValue(desc); } else if (TypeHelper.IsLongArrType(fieldType)) { newValue = LongArrFieldValue(desc); } else if (TypeHelper.IsStringArrType(fieldType)) { newValue = StrArrFieldValue(desc); } else if (TypeHelper.IsFloatArrType(fieldType)) { newValue = FloatArrFieldValue(desc); } else if (TypeHelper.IsDoubleArrType(fieldType)) { newValue = DoubleArrFieldValue(desc); } else if (TypeHelper.IsEnumType(fieldType)) { newValue = EnumFieldValue(desc); } else if (TypeHelper.IsObjectArrayType(fieldType)) { newValue = ObjectArrFieldValue(desc); } else { Log.Error($"行为树节点暂时未支持此类型:{fieldType}!"); return; } mCurBehaviorNode.Args.SetKeyValueComp(desc.name, newValue); } }
public string DrawSearchList() { List <string> targetList = new List <string>(); if (mSubWinType == SubWinType.CreateNode) { targetList = GraphDesigner.GetCanCreateList(); } else if (mSubWinType == SubWinType.ReplaceNode) { targetList = GraphDesigner.GetCanRepalceList(); } List <string> nodeNameList = Filter(targetList, mSearchNode); GUILayout.BeginHorizontal(); GUI.SetNextControlName("Search"); this.mSearchNode = GUILayout.TextField(this.mSearchNode, GUI.skin.FindStyle("ToolbarSeachTextField")); GUI.FocusControl("Search"); GUILayout.EndHorizontal(); // toolbarRect = new Rect(0f, 15f + 20, mWidth, 25f); GUILayout.BeginArea(toolbarRect, EditorStyles.toolbar); GUILayout.BeginHorizontal(); GUILayout.Label("Filter"); Array strArr = Enum.GetValues(typeof(NodeClassifyType)); List <string> strList = new List <string>(); strList.Add("All"); foreach (object str in strArr) { strList.Add(str.ToString()); } mEnumNodeTypeArr = strList.ToArray(); mEnumNodeTypeSelection = EditorGUILayout.Popup(mEnumNodeTypeSelection, mEnumNodeTypeArr); if (GUILayout.Button("Clear")) { ClearNodes(); } GUILayout.EndHorizontal(); GUILayout.EndArea(); // GUILayout.BeginArea(new Rect(0, 0, windowRect.width, windowRect.height)); float topSpace = 60; mTreeScrollPos = GUI.BeginScrollView(new Rect(0f, topSpace, windowRect.width, windowRect.height - topSpace), mTreeScrollPos, new Rect(0f, 0f, windowRect.width - 20f, nodeNameList.Count * 19), false, true); foreach (string name in nodeNameList) { NodeMeta proto = BTEditor.Instance.GetComponent <BTNodeInfoComponent>().GetNodeMeta(name); if (GUILayout.Button(name + $"({proto.describe})", GetButtonStyle())) { if (SubWinType.CreateNode == mSubWinType) { GraphDesigner.onCreateNode(name, Vector2.zero); } else if (SubWinType.ReplaceNode == mSubWinType) { GraphDesigner.onChangeNodeType(name, Vector2.zero); } BTEditorWindow.Instance.CloseSubWin(); } } GUI.EndScrollView(); GUILayout.EndArea(); return(""); }