public static void DeleteNode(Node node, BehaviorTrees bt)
        {
            if (node.parentNode != null)
            {
                node.parentNode.childNodes = ArrayUtility.Remove <Node>(node.parentNode.childNodes, node);
            }

            foreach (Node child in node.childNodes)
            {
                if (child != null)
                {
                    child.parentNode = null;
                }
            }

            if (node.decorators.Length > 0)
            {
                foreach (Decorator decorator in node.decorators)
                {
                    BehaviorTreesEditorUtility.DeleteDecorator(decorator);
                }
            }
            if (node is Composite && (node as Composite).services.Length > 0)
            {
                foreach (Service service in (node as Composite).services)
                {
                    BehaviorTreesEditorUtility.DeleteService(service);
                }
            }
            bt.nodes = ArrayUtility.Remove <Node>(bt.nodes, node);
            BehaviorTreesEditorUtility.DestroyImmediate(node);
        }
        public static T AddService <T>(Composite parent, BehaviorTrees bt)
        {
            if (parent == null)
            {
                Debug.LogWarning("Can't add a service to the behavior trees, because the behavior trees are null.");
                return(default(T));
            }

            Service service = ScriptableObject.CreateInstance(typeof(T)) as Service;

            service.hideFlags = HideFlags.HideInHierarchy;

            service.Name    = BehaviorTreesEditorUtility.GenerateName <T>();
            service.tick    = 0.1f;
            service.comment = service.Name + ": tick every 0.1s";
            service.parent  = parent;
            parent.services = ArrayUtility.Add <Service>(parent.services, service);

            if (EditorUtility.IsPersistent(bt))
            {
                AssetDatabase.AddObjectToAsset(service, bt);
            }

            AssetCreator.SaveAIAsset();
            return((T)(object)service);
        }
        public static T AddDecorator <T>(Node parent, BehaviorTrees bt)
        {
            if (parent == null)
            {
                Debug.LogWarning("Can't add a decorator to the behavior trees, because the behavior trees are null.");
                return(default(T));
            }

            Decorator decorator = ScriptableObject.CreateInstance(typeof(T)) as Decorator;

            decorator.hideFlags = HideFlags.HideInHierarchy;

            decorator.Name    = BehaviorTreesEditorUtility.GenerateName <T>();
            decorator.comment = decorator.Name;
            decorator.parent  = parent;
            parent.decorators = ArrayUtility.Add <Decorator>(parent.decorators, decorator);

            if (EditorUtility.IsPersistent(bt))
            {
                AssetDatabase.AddObjectToAsset(decorator, bt);
            }

            AssetCreator.SaveAIAsset();
            return((T)(object)decorator);
        }
Example #4
0
        private void SelectBehaviorBrain()
        {
            GUIContent content = new GUIContent(BehaviorTreesEditor.active != null ? BehaviorTreesEditor.active.name : "[None Selected]");
            float      width   = EditorStyles.toolbarDropDown.CalcSize(content).x;

            width = Mathf.Clamp(width, 100f, width);
            if (GUILayout.Button(content, EditorStyles.toolbarDropDown, GUILayout.Width(width)))
            {
                GenericMenu menu = new GenericMenu();
                if (BehaviorTreesEditor.active != null)
                {
                    SelectBehaviorBrainMenu(BehaviorTreesEditor.active, ref menu);
                }

                menu.AddItem(new GUIContent("[Create New]"), false, delegate()
                {
                    BehaviorTrees bt = AssetCreator.CreateAsset <BehaviorTrees>(true);
                    if (bt != null)
                    {
                        bt.Name = bt.name;

                        Root root   = BehaviorTreesEditorUtility.AddNode <Root>(BehaviorTreesEditor.center, bt);
                        bt.rootNode = root;
                        root.Name   = "Root";

                        AssetCreator.SaveAIAsset();
                        BehaviorTreesEditor.SelectBehaviorTrees(bt);
                    }
                });
                menu.ShowAsContext();
            }
        }
Example #5
0
        public static void CreateBehaviorTrees()
        {
            // This code is borrowed from ICode(https://www.assetstore.unity3d.com/en/#!/content/13761)
            BehaviorTrees bt = AssetCreator.CreateAsset <BehaviorTrees>(false);

            if (bt != null)
            {
                bt.Name = bt.name;
                Root root = BehaviorTreesEditorUtility.AddNode <Root>(BehaviorTreesEditor.center, bt);
                bt.rootNode = root;
                root.Name   = "Root";
                //AssetCreator.SaveAIAsset();
                SaveAIAsset();
            }
        }
Example #6
0
 private void SelectGameObject()
 {
     if (GUILayout.Button(BehaviorTreesEditor.activeGameObject != null ? BehaviorTreesEditor.activeGameObject.name : "[None Selected]", EditorStyles.toolbarDropDown, GUILayout.Width(100)))
     {
         GenericMenu  toolsMenu = new GenericMenu();
         List <Brain> brains    = BehaviorTreesEditorUtility.FindInScene <Brain>();
         foreach (Brain brain in brains)
         {
             GameObject gameObject = brain.gameObject;
             toolsMenu.AddItem(new GUIContent(gameObject.name), false, delegate()
             {
                 BehaviorTreesEditor.SelectGameObject(gameObject);
             });
         }
         toolsMenu.ShowAsContext();
     }
 }
Example #7
0
        protected override void CanvasContextMenu()
        {
            if (_currentEvent.type != EventType.MouseDown || _currentEvent.button != 1 || _currentEvent.clickCount != 1 || BehaviorTreesEditor.active == null)
            {
                return;
            }

            GenericMenu canvasMenu = new GenericMenu();

            // Composite
            canvasMenu.AddItem(new GUIContent("Create Composite/Selector"), false, delegate()
            {
                BehaviorTreesEditorUtility.AddNode <Selector>(_mousePosition, BehaviorTreesEditor.active);
            });
            canvasMenu.AddItem(new GUIContent("Create Composite/Sequence"), false, delegate()
            {
                BehaviorTreesEditorUtility.AddNode <Sequence>(_mousePosition, BehaviorTreesEditor.active);
            });
            // Task
            canvasMenu.AddItem(new GUIContent("Create Task/Wait"), false, delegate()
            {
                BehaviorTreesEditorUtility.AddNode <Wait>(_mousePosition, BehaviorTreesEditor.active);
            });
            canvasMenu.AddSeparator("Create Task/");
            canvasMenu.AddItem(new GUIContent("Create Task/Task"), false, delegate()
            {
                BehaviorTreesEditorUtility.AddNode <Task>(_mousePosition, BehaviorTreesEditor.active);
            });
            canvasMenu.AddSeparator("Empty/");
            canvasMenu.AddItem(new GUIContent("Empty/Task"), false, delegate()
            {
                BehaviorTreesEditorUtility.EmptyNode(_mousePosition, BehaviorTreesEditor.active);
            });
            canvasMenu.AddSeparator("Save/");
            canvasMenu.AddItem(new GUIContent("Save/Save"), false, delegate()
            {
                AssetCreator.SaveAIAsset();
            });
            canvasMenu.AddSeparator("Refresh/");
            canvasMenu.AddItem(new GUIContent("Refresh/Refresh"), false, delegate()
            {
                AssetCreator.Refresh();
            });

            canvasMenu.ShowAsContext();
        }
        public static void BeginInspectorGUI(ref string name, ref string description, string style = "IN BigTitle", float width = 100f)
        {
            GUILayout.BeginVertical(style);
            EditorGUIUtility.labelWidth = width;

            if (BehaviorTreesEditorUtility.DrawHeader("Default", false))
            {
                GUILayout.BeginHorizontal();
                GUILayout.Space(7f);
                name = EditorGUILayout.TextField("Name", name);
                GUILayout.EndHorizontal();
                if (string.IsNullOrEmpty(description) == false)
                {
                    GUILayout.BeginHorizontal();
                    GUILayout.Space(7f);
                    description = EditorGUILayout.TextArea(description, GUI.skin.textArea, GUILayout.Height(100f));
                    GUILayout.EndHorizontal();
                }
            }
        }
        public static T AddNode <T>(Vector2 position, BehaviorTrees bt)
        {
            if (bt == null)
            {
                Debug.LogWarning("Can't add a node to the behavior trees, because the behavior trees are null.");
                return(default(T));
            }

            Node node = ScriptableObject.CreateInstance(typeof(T)) as Node;

            node.hideFlags = HideFlags.HideInHierarchy;

            node.Name     = BehaviorTreesEditorUtility.GenerateName <T>();
            node.comment  = node.Name;
            node.bt       = bt;
            bt.nodes      = ArrayUtility.Add <Node>(bt.nodes, node);
            node.position = new Rect(position.x, position.y, BehaviorTreesEditorStyles.NodeNormalWidth, BehaviorTreesEditorStyles.NodeNormalHeight);

            if (EditorUtility.IsPersistent(bt))
            {
                AssetDatabase.AddObjectToAsset(node, bt);
            }

            if (node is BehaviorTrees)
            {
                node.position.width  = 150f;
                node.position.height = 45f;

                Root root = BehaviorTreesEditorUtility.AddNode <Root>(BehaviorTreesEditor.center, node as BehaviorTrees);
                root.Name = "Root";
            }
            else if (node is Wait)
            {
                Wait wait = node as Wait;
                wait.tick    = 0.1f;
                wait.comment = "Wait: 0.1s";
            }

            AssetCreator.SaveAIAsset();
            return((T)(object)node);
        }
Example #10
0
        private void CreateCodePack(System.Type type)
        {
            GUILayout.Label("Selected \"" + type.Name + "\"", "LODLevelNotifyText");
            GUILayout.Space(6f);
            List <MethodInfo> decos = GetMethodInfos(type, typeof(bool));
            List <MethodInfo> servs = GetMethodInfos(type, typeof(void));
            List <MethodInfo> tasks = GetMethodInfos(type, typeof(System.IDisposable));

            tasks.AddRange(GetMethodInfos(type, typeof(IEnumerator)));
            bool created = DrawCreateButton(!(decos.Count > 0) && !(decos.Count > 0) && !(tasks.Count > 0));

            if (decos.Count > 0)
            {
                if (BehaviorTreesEditorUtility.DrawHeader("Decorators", false))
                {
                    DrawMethods(decos);
                }
            }
            if (servs.Count > 0)
            {
                if (BehaviorTreesEditorUtility.DrawHeader("Services", false))
                {
                    DrawMethods(servs);
                }
            }
            if (tasks.Count > 0)
            {
                if (BehaviorTreesEditorUtility.DrawHeader("Tasks", false))
                {
                    DrawMethods(tasks);
                }
            }
            if (created)
            {
                CreatePrefab(type, "Code Pack");
            }
        }
 public static void DeleteService(Service service)
 {
     service.parent.services = ArrayUtility.Remove <Service>(service.parent.services, service);
     BehaviorTreesEditorUtility.DestroyImmediate(service);
 }
 public static void DeleteDecorator(Decorator decorator)
 {
     decorator.parent.decorators = ArrayUtility.Remove <Decorator>(decorator.parent.decorators, decorator);
     BehaviorTreesEditorUtility.DestroyImmediate(decorator);
 }
Example #13
0
        private void ServiceContextMenu()
        {
            if (_currentEvent.type != EventType.MouseDown || _currentEvent.button != 1 || _currentEvent.clickCount != 1)
            {
                return;
            }

            Service service = MouseOverService();

            if (service == null)
            {
                return;
            }

            int currentIndex = 0;

            for (int i = 0; i < service.parent.services.Length; i++)
            {
                if (service.parent.services[i] == service)
                {
                    break;
                }
                currentIndex++;
            }
            GenericMenu menu = new GenericMenu();

            if (currentIndex > 0)
            {
                menu.AddItem(new GUIContent("Move Up"), false, delegate()
                {
                    service.parent.services = ArrayUtility.MoveItem <Service>(service.parent.services, currentIndex, currentIndex - 1);
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Move Up"));
            }

            if (currentIndex < service.parent.services.Length - 1)
            {
                menu.AddItem(new GUIContent("Move Down"), false, delegate()
                {
                    service.parent.services = ArrayUtility.MoveItem <Service>(service.parent.services, currentIndex, currentIndex + 1);
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Move Down"));
            }

            menu.AddItem(new GUIContent("Delete Service"), false, delegate()
            {
                if (_serviceSelection.Contains(service))
                {
                    _serviceSelection.Clear();
                }
                BehaviorTreesEditorUtility.DeleteService(service);
                UpdateUnitySelection();
                EditorUtility.SetDirty(BehaviorTreesEditor.active);
            });
            menu.ShowAsContext();
            UnityEngine.Event.current.Use();
        }
Example #14
0
        private void DecoratorContextMenu()
        {
            if (_currentEvent.type != EventType.MouseDown || _currentEvent.button != 1 || _currentEvent.clickCount != 1)
            {
                return;
            }

            Decorator decorator = MouseOverDecorator();

            if (decorator == null)
            {
                return;
            }

            int currentIndex = 0;

            for (int i = 0; i < decorator.parent.decorators.Length; i++)
            {
                if (decorator.parent.decorators[i] == decorator)
                {
                    break;
                }
                currentIndex++;
            }

            GenericMenu menu = new GenericMenu();

            if (currentIndex > 0)
            {
                menu.AddItem(new GUIContent("Move Up"), false, delegate()
                {
                    decorator.parent.decorators = ArrayUtility.MoveItem <Decorator>(decorator.parent.decorators, currentIndex, currentIndex - 1);
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Move Up"));
            }

            if (currentIndex < decorator.parent.decorators.Length - 1)
            {
                menu.AddItem(new GUIContent("Move Down"), false, delegate()
                {
                    decorator.parent.decorators = ArrayUtility.MoveItem <Decorator>(decorator.parent.decorators, currentIndex, currentIndex + 1);
                });
            }
            else
            {
                menu.AddDisabledItem(new GUIContent("Move Down"));
            }

            menu.AddItem(new GUIContent("Delete Decorator"), false, delegate()
            {
                if (_decoratorSelection.Contains(decorator))
                {
                    _decoratorSelection.Clear();
                }
                BehaviorTreesEditorUtility.DeleteDecorator(decorator);
                UpdateUnitySelection();
                EditorUtility.SetDirty(BehaviorTreesEditor.active);
            });
            menu.ShowAsContext();
            UnityEngine.Event.current.Use();
        }
Example #15
0
        private void NodeContextMenu()
        {
            if (_currentEvent.type != EventType.MouseDown || _currentEvent.button != 1 || _currentEvent.clickCount != 1)
            {
                return;
            }

            Node node = MouseOverNode();

            if (node == null)
            {
                return;
            }

            GenericMenu nodeMenu = new GenericMenu();

            if (!(node is Root))
            {
                nodeMenu.AddItem(new GUIContent("Add Decorator/Decorator"), false, delegate()
                {
                    BehaviorTreesEditorUtility.AddDecorator <Decorator>(node, BehaviorTreesEditor.active);
                });

                if (node is Composite)
                {
                    nodeMenu.AddItem(new GUIContent("Add Service"), false, delegate()
                    {
                        BehaviorTreesEditorUtility.AddService <Service>((Composite)node, BehaviorTreesEditor.active);
                    });
                }
                else
                {
                    nodeMenu.AddDisabledItem(new GUIContent("Add Service"));
                }
                nodeMenu.AddSeparator("/");
                nodeMenu.AddItem(new GUIContent("Delete Node"), false, delegate()
                {
                    if (_selection.Contains(node))
                    {
                        foreach (Node mNode in _selection)
                        {
                            if (!(mNode is Root))
                            {
                                BehaviorTreesEditorUtility.DeleteNode(mNode, BehaviorTreesEditor.active);
                            }
                        }
                        _selection.Clear();
                    }
                    else
                    {
                        BehaviorTreesEditorUtility.DeleteNode(node, BehaviorTreesEditor.active);
                    }
                    UpdateUnitySelection();
                    EditorUtility.SetDirty(BehaviorTreesEditor.active);
                });
                nodeMenu.AddItem(new GUIContent("Unlink Node"), false, delegate()
                {
                    if (_selection.Contains(node))
                    {
                        foreach (Node mNode in _selection)
                        {
                            if (!(mNode is Root))
                            {
                                BehaviorTreesEditorUtility.UnlinkNode(mNode, BehaviorTreesEditor.active);
                            }
                        }
                        _selection.Clear();
                    }
                    else
                    {
                        BehaviorTreesEditorUtility.UnlinkNode(node, BehaviorTreesEditor.active);
                    }
                    UpdateUnitySelection();
                    EditorUtility.SetDirty(BehaviorTreesEditor.active);
                });
            }
            else
            {
                nodeMenu.AddDisabledItem(new GUIContent("Add Decorator"));
                nodeMenu.AddDisabledItem(new GUIContent("Delete Node"));
            }
            nodeMenu.ShowAsContext();
            UnityEngine.Event.current.Use();
        }
Example #16
0
        private static void DrawNode(Node node, bool selected, Texture2D iconImage, int boxColor)
        {
            float sharedHeight = node.position.yMin + 15;

            DrawDecorators(node, ref sharedHeight);

            Rect insideMainNode = node.position;

            insideMainNode.xMin += 7;
            insideMainNode.xMax -= 7;
            insideMainNode.yMin  = sharedHeight;
            insideMainNode.yMax  = sharedHeight + 32 + GetCommentHeight(node.comment);

            GUIStyle insideMainNodeStyle = BehaviorTreesEditorStyles.GetNodeStyle(boxColor, false);

            if (EditorApplication.isPlaying)
            {
                if (node is Task)
                {
                    Task t = node as Task;
                    insideMainNodeStyle = BehaviorTreesEditorStyles.GetNodeStyle(
                        BehaviorTreesEditor.CheckThisTaskClosed(t) ? (int)NodeColor.Yellow : boxColor, BehaviorTreesEditor.CheckThisTaskClosed(t));
                }
            }
            GUI.Box(insideMainNode, "", insideMainNodeStyle);

            int?myIndex = BehaviorTreesEditorUtility.GetMyIndex(node);

            if (myIndex != null && circleIcon != null)
            {
                float circleIconWidth  = circleIcon.width * 1.1f;
                float circleIconHeight = circleIcon.height * 1.1f;
                Rect  indexRect        = new Rect(insideMainNode.xMax - circleIconWidth / 2f + 12, insideMainNode.yMin - circleIconHeight / 2f, circleIconWidth, circleIconHeight);
                GUI.DrawTexture(indexRect, circleIcon);
                indexRect.xMin -= 2;
                indexRect.yMin -= 2;
                GUI.Label(indexRect, "<color=black>" + (myIndex + 1).ToString() + "</color>", BehaviorTreesEditorStyles.nodeIndexLabel);
            }

            Rect iconRect = node.position;

            iconRect.x = node.position.xMin + 7;
            iconRect.y = sharedHeight - 1;
            GUI.Label(iconRect, iconImage);

            Rect nameRect = node.position;

            nameRect.x = node.position.xMin + 42;
            nameRect.y = sharedHeight + 5;

            GUI.Label(nameRect, "<color=white>" + node.Name + "</color>", BehaviorTreesEditorStyles.nodeBoxNameNormalStyle);

            GUIContent commentContent = new GUIContent(node.comment);
            Rect       commentRect    = node.position;

            commentRect.x     = node.position.xMin + 7;
            commentRect.y     = sharedHeight + 30;
            commentRect.width = BehaviorTreesEditorStyles.nodeBoxCommentStyle.CalcSize(commentContent).x + 10;
            GUI.Label(commentRect, "<color=white>" + node.comment + "</color>", BehaviorTreesEditorStyles.nodeBoxCommentStyle);

            sharedHeight += insideMainNode.yMax - insideMainNode.yMin + 5;
            DrawServices(node, sharedHeight);
        }