Unity之如何去除动画scale

代码如下:

  1 using System;
  2 using System.Collections.Generic;
  3 using UnityEditor;
  4 using UnityEngine;
  5 
  6 
  7 public class RemoveAminCurve : AssetPostprocessor
  8 {
  9     //模型导入之前调用 
 10     public void OnPreprocessModel()
 11     {
 12         if (assetPath.StartsWith("Assets/FBX/Animation/HaiDaoHunLi/Animation"))
 13         {
 14             Debug.Log("OnPreprocessModel=" + this.assetPath);
 15         }
 16     }
 17 
 18     //模型导入之前调用 
 19     public void OnPostprocessModel(GameObject go)
 20     {
 21         if (assetPath.ToLower().Contains("anim"))
 22         {
 23             Debug.Log("OnPostprocessModel = " + assetPath);
 24             Apply(go);
 25         }
 26         
 27     } 
 28 
 29     //[MenuItem("BuildTool/RemoveSelectAnimCurve")]
 30     //static void RemoveSelectAnimCurve()
 31     //{
 32     //    //foreach (UnityEngine.GameObject o in Selection.GetFiltered(typeof(UnityEngine.GameObject), SelectionMode.DeepAssets))
 33     //    //{
 34     //    //    Debug.Log(o.name);
 35     //    //    Apply(o);
 36     //    //}
 37 
 38     //    foreach (string guid in Selection.assetGUIDs)
 39     //    {
 40     //        string assetPath = AssetDatabase.GUIDToAssetPath(guid);
 41     //        //Object ob = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Object));
 42     //        UnityEngine.Object[] objs = AssetDatabase.LoadAllAssetRepresentationsAtPath(assetPath);
 43     //        bool isDirty = false;
 44     //        for (int i = 0; i < objs.Length; i++)
 45     //        {
 46     //            if (objs[i] is AnimationClip)
 47     //            {
 48     //                isDirty = true;
 49     //                Apply(objs[i] as AnimationClip);
 50     //                CompressAnimationClip(objs[i]);
 51     //            }
 52     //        }
 53     //        //if (isDirty)
 54     //        //    AssetDatabase.ImportAsset(assetPath);
 55     //        //EditorUtility.SetDirty(ob);
 56     //    }
 57     //}
 58     //static void Apply(AnimationClip clip)
 59     //{
 60     //    Debug.Log(clip.name);
 61     //    foreach (EditorCurveBinding theCurveBinding in AnimationUtility.GetCurveBindings(clip))
 62     //    {
 63     //        string name = theCurveBinding.propertyName.ToLower();
 64     //        if (name.Contains("scale"))
 65     //        {
 66     //            AnimationUtility.SetEditorCurve(clip, theCurveBinding, null);
 67     //        }
 68     //    }
 69     //}
 70     static void Apply(GameObject g)
 71     {
 72         List<AnimationClip> animationClipList = new List<AnimationClip>(AnimationUtility.GetAnimationClips(g));
 73 
 74         if (animationClipList.Count == 0)
 75         {
 76             AnimationClip[] objectList = UnityEngine.Object.FindObjectsOfType(typeof(AnimationClip)) as AnimationClip[];
 77             animationClipList.AddRange(objectList);
 78         }
 79         foreach (AnimationClip theAnimation in animationClipList)
 80         {
 81             CompressAnimationClip(theAnimation);
 82             foreach (EditorCurveBinding theCurveBinding in AnimationUtility.GetCurveBindings(theAnimation))
 83             {
 84                 string name = theCurveBinding.propertyName.ToLower();
 85                 if (name.Contains("scale"))
 86                 {
 87                     AnimationUtility.SetEditorCurve(theAnimation, theCurveBinding, null);
 88                 }
 89             }
 90         }
 91     }
 92     public static bool CompressAnimationClip(AnimationClip clip)
 93     {
 94         try
 95         {
 96             AnimationClipCurveData[] curves = null;
 97             curves = AnimationUtility.GetAllCurves(clip);
 98             Keyframe key;
 99             Keyframe[] keyFrames;
100             for (int ii = 0; ii < curves.Length; ++ii)
101             {
102                 AnimationClipCurveData curveDate = curves[ii];
103                 if (curveDate.curve == null || curveDate.curve.keys == null)
104                 {
105                     //Debug.LogWarning(string.Format("AnimationClipCurveData {0} don't have curve; Animation name {1} ", curveDate, animationPath));
106                     continue;
107                 }
108                 keyFrames = curveDate.curve.keys;
109                 for (int i = 0; i < keyFrames.Length; i++)
110                 {
111                     key = keyFrames[i];
112                     key.value = float.Parse(key.value.ToString("f3"));
113                     key.inTangent = float.Parse(key.inTangent.ToString("f3"));
114                     key.outTangent = float.Parse(key.outTangent.ToString("f3"));
115                     keyFrames[i] = key;
116                 }
117                 curveDate.curve.keys = keyFrames;
118                 clip.SetCurve(curveDate.path, curveDate.type, curveDate.propertyName, curveDate.curve);
119             }
120             //AssetDatabase.CreateAsset(clip, animationPath);
121             Debug.Log(string.Format("  CompressAnimationClip {0} Success !!!", clip.name));
122             return true;
123         }
124         catch (Exception e)
125         {
126             Debug.LogError(string.Format("CompressAnimationClip Failed !!! animationPath : {0} error: {1}", clip.name, e));
127             return false;
128         }
129     }
130 
131     public static bool CompressAnimationClip(UnityEngine.Object o)
132     {
133         string animationPath = AssetDatabase.GetAssetPath(o);
134         try
135         {
136             //AnimationClip clip = GameObject.Instantiate(o) as AnimationClip;
137             AnimationClip clip = o as AnimationClip;
138             AnimationClipCurveData[] curves = null;
139             curves = AnimationUtility.GetAllCurves(clip);
140             Keyframe key;
141             Keyframe[] keyFrames;
142             for (int ii = 0; ii < curves.Length; ++ii)
143             {
144                 AnimationClipCurveData curveDate = curves[ii];
145                 if (curveDate.curve == null || curveDate.curve.keys == null)
146                 {
147                     //Debug.LogWarning(string.Format("AnimationClipCurveData {0} don't have curve; Animation name {1} ", curveDate, animationPath));
148                     continue;
149                 }
150                 keyFrames = curveDate.curve.keys;
151                 for (int i = 0; i < keyFrames.Length; i++)
152                 {
153                     key = keyFrames[i];
154                     key.value = float.Parse(key.value.ToString("f3"));
155                     key.inTangent = float.Parse(key.inTangent.ToString("f3"));
156                     key.outTangent = float.Parse(key.outTangent.ToString("f3"));
157                     keyFrames[i] = key;
158                 }
159                 curveDate.curve.keys = keyFrames;
160                 clip.SetCurve(curveDate.path, curveDate.type, curveDate.propertyName, curveDate.curve);
161             }
162             //AssetDatabase.CreateAsset(clip, animationPath);
163             Debug.Log(string.Format("  CompressAnimationClip {0} Success !!!", animationPath));
164             return true;
165         }
166         catch (Exception e)
167         {
168             Debug.LogError(string.Format("CompressAnimationClip Failed !!! animationPath : {0} error: {1}", animationPath, e));
169             return false;
170         }
171     }
172 }

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

原文地址:https://www.cnblogs.com/jietian331/p/14310060.html