/// <summary> /// Adds a new node to the parent, automatically handles undo, dirty flag and save node. /// <param name="parent">The branch to add the child.</param> /// <param name="childType">The type of the new node.</param> /// <returns>The new node.</returns> /// </summary> public static ActionNode AddNode(BranchNode parent, System.Type childType) { // Validate parameters if (parent != null && parent.tree != null && childType != null) { // Register Undo #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2 Undo.RegisterUndo(parent.tree, "Add New Node"); #else Undo.RecordObject(parent.tree, "Add New Node"); #endif var newNode = parent.tree.AddNode(childType); if (newNode != null) { // Adds new node as child of parent parent.Add(newNode); // Call OnValidate on the parent parent.OnValidate(); // Saves node and sets dirty flag StateUtility.SetDirty(parent.tree); return(newNode); } } return(null); }
/// <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> /// Inserts a new node to the supplied branch, automatically handles undo, dirty flag and save node. /// <param name="node">The branch to add a new node.</param> /// <param name="newNodePosition">Move the node to the position of this node.</param> /// <param name="branch">The branch to drop the node or null.</param> /// </summary> public static bool MoveNode(ActionNode node, ActionNode newNodePosition, BranchNode branch) { // Validate parameters if (node != null && node.tree != null) { // Get the tree var tree = node.tree; // The node does not belongs to the tree? if (!tree.GetNodes().Contains(node)) { return(false); } // Register Undo #if UNITY_4_0_0 || UNITY_4_1 || UNITY_4_2 Undo.RegisterUndo(tree, "Move Node"); #else Undo.RecordObject(tree, "Move Node"); #endif // The node will be a root node? if (branch == null) { // Remove from old branch if (node.branch != null) { BranchNode oldBranch = node.branch; node.branch.Remove(node); // Call OnValidate on old branch oldBranch.OnValidate(); } if (newNodePosition == null) { var newIndex = node.tree.GetNodes().Count - 1; node.tree.MoveNode(node.GetIndex(), newIndex); } else { var newIndex = newNodePosition.root.GetIndex(); node.tree.MoveNode(node.GetIndex(), newIndex); } } // The new node position is null? else if (newNodePosition == null) { // node.branch = branch; // Store old branch var oldBranch = node.branch; // Remove from old branch if (oldBranch != null) { oldBranch.Remove(node); } // Add to drop if (!branch.Add(node)) { // Restore old branch if (oldBranch != null) { oldBranch.Add(node); } return(false); } // Call OnValidate on branches branch.OnValidate(); if (oldBranch != null && oldBranch != branch) { oldBranch.OnValidate(); } node.tree.HierarchyChanged(); } else { // Cache the oldBranch BranchNode oldBranch = node.branch; // Get index var index = -1; var children = branch.children; for (int i = 0; i < children.Length; i++) { if (children[i] == newNodePosition) { index = i; break; } } // The index is invalid? if (index < 0 || !branch.Insert(index, node)) { return(false); } else { // Call OnValidate on the branches if (oldBranch != null) { oldBranch.OnValidate(); } branch.OnValidate(); node.tree.HierarchyChanged(); } } // Save move opration StateUtility.SetDirty(tree); return(true); } return(false); }