- Home /
Removing scale curves from animation
Hi, I was reading in doc, that unused scale curves in animations can have bad impact on the performance. So I decided to try removing these curves. I went through many articles and I came with this script (see below) which is called in OnPostprocessModel(GameObject g) in class derived from AssetPostprocessor.
void Apply(GameObject g)
{
List<AnimationClip> animationClipList = new List<AnimationClip>(AnimationUtility.GetAnimationClips(g));
if (animationClipList.Count == 0) {
AnimationClip[] objectList = UnityEngine.Object.FindObjectsOfType (typeof(AnimationClip)) as AnimationClip[];
animationClipList.AddRange(objectList);
}
int count = 0;
foreach (AnimationClip theAnimation in animationClipList)
{
foreach (AnimationClipCurveData theCurve in AnimationUtility.GetAllCurves(theAnimation))
{
string name = theCurve.propertyName.ToLower();
if (name.Contains("scale"))
{
for (int i = theCurve.curve.keys.Length - 1; i >= 0; i--) {
theCurve.curve.RemoveKey(i);
}
string propertyName = theCurve.propertyName;
// we can't delete "*.x", e.g. m_LocalScale.x - but only "*", e.g. m_LocalScale
if (propertyName.IndexOf(".") > 0) {
propertyName = propertyName.Substring(0, propertyName.IndexOf("."));
}
Debug.Log(string.Format("Fixing: {0} - {1}", theCurve.path, propertyName));
theAnimation.SetCurve(theCurve.path, theCurve.type, propertyName, null);
count++;
}
}
}
int checkCount = 0;
foreach (AnimationClip theAnimation in animationClipList)
{
foreach (AnimationClipCurveData theCurve in AnimationUtility.GetAllCurves(theAnimation))
{
string name = theCurve.propertyName.ToLower();
if (name.Contains("scale"))
{
checkCount++;
}
}
}
if (count > 0)
{
Debug.Log("Total number of removed curves is " + count + ". GO name: " + g.name);
Debug.Log("Number of remaining scale curves is " + checkCount);
}
}
But I'm not sure if it's actually working. I tried to reimport all animation, but I can't see any changes in any file. Neither in meta files. My check function tells me that there is no more scale curves (variable "checkCount"), but I'm not sure if I can trust it :-). I can't see any impact on the performance in game as well. Does anybody have same experiences? Thank you in advance for your answers!
PS: My animations are imported from fbx files.
$$anonymous$$aybe there's no need to Remove$$anonymous$$ey() because whole curves are deleted.
Answer by aniv · Apr 10, 2014 at 11:26 AM
I tried your script, it works for me. You can check if there are scale curves using animation window when you click on object that has Mecanim controller with imported animations assigned to states. Probably you have other things that influence the performance much more.
EDIT: This has same function just uses new API:
void Apply(GameObject g)
{
List<AnimationClip> animationClipList = new List<AnimationClip>(AnimationUtility.GetAnimationClips(g));
if (animationClipList.Count == 0) {
AnimationClip[] objectList = UnityEngine.Object.FindObjectsOfType (typeof(AnimationClip)) as AnimationClip[];
animationClipList.AddRange(objectList);
}
foreach (AnimationClip theAnimation in animationClipList)
{
foreach (EditorCurveBinding theCurveBinding in AnimationUtility.GetCurveBindings(theAnimation))
{
string name = theCurveBinding.propertyName.ToLower();
if (name.Contains("scale"))
{
AnimationUtility.SetEditorCurve(theAnimation, theCurveBinding, null);
}
}
}
}
I'd like to use the new APIs, however they run much slower and result in larger .anim files than the original.
Your answer
Follow this Question
Related Questions
Imported FBX animations with scales problem 0 Answers
Model shrinking during animation 1 Answer
Scale animation with model 1 Answer
Adding animation clips via script 2 Answers
Disabling scaling in unity animations? 0 Answers