Unity之如何从fbx提取Animation clip文件

从 fbx 中提取 clip 文件,选中 clip 按 Ctrl + D 就可以实现。

但有时候需要工具来做这件事情,

提取的同时对 clip 进行了压缩,并且去除了scale

代码如下:

 1     static AnimationClip GenerateAnimClip(string fbx, string savePath)
 2     {
 3         AnimationClip src = AssetDatabase.LoadAssetAtPath<AnimationClip>(fbx);
 4         AnimationClip temp = new AnimationClip();
 5         EditorUtility.CopySerialized(src, temp);
 6         CompressAnimationClip(temp);                // 压缩 anim clip
 7         RemoveAnimClipScale(temp);                  // 去除 scale
 8         CreateDirectory(savePath);
 9         AssetDatabase.CreateAsset(temp, savePath);
10         EditorUtility.SetDirty(temp);
11         return temp;
12     }
13 
14     static void RemoveAnimClipScale(AnimationClip clip)
15     {
16         foreach (EditorCurveBinding theCurveBinding in AnimationUtility.GetCurveBindings(clip))
17         {
18             string name = theCurveBinding.propertyName.ToLower();
19             if (name.Contains("scale"))
20             {
21                 AnimationUtility.SetEditorCurve(clip, theCurveBinding, null);
22             }
23         }
24     }
25 
26     public static bool CompressAnimationClip(AnimationClip clip)
27     {
28         try
29         {
30             AnimationClipCurveData[] curves = AnimationUtility.GetAllCurves(clip);
31             Keyframe key;
32             Keyframe[] keyFrames;
33             for (int ii = 0; ii < curves.Length; ++ii)
34             {
35                 AnimationClipCurveData curveDate = curves[ii];
36                 if (curveDate.curve == null || curveDate.curve.keys == null)
37                 {
38                     //Debug.LogWarning(string.Format("AnimationClipCurveData {0} don't have curve; Animation name {1} ", curveDate, animationPath));
39                     continue;
40                 }
41                 keyFrames = curveDate.curve.keys;
42                 for (int i = 0; i < keyFrames.Length; i++)
43                 {
44                     key = keyFrames[i];
45                     key.value = float.Parse(key.value.ToString("f3"));
46                     key.inTangent = float.Parse(key.inTangent.ToString("f3"));
47                     key.outTangent = float.Parse(key.outTangent.ToString("f3"));
48                     keyFrames[i] = key;
49                 }
50                 curveDate.curve.keys = keyFrames;
51                 clip.SetCurve(curveDate.path, curveDate.type, curveDate.propertyName, curveDate.curve);
52             }
53             //AssetDatabase.CreateAsset(clip, animationPath);
54             Debug.Log(string.Format("  CompressAnimationClip {0} Success !!!", clip.name));
55             return true;
56         }
57         catch (Exception e)
58         {
59             Debug.LogError(string.Format("CompressAnimationClip Failed !!! animationPath : {0} error: {1}", clip.name, e));
60             return false;
61         }
62     }

转载请注明出处:https://www.cnblogs.com/jietian331/p/7264367.html

posted @ 2017-07-31 17:47  孤独の巡礼  阅读(6643)  评论(3编辑  收藏  举报