[Unity-26] AnimationClip压缩-动画文件压缩

动画压缩方法

一、常用方法

1. Rig->Animation Type:改为Generic

2. Animations->Anim.Compression:Optimal

二、高级方法

1. 去掉AnimationClip中的无效曲线:例如ScaleCurve

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class RemoveCurve : AssetPostprocessor
{

    void OnPostprocessModel(GameObject g)
    {
        Apply(g);
    }

    void Apply(GameObject g)
    {
        List animationClipList = new List(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);
                }
            }
        }
    }
}

2. 压缩AnimationClip文件float的精度


public static bool CompressAnimationClip(Object o)
    {
        string animationPath = AssetDatabase.GetAssetPath(o);
        try
        {
            //AnimationClip clip = GameObject.Instantiate(o) as AnimationClip;
            AnimationClip clip = o as AnimationClip;
            AnimationClipCurveData[] curves = null;
            curves = AnimationUtility.GetAllCurves(clip);
            Keyframe key;
            Keyframe[] keyFrames;
            for (int ii = 0; ii < curves.Length; ++ii)
            {
                AnimationClipCurveData curveDate = curves[ii];
                if (curveDate.curve == null || curveDate.curve.keys == null)
                {
                    //Debug.LogWarning(string.Format("AnimationClipCurveData {0} don't have curve; Animation name {1} ", curveDate, animationPath));
                    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;
                clip.SetCurve(curveDate.path, curveDate.type, curveDate.propertyName, curveDate.curve);
            }
            //AssetDatabase.CreateAsset(clip, animationPath);
            Debug.Log(string.Format("  CompressAnimationClip {0} Success !!!", animationPath));
            return true;
        }
        catch(Exception e)
        {
            Debug.LogError(string.Format("CompressAnimationClip Failed !!! animationPath : {0} error: {1}", animationPath, e));
            return false;
        } 
    }

3. 简化骨骼名字

unity的动画数据存储的时候按每个骨骼节点的轨道拆分存储的,每个骨骼存储的是从根节点到这个骨骼节点的hierarchy路径,字符串的,骨骼多了之后这部分字符串不少。如果把所有骨骼名字都简化,这里可以省不少内存
动画文件存储内容

你可能感兴趣的:(Unity游戏开发)