Example #1
0
        /// <summary>
        /// Remove the suplied branch from the tree.
        /// <param name="branch">The node to be removed.</param>
        /// <returns>True if the node was successfully removed; false otherwise.</returns>
        /// </summary>
        public static bool RemoveBranch(BranchNode branch)
        {
            // The Branch is not a decorator
            if (branch != null && branch.tree != null)
            {
                // Gets parent's children and node's id
                var          parent   = branch.branch;
                ActionNode[] children = branch.children;
                var          tree     = branch.tree;

                // The parent is a decorator or null (node is root) and the branch has more than one child?
                if (children.Length >= 2 && (parent == null || parent is DecoratorNode) || parent == null)
                {
                    EditorApplication.Beep();
                    return(false);
                }

                // Register Undo
                #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2
                Undo.RegisterUndo(branch.tree, "Remove Branch");
                #else
                Undo.RecordObject(branch.tree, "Remove Branch");
                #endif

                // Removes children from branch and adds to parent
                if (parent != null)
                {
                    // Get the branch index
                    int branchIndex = (new List <ActionNode>(parent.children)).IndexOf(branch);
                    parent.Remove(branch);

                    for (int i = 0; i < children.Length; i++)
                    {
                        branch.Remove(children[i]);
                        parent.Insert(branchIndex++, children[i]);
                    }

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

                // Removes node from tree
                tree.RemoveNode(branch, false);

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

                return(true);
            }
            return(false);
        }