// Taken from here: https://gist.github.com/phosphoer/93ca8dcbf925fc006e4e9f6b799c13b0 private static BlendTree CloneBlendTree(BlendTree newTree, BlendTree oldTree) { // Create a child tree in the destination parent, this seems to be the only way to correctly // add a child tree as opposed to AddChild(motion) BlendTree pastedTree = newTree is null ? new BlendTree() : newTree.CreateBlendTreeChild(newTree.maxThreshold); pastedTree.name = oldTree.name; pastedTree.blendType = oldTree.blendType; pastedTree.blendParameter = oldTree.blendParameter; pastedTree.blendParameterY = oldTree.blendParameterY; pastedTree.minThreshold = oldTree.minThreshold; pastedTree.maxThreshold = oldTree.maxThreshold; pastedTree.useAutomaticThresholds = oldTree.useAutomaticThresholds; // Recursively duplicate the tree structure // Motions can be directly added as references while trees must be recursively to avoid accidental sharing foreach (var child in oldTree.children) { if (child.motion is BlendTree tree) { var childTree = CloneBlendTree(pastedTree, tree); // need to save the blend tree into the animator childTree.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(childTree, _assetPath); } else { var children = pastedTree.children; ArrayUtility.Add(ref children, child); pastedTree.children = children; } } return(pastedTree); }