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);
        }
        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);
        }
Пример #3
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();
            }
        }
Пример #4
0
 private void SelectSave()
 {
     if (GUILayout.Button("[Save]", EditorStyles.toolbarButton, GUILayout.Width(100)))
     {
         AssetCreator.SaveAIAsset();
     }
 }
        public static void DestroyImmediate(ScriptableObject obj)
        {
            if (obj == null)
            {
                return;
            }

            UnityEngine.Object.DestroyImmediate(obj, true);
            AssetCreator.SaveAIAsset();
        }
Пример #6
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();
            }
        }
Пример #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();
        }
Пример #8
0
        private void AddTransition(Node fromNode, Node toNode)
        {
            if (fromNode is Root && !(toNode is Composite))
            {
                return;
            }

            if (fromNode is Root)
            {
                for (int i = 0; i < fromNode.childNodes.Length; i++)
                {
                    fromNode.childNodes[i].parentNode = null;
                }
                fromNode.childNodes = null;
            }

            toNode.parentNode   = fromNode;
            fromNode.childNodes = ArrayUtility.Add <Node>(fromNode.childNodes, toNode);
            AssetCreator.SaveAIAsset();
        }
        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);
        }
Пример #10
0
        private void CreatePrefab(System.Type type, string name)
        {
            string path = EditorUtility.SaveFilePanelInProject("Save As", "New " + name + ".prefab", "prefab", "Save " + name + " prefab as...", "Assets/");

            if (!string.IsNullOrEmpty(path))
            {
                GameObject go = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;

                Object prefab     = (go != null) ? go : PrefabUtility.CreateEmptyPrefab(path);
                string scriptName = path.Replace(".prefab", "");
                scriptName = scriptName.Substring(path.LastIndexOfAny(new char[] { '/', '\\' }) + 1);
                go         = new GameObject(scriptName);
                go.AddComponent(type);

                PrefabUtility.ReplacePrefab(go, prefab);
                DestroyImmediate(go);
                AssetCreator.SaveAIAsset();
                //AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport);

                go = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
                Selection.activeGameObject = go;
            }
        }