删减动画精度,以优化动画文件,网上已经不少文章了。写这篇时因为没有看到(可能有我没看到)有人写导入动画时自动优化,比较简单直接上代码
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class RemoveCurve : AssetPostprocessor
{
// 导入模型后触发
void OnPostprocessModel(GameObject g)
{
ApplyReduceFloat(g);
}
// 导入时自动优化动画精度
void ApplyReduceFloat(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)
{
var curves = AnimationUtility.GetCurveBindings(theAnimation);
foreach (var curveDate in curves)
{
var curve = AnimationUtility.GetEditorCurve(theAnimation, curveDate);
if (curve == null || curve.keys == null)
{
continue;
}
var keyFrames = curve.keys;
for (var i = 0; i < keyFrames.Length; i++)
{
var key = keyFrames[i];
key.value = float.Parse(key.value.ToString("f3")); // 优化到3位小数,可根据自身需求调整
key.inTangent = float.Parse(key.inTangent.ToString("f3"));
key.outTangent = float.Parse(key.outTangent.ToString("f3"));
keyFrames[i] = key;
}
curve.keys = keyFrames;
theAnimation.SetCurve(curveDate.path, curveDate.type, curveDate.propertyName, curve);
}
}
}
}