public static void Create_C(GameObject obj, string path, bool setloopByName = true, CurveDataFilterDelegate IsFiltered = null) { //GameObject obj = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject; if (obj == null) { Debug.LogError("nullObj: " + path); } Animation ani = obj.GetComponent<Animation>(); if (ani == null) { Debug.Log("No animtion on obj " + path); return; } AnimationClip[] clips = new AnimationClip[ani.GetClipCount()]; AnimationClip[] duplicatedClips = new AnimationClip[clips.Length]; int index = 0; foreach (AnimationState s in ani) { clips[index] = s.clip; duplicatedClips[index] = AnimationImportTools.DuplicateClip(clips[index], IsFiltered); if (setloopByName && IsLoopAnimation(s.clip.name)) { duplicatedClips[index].wrapMode = WrapMode.Loop; } index++; } string absoluteRootFolder = System.IO.Directory.GetParent(Application.dataPath) + "/"; string oriAniFolder = System.IO.Path.GetDirectoryName(path); string duplicatedAniFolder = System.IO.Path.GetDirectoryName(path) + "/backup"; string absoluteDuplicateFolder = System.IO.Path.Combine(absoluteRootFolder, duplicatedAniFolder); if (!System.IO.Directory.Exists(absoluteDuplicateFolder)) { System.IO.Directory.CreateDirectory(absoluteDuplicateFolder); } string[] anisPath = new string[clips.Length]; string[] oriAniPath = new string[clips.Length]; for (int i = 0; i < clips.Length; i++) { if (clips[i] != null) { string fullPath = System.IO.Path.Combine(duplicatedAniFolder, clips[i].name + "_c.anim"); AssetDatabase.CreateAsset(duplicatedClips[i], fullPath); anisPath[i] = System.IO.Path.Combine(duplicatedAniFolder, clips[i].name); oriAniPath[i] = System.IO.Path.Combine(oriAniFolder, clips[i].name); } else { Debug.LogWarning("null clip @ " + path + " " + i); } } for (int i = 0; i < anisPath.Length; i++) { string _cFileName = System.IO.Path.Combine(absoluteRootFolder, oriAniPath[i] + "_c.anim"); string _dFileName = System.IO.Path.Combine(absoluteRootFolder, anisPath[i] + "_c.anim"); bool _dExist = System.IO.File.Exists(_dFileName); if (!_dExist) continue; FileUtil.ReplaceFile(_dFileName, _cFileName); System.IO.File.Delete(_dFileName); } }
public static AnimationClip DuplicateClip(AnimationClip clip, CurveDataFilterDelegate IsFiltered = null) { AnimationClip copiedClip = new AnimationClip(); EditorUtility.CopySerialized(clip, copiedClip); foreach (EditorCurveBinding binding in AnimationUtility.GetCurveBindings(copiedClip)) { bool skip = false; //filter excluded object if (IsExcludedAnimationObject(binding)) { skip |= true; } //filter scale if (IsFiltered != null && IsFiltered(binding)) { skip |= true; } if (skip) { AnimationUtility.SetEditorCurve(copiedClip, binding, null); } } return copiedClip; }