Beispiel #1
0
 private void SetNodePosition(BTNode parent)
 {
     BTHelper.AutoAlignPosition(parent);
     if (parent.IsHaveChild)
     {
         foreach (var node in parent.ChildNodeList)
         {
             SetNodePosition(node);
         }
     }
 }
Beispiel #2
0
 public BTNode(BehaviourTree owner, BTNode parent, BTNodeData data)
 {
     Owner                = owner;
     Parent               = parent;
     Data                 = data;
     BTNodeGraph          = new BTNodeGraph();
     NodeName             = data.name;
     BTNodeGraph.RealRect = new Rect(data.posX, data.posY, BTConst.DefaultWidth, BTConst.DefaultHeight);
     ChildNodeList        = new List <BTNode> ();
     Guid                 = BTHelper.GenerateUniqueStringId();
     Type                 = BTHelper.CreateNodeType(this);
 }
 public BehaviourTree(string name, BTNodeData data = null)
 {
     Name           = name;
     BTNodeDict     = new Dictionary <string, BTNode> ();
     OrphanNodeDict = new Dictionary <string, BTNode> ();
     if (data == null)
     {
         data = new BTNodeData(BTConst.RootName, null,
                               (BTEditorWindow.window.position.width - BTConst.RIGHT_INSPECT_WIDTH) / 2, 50);
         data.AddData("restart", "1");
     }
     Root = new BTNode(this, null, data);
     AddNode(Root);
     BTHelper.AutoAlignPosition(Root);
 }
Beispiel #4
0
        public void Initialize()
        {
            if (mBehaviourTree == null)
            {
                mBehaviourTree = new BehaviourTree();
            }

            if (BTGrid == null)
            {
                BTGrid = new BTGrid();
            }

            BTHelper.LoadNodeFile();

            LoadBehaviorTree();
        }
Beispiel #5
0
        public void Callback(object obj)
        {
            string name = obj.ToString();

            if (name == "Delete")
            {
                BTHelper.RemoveChild(this);
            }
            else if (name == "Copy")
            {
                BTEditorWindow.CopyNode = this;
            }
            else if (name == "Paste")
            {
                BTHelper.PasteChild(Owner, this, Data.posX, Data.posY + BTConst.DefaultHeight);
            }
            else
            {
                var node = BTHelper.AddChild(Owner, this, name);
                BTHelper.SetNodeDefaultData(node, name);
            }
        }
Beispiel #6
0
        void DrawNodeInspector()
        {
            GUI.DrawTexture(new Rect(position.width - BTConst.RIGHT_INSPECT_WIDTH - 5, 0,
                                     BTConst.RIGHT_INSPECT_WIDTH + 5, 500), BTNodeStyle.NodeEditorBG);

            GUILayout.Space(SPACE_VALUE);
            //EditorGUILayout.BeginHorizontal ();
            //{
            //	IsAutoAlign = GUILayout.Toggle (IsAutoAlign, "自动对齐", GUILayout.MaxWidth (80));
            //	IsLockAxisY = GUILayout.Toggle (IsLockAxisY, "锁定Y轴", GUILayout.MaxWidth (80));
            //}
            //EditorGUILayout.EndHorizontal ();
            EditorGUILayout.BeginHorizontal();
            {
                if (GUILayout.Button("JsonBT目录"))
                {
                    System.Diagnostics.Process.Start(BTHelper.jsonPath);
                }
                if (GUILayout.Button("LuaBT目录"))
                {
                    System.Diagnostics.Process.Start(BTHelper.behaviorPath);
                }
                if (GUILayout.Button("Node配置"))
                {
                    BTEditorOption.ShowWindow();
                }
            }
            EditorGUILayout.EndHorizontal();

            GUILayout.Space(SPACE_VALUE);
            if (GUILayout.Button("加载行为树"))
            {
                LoadBehaviorTree();
            }

            if (mAllShowJsons != null && mAllShowJsons.Length > 0 && mLastSelectJson != mCurSelectJson)
            {
                mLastSelectJson = mCurSelectJson;
                var fileName = mAllShowJsons [mCurSelectJson];
                var file     = Path.Combine(BTHelper.jsonPath, string.Format("{0}.json", fileName));
                mBehaviourTree = BTHelper.LoadBehaviorTree(file);
                if (mBehaviourTree == null)
                {
                    Debug.LogErrorFormat("读取行为树失败, {0}", file);
                    mBehaviourTree = new BehaviourTree(DEFAULE_BT_NAME);
                }
            }

            if (GUILayout.Button(DEFAULE_BT_NAME))
            {
                mBehaviourTree = new BehaviourTree(DEFAULE_BT_NAME);
            }

            GUILayout.Space(SPACE_VALUE);
            EditorGUIUtility.labelWidth = 40;
            if (mAllShowJsons != null && mAllShowJsons.Length > 0)
            {
                mCurSelectJson = EditorGUILayout.Popup("行为树:", mCurSelectJson, mAllShowJsons);
            }
            EditorGUIUtility.labelWidth = 60;

            GUILayout.Space(SPACE_VALUE);
            EditorGUILayout.BeginVertical("Box");
            {
                EditorGUILayout.LabelField("行为树数据");
                if (mBehaviourTree != null)
                {
                    mBehaviourTree.Name = EditorGUILayout.TextField("行为树名:", mBehaviourTree.Name);
                }

                GUI.color = Color.green;
                if (GUILayout.Button("保存行为树"))
                {
                    if (mBehaviourTree.OrphanNodeDict.Count > 0)
                    {
                        EditorUtility.DisplayDialog("提示", "有节点未连上", "确定");
                    }
                    else
                    {
                        BTHelper.SaveBTData(mBehaviourTree);
                    }
                }
                GUI.color = Color.white;
            }
            EditorGUILayout.EndVertical();

            GUILayout.Space(SPACE_VALUE);
            var node = window.CurSelectNode;

            if (node != null)
            {
                var data = node.Data;
                EditorGUILayout.BeginHorizontal();

                if (mIsSettingNode)
                {
                    EditorGUILayout.LabelField("节点说明", GUILayout.MaxWidth(50));
                    data.desc = EditorGUILayout.TextArea(data.desc, GUILayout.MaxWidth(BTConst.RIGHT_INSPECT_WIDTH));
                }
                else
                {
                    EditorGUILayout.HelpBox(data.desc, MessageType.Info);
                }

                if (GUILayout.Button(mIsSettingNode ? "ok" : "set", GUILayout.MaxWidth(30)))
                {
                    mIsSettingNode = !mIsSettingNode;
                }
                EditorGUILayout.EndHorizontal();

                GUILayout.Space(SPACE_VALUE);
                EditorGUILayout.BeginVertical("Box");
                {
                    EditorGUILayout.LabelField("节点数据");
                    data.displayName = EditorGUILayout.TextField("显示名:", data.displayName);
                    EditorGUILayout.LabelField("节点名:", data.name);
                    if (node.Guid != mLastNodeGuid)
                    {
                        mLastNodeGuid = node.Guid;
                        mChangeDict.Clear();
                    }

                    DrawDataInspector(data);
                }
                EditorGUILayout.EndVertical();
            }
        }
        void OnGUI()
        {
            GUILayout.Space(SPACE_VALUE);
            if (GUILayout.Button("读取配置"))
            {
                mOptions = BTHelper.ReadBTNodeOption();
            }

            GUILayout.Space(SPACE_VALUE);
            GUI.color = Color.green;
            if (GUILayout.Button("保存配置"))
            {
                if (mOptions != null)
                {
                    BTHelper.WriteBTNodeOption(mOptions);
                }
            }
            GUI.color = Color.white;

            if (!string.IsNullOrEmpty(mDelNode))
            {
                mOptions.Remove(mDelNode);
                mDelNode = null;
            }

            if (mSelectDict != null && !string.IsNullOrEmpty(mDelKey))
            {
                mSelectDict.Remove(mDelKey);
                if (mChangeDict.ContainsKey(mDelKey))
                {
                    mChangeDict.Remove(mDelKey);
                }
                mDelKey = null;
            }

            foreach (var key in mChangeDict.Keys)
            {
                if (mSelectDict.ContainsKey(key))
                {
                    mSelectDict [key] = mChangeDict [key];
                }
                else
                {
                    mSelectDict.Add(key, mChangeDict [key]);
                }
            }

            mChangeDict.Clear();

            if (mSelectDict != null)
            {
                EditorGUIUtility.labelWidth = 70;
                EditorGUILayout.BeginVertical("Box");
                {
                    EditorGUILayout.TextField("修改配置:", mSelectNode);

                    EditorGUIUtility.labelWidth = 24;
                    foreach (var kv in mSelectDict)
                    {
                        DrawItemInspector(kv);
                    }

                    EditorGUILayout.BeginHorizontal();
                    {
                        mKey   = EditorGUILayout.TextField("key:", mKey);
                        mValue = EditorGUILayout.TextField("val:", mValue);
                        if (GUILayout.Button("+", GUILayout.MaxWidth(20)))
                        {
                            if (!string.IsNullOrEmpty(mKey))
                            {
                                mSelectDict.Add(mKey, mValue);
                                mKey   = "";
                                mValue = "";
                            }
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.EndVertical();
                GUILayout.Space(SPACE_VALUE);
            }

            if (mOptions != null)
            {
                EditorGUILayout.LabelField("所有配置:");
                scrollPos = EditorGUILayout.BeginScrollView(scrollPos);
                EditorGUIUtility.labelWidth = 35;
                foreach (var key in mOptions.Keys)
                {
                    DrawOptionInspector(key);
                }
                EditorGUILayout.EndScrollView();

                EditorGUIUtility.labelWidth = 60;
                EditorGUILayout.BeginHorizontal();
                {
                    mAddNode = EditorGUILayout.TextField("新增节点:", mAddNode);
                    if (GUILayout.Button("+", GUILayout.MaxWidth(20)))
                    {
                        if (!string.IsNullOrEmpty(mAddNode))
                        {
                            var data = new Dictionary <string, string> ();
                            data.Add("displayName", "");
                            mOptions.Add(mAddNode, data);
                            mAddNode = null;
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
        }
Beispiel #8
0
        void ShowMenu()
        {
            var menu = BTHelper.GetGenericMenu(this, Callback);

            menu.ShowAsContext();
        }