Skip to content

Instantly share code, notes, and snippets.

@smakhtin
Created June 12, 2012 10:20
Show Gist options
  • Save smakhtin/2916758 to your computer and use it in GitHub Desktop.
Save smakhtin/2916758 to your computer and use it in GitHub Desktop.
Create a copy of read-only mesh animation in Unity (by neodrop)
using UnityEditor;
using UnityEngine;
public class CurvesTransferer
{
const string duplicatePostfix = "_copy";
static void CopyClip(string importedPath, string copyPath)
{
AnimationClip src = AssetDatabase.LoadAssetAtPath(importedPath, typeof(AnimationClip)) as AnimationClip;
AnimationClip newClip = new AnimationClip();
newClip.name = src.name + duplicatePostfix;
AssetDatabase.CreateAsset(newClip, copyPath);
AssetDatabase.Refresh();
}
[MenuItem("Assets/Transfer Clip Curves to Copy")]
static void CopyCurvesToDuplicate()
{
// Get selected AnimationClip
AnimationClip imported = Selection.activeObject as AnimationClip;
if (imported == null)
{
Debug.Log("Selected object is not an AnimationClip");
return;
}
// Find path of copy
string importedPath = AssetDatabase.GetAssetPath(imported);
string copyPath = importedPath.Substring(0, importedPath.LastIndexOf("/"));
copyPath += "/" + imported.name + duplicatePostfix + ".anim";
CopyClip(importedPath, copyPath);
AnimationClip copy = AssetDatabase.LoadAssetAtPath(copyPath, typeof(AnimationClip)) as AnimationClip;
if (copy == null)
{
Debug.Log("No copy found at " + copyPath);
return;
}
// Copy curves from imported to copy
AnimationClipCurveData[] curveDatas = AnimationUtility.GetAllCurves(imported, true);
for (int i = 0; i < curveDatas.Length; i++)
{
AnimationUtility.SetEditorCurve(
copy,
curveDatas[i].path,
curveDatas[i].type,
curveDatas[i].propertyName,
curveDatas[i].curve
);
}
Debug.Log("Copying curves into " + copy.name + " is done");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment