美文网首页
动画优化

动画优化

作者: AngerCow | 来源:发表于2021-01-25 17:05 被阅读0次

压缩动画精度,去除动画大小缩放曲线

static void CompressAnimationClip(AnimationClip theAnimation,bool isRemoveScale)
    {
        if (theAnimation == null) 
            return;
        string assetPath = AssetDatabase.GetAssetPath(theAnimation);
        try
        {
            if (isRemoveScale)
            {
                //去除scale曲线
                foreach (EditorCurveBinding theCurveBinding in AnimationUtility.GetCurveBindings(theAnimation))
                {
                    string name = theCurveBinding.propertyName.ToLower();
                    if (name.Contains("scale"))
                    {
                        AnimationUtility.SetEditorCurve(theAnimation, theCurveBinding, null);
                    }
                }
            }
            //浮点数精度压缩到f3
            AnimationClipCurveData[] curves = null;
            curves = AnimationUtility.GetAllCurves(theAnimation);
            Keyframe key;
            Keyframe[] keyFrames;
            for (int ii = 0; ii < curves.Length; ++ii)
            {
                AnimationClipCurveData curveDate = curves[ii];
                if (curveDate.curve == null || curveDate.curve.keys == null)
                {
                    continue;
                }
                keyFrames = curveDate.curve.keys;
                for (int i = 0; i < keyFrames.Length; i++)
                {
                    key = keyFrames[i];
                    key.value = float.Parse(key.value.ToString("f3"));
                    key.inTangent = float.Parse(key.inTangent.ToString("f3"));
                    key.outTangent = float.Parse(key.outTangent.ToString("f3"));
                    keyFrames[i] = key;
                }
                curveDate.curve.keys = keyFrames;
                theAnimation.SetCurve(curveDate.path, curveDate.type, curveDate.propertyName, curveDate.curve);
            }
        }
        catch (System.Exception e)
        {
            Debug.LogError(string.Format("CompressAnimationClip Failed !!! animationPath : {0} error: {1}", assetPath, e));
        }
    }

相关文章

网友评论

      本文标题:动画优化

      本文链接:https://www.haomeiwen.com/subject/mtdizktx.html