Exemple #1
0
        /// <summary>
        /// Adds a new node to the tree, automatically handles undo, dirty flag and save node.
        /// <param name="tree">The tree to add a new node.</param>
        /// <param name="nodeType">The type of the new node.</param>
        /// <returns>The new node.</returns>
        /// </summary>
        public static ActionNode AddNode(InternalBehaviourTree tree, System.Type nodeType)
        {
            // Validate parameters
            if (tree != null && nodeType != null && nodeType.IsSubclassOf(typeof(ActionNode)) && !nodeType.IsAbstract)
            {
                // Register Undo
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                Undo.RegisterUndo(tree, "Add New Node");
                #else
                Undo.RecordObject(tree, "Add New Node");
                #endif

                // Create new node
                var newNode = tree.AddNode(nodeType);

                if (newNode != null)
                {
                    // Saves node and sets dirty flag
                    StateUtility.SetDirty(tree);
                    return(newNode);
                }
            }

            return(null);
        }
        /// <summary>
        /// A Unity callback called when the object is loaded.
        /// </summary>
        void OnEnable()
        {
            m_Tree = target as InternalBehaviourTree;
            BehaviourWindow.activeNodeChanged += this.ActiveNodeChanged;

            m_StatesEditor = new ParentStatesEditor(m_Tree, serializedObject.FindProperty("m_Position"));

            // Try to create an editor for the current active node
            this.ActiveNodeChanged();
        }
        public static int GetBehaviourTreeDepth(this InternalStateBehaviour state)
        {
            int depth = 0;
            InternalBehaviourTree tree = state.tree;

            while (tree != null)
            {
                tree = tree.tree;
                depth++;
            }
            return(depth);
        }
Exemple #4
0
    private void SearchInTree(InternalBehaviourTree tree)
    {
        var nodes = tree.GetNodes();

        foreach (var node in nodes)
        {
            node.IsMatchSearch = false;
            if (node.GetType().Name.Equals(m_searchNode))
            {
                node.IsMatchSearch = true;
            }
        }
    }
Exemple #5
0
        /// <summary>
        /// Paste the node in BehaviourTreeUtility.nodeToPaste in the supplied tree.
        /// <param name="tree">The target tree.</param>
        /// <param name="parent">Optional parent to paste the node; or null to paste as a root node.</param>
        /// <returns>The pasted node.</returns>
        /// </summary>
        public static ActionNode PasteNode(InternalBehaviourTree tree, BranchNode parent = null)
        {
            // Get the node to be pasted
            var node = BehaviourTreeUtility.nodeToPaste;

            // Validate parameters
            if (node != null && tree != null)
            {
                // Register Undo
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                Undo.RegisterUndo(tree, "Paste Node");
                #else
                Undo.RecordObject(tree, "Paste Node");
                #endif

                var newNode = node.Copy(tree);

                if (newNode != null)
                {
                    // Add to parent branch?
                    if (parent != null)
                    {
                        parent.Add(newNode);

                        // Call OnValidate on the parent
                        parent.OnValidate();
                    }

                    // Saves node and sets dirty flag
                    StateUtility.SetDirty(tree);

                    // Reload tree to update variables
                    tree.LoadNodes();
                }
                return(newNode);
            }
            return(null);
        }
        /// <summary>
        /// Draws the script details in the gui.
        /// <param name="script">The script to be drawn.</param>
        /// </summary>
        void DrawScriptDetail(Script script)
        {
            GUILayout.BeginVertical(s_Styles.scriptDetailBox);

            // Draw disabled inspector
            if (m_NodeEditor != null && m_SelectedNodeSample != null)
            {
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                EditorGUIUtility.LookLikeInspector();
                #endif

                // Store gui enabled
                var guiEnabled = GUI.enabled;
                GUI.enabled = false;

                GUILayout.Space(-1f);
                m_NodeEditor.DrawNode(m_SelectedNodeSample);
                GUILayout.Space(8f);

                // Restore gui enabled
                GUI.enabled = guiEnabled;

                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                EditorGUIUtility.LookLikeControls();
                #endif
            }

            // Draws description?
            if (script.description != string.Empty)
            {
                EditorGUILayout.LabelField(string.Empty, "Description: " + script.description, s_Styles.description);
            }

            // Draw parent
            // Update active node
            var activeNode = BehaviourWindow.activeNode;
            UpdateActiveNode(activeNode);

            // Add to a tree
            InternalBehaviourTree activeTree = BehaviourWindow.activeTree;

            if (activeTree != null)
            {
                GUILayout.BeginHorizontal();
                EditorGUILayout.PrefixLabel("Parent");
                if (activeNode != null && m_ActiveNodeType != null)
                {
                    EditorGUILayout.LabelField(new GUIContent(activeNode.name + m_ActiveNodeTypeName, m_ActiveNodeIcon, m_ActiveNodeInfo.description), s_Styles.titleText);
                }
                else
                {
                    EditorGUILayout.LabelField("null", s_Styles.errorLabel);
                }
                GUILayout.EndHorizontal();

                var guiEnabled2  = GUI.enabled;
                var activeBranch = BehaviourWindow.activeNode as BranchNode;
                GUI.enabled = activeTree != null;
                if (GUILayout.Button(activeBranch != null ? "Add as Child of " + activeBranch.name : "Add as Root", GUILayout.ExpandWidth(false)))
                {
                    ActionNode newNode = null;
                    if (activeBranch != null)
                    {
                        newNode = BehaviourTreeUtility.AddNode(activeBranch, script.type);
                    }
                    else
                    {
                        newNode = BehaviourTreeUtility.AddNode(activeTree, script.type);
                    }

                    // Select the new node
                    if (newNode != null)
                    {
                        BehaviourWindow.activeNodeID = newNode.instanceID;
                    }
                }
                GUI.enabled = guiEnabled2;
            }
            else
            {
                // Add to an ActionState
                var actionState = BehaviourWindow.activeState as InternalActionState;

                if (actionState != null && GUILayout.Button("Add Node", GUILayout.ExpandWidth(false)))
                {
                    ActionNode newNode = ActionStateUtility.AddNode(actionState, script.type);
                    // Select the new node
                    if (newNode != null)
                    {
                        BehaviourWindow.activeNodeID = newNode.instanceID;
                    }
                }
            }

            GUILayout.EndVertical();

            GUILayout.Space(18f);

            // Workaround to avoid to close the GUIPropertyField on click
            if (Event.current.type == EventType.Repaint)
            {
                m_LastRect = GUILayoutUtility.GetLastRect();
            }
            if (Event.current.type == EventType.MouseDown && m_LastRect.Contains(Event.current.mousePosition))
            {
                Event.current.Use();
            }
        }