Ejemplo n.º 1
0
        void OnGUI()
        {
            BtGrid.DrawGrid(position.size);
            GUILayout.BeginHorizontal();
            {
                GUILayout.BeginVertical();
                {
                    mBehaviourTree.Update(position);
                }
                GUILayout.EndVertical();
                GUILayout.BeginVertical(GUILayout.MaxWidth(BtConst.RightInspectWidth));
                {
                    DrawNodeInspector();
                }
                GUILayout.EndVertical();
            }
            GUILayout.EndHorizontal();

            if (Event.type == EventType.KeyUp)
            {
                if (Event.keyCode == KeyCode.Delete)
                {
                    if (CurSelectNode != null && !CurSelectNode.IsRoot)
                    {
                        Event.Use();
                        BtHelper.RemoveChild(CurSelectNode);
                    }
                }
            }
        }
Ejemplo n.º 2
0
 private void SetNodePosition(BtNode parent)
 {
     BtHelper.AutoAlignPosition(parent);
     if (parent.IsHaveChild)
     {
         foreach (var node in parent.ChildNodeList)
         {
             SetNodePosition(node);
         }
     }
 }
Ejemplo n.º 3
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);
 }
Ejemplo n.º 4
0
        public void Initialize()
        {
            if (mBehaviourTree == null)
            {
                mBehaviourTree = new BehaviourTree();
            }

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

            BtHelper.LoadNodeFile();

            LoadBehaviorTree();
        }
Ejemplo n.º 5
0
        public BehaviourTree(string name, BtNodeData data = null)
        {
            Name           = name;
            NodeDict       = new Dictionary <string, BtNode>();
            NodePosDict    = new Dictionary <string, int>();
            BrokenNodeDict = new Dictionary <string, BtNode>();
            if (data == null)
            {
                data = new BtNodeData(BtConst.RootName, null,
                                      (BtEditorWindow.Window.position.width - BtConst.RightInspectWidth) / 2, 50);
                data.AddData("restart", "1");
            }

            Root = new BtNode(this, null, data);
            AddNode(Root);
            BtHelper.AutoAlignPosition(Root);
        }
Ejemplo n.º 6
0
        public void Callback(object obj)
        {
            var 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.AddChildNode(Owner, this, name);
                BtHelper.SetNodeDefaultData(node, name);
            }
        }
Ejemplo n.º 7
0
        void OnGUI()
        {
            GUILayout.Space(SPACE_VALUE);
            if (GUILayout.Button("刷新路径"))
            {
                BtHelper.CleanPath();
            }

            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();
            }
        }
Ejemplo n.º 8
0
        public BtGrid()
        {
            var path = BtHelper.ModulePath() + "/Editor/GUI/background.png";

            mBackground = AssetDatabase.LoadAssetAtPath <Texture>(path);
        }
Ejemplo n.º 9
0
        private void ShowMenu()
        {
            var menu = BtHelper.GetGenericMenu(this, Callback);

            menu.ShowAsContext();
        }
Ejemplo n.º 10
0
        private void DrawNodeInspector()
        {
            GUI.DrawTexture(new Rect(position.width - BtConst.RightInspectWidth - 5, 0,
                                     BtConst.RightInspectWidth + 5, 500), BtNodeStyle.NodeEditorBg);

            EditorGUILayout.BeginHorizontal();
            {
                IsDebug = GUILayout.Toggle(IsDebug, "是否调试", GUILayout.MaxWidth(80));
                //IsAutoAlign = GUILayout.Toggle(IsAutoAlign, "自动对齐", GUILayout.MaxWidth(80));
                //IsLockAxisY = GUILayout.Toggle(IsLockAxisY, "锁定Y轴", GUILayout.MaxWidth(80));
            }
            EditorGUILayout.EndHorizontal();

            GUILayout.Space(SPACE_VALUE);
            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, $"{fileName}.json");
                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 != null && mBehaviourTree.BrokenNodeDict.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.RightInspectWidth));
                }
                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();
            }
        }