Hi all,
I wrote a script to remove the Scale curves from an animation because often I don’t need them. I’m using AnimationUtility GetAllCurves and SetCurve to make a new animation with only the curves I want to keep.
The generated AnimationClip does have fewer curves, but the file is about 4x larger! I suspect this is because I’m no longer benefitting from the Model Importer’s Animation Compression and Keyframe Reduction.
How can I reduce the file size and take advantage of these optimizations??
Script:
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
public class RemoveAnimScaleCurves : ScriptableObject
{
[MenuItem("Util/Remove Anim Scale Curves")]
public static void CleanCurves() {
foreach ( UnityEngine.Object o in Selection.objects) {
if (o != null && o is AnimationClip) {
AnimationClip selectedClip = (AnimationClip) o;
AnimationClipCurveData[] curves = AnimationUtility.GetAllCurves(selectedClip);
AnimationClip newClip = new AnimationClip();
foreach (AnimationClipCurveData curve in curves) {
// Weed out Scale
if ( ! curve.propertyName.Contains("Scale") && curve.curve.keys.Length > 0 )
newClip.SetCurve( curve.path, curve.type, curve.propertyName, curve.curve );
}
newClip.name = selectedClip.name + "_NoScale";
newClip.wrapMode = selectedClip.wrapMode;
Debug.Log ( "Old Clip " + selectedClip.name + " has " + curves.Length + " curves" );
Debug.Log ( "New Clip " + newClip.name + " has " + AnimationUtility.GetAllCurves(newClip).Length + " curves" );
SaveAnimationClip( newClip );
AssetDatabase.SaveAssets();
}
}
}
public static void SaveAnimationClip(AnimationClip a) {
if(!Directory.Exists("Assets/Animations Generated")) {
AssetDatabase.CreateFolder("Assets", "Animations Generated");
AssetDatabase.Refresh();
}
string path = AssetDatabase.GenerateUniqueAssetPath("Assets/Animations Generated/"+a.name+".asset");
AssetDatabase.CreateAsset(a, path);
}
}