从模型中抽取动画文件

  由于模型是由第三方的软件制作的,用unity不能直接编辑模型里的动画文件(read-ony),比如为动画绑定事件,所以要把模型中的动画文件抽取出来,这样文件是可写的了。抽取动画文件的脚本非本人所写,贴在此处大家分享。---unity3d

 1 using UnityEditor;
 2 using UnityEngine;
 3 using System.IO;
 4 
 5 public class CurvesTransferer
 6 {
 7     [MenuItem("Character Generator/Transfer Clip Curves to Copy")]
 8     static void CopyClip()
 9     {
10         foreach (Object o in Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets))
11         {
12             if (!(o is GameObject)) continue;
13             if (!o.name.Contains("@")) continue;
14             GameObject animationFBX = (GameObject)o;
15 
16             AnimationClip srcClip = animationFBX.animation.clip;
17             AnimationClip newClip = new AnimationClip();
18             newClip.name = srcClip.name;
19 
20             // Create directory to store generated materials.
21             if (!Directory.Exists(AnimationsPath(animationFBX)))
22                 Directory.CreateDirectory(AnimationsPath(animationFBX));
23 
24             string animationPath = AnimationsPath(animationFBX) + newClip.name + ".anim";
25 
26             AssetDatabase.CreateAsset(newClip, animationPath);
27             AssetDatabase.Refresh();
28 
29             AnimationClipCurveData[] curveDatas = AnimationUtility.GetAllCurves(srcClip, true);
30             for (int i = 0; i < curveDatas.Length; i++)
31             {
32                 AnimationUtility.SetEditorCurve(newClip, curveDatas[i].path, curveDatas[i].type, curveDatas[i].propertyName, curveDatas[i].curve);
33             }
34         }
35     }
36 
37     // Returns the path to the directory that holds the specified FBX.
38     static string CharacterRoot(GameObject character)
39     {
40         string root = AssetDatabase.GetAssetPath(character);
41         return root.Substring(0, root.LastIndexOf('/') + 1);
42     }
43 
44     // Returns the path to the directory that holds materials generated
45     // for the specified FBX.
46     public static string AnimationsPath(GameObject character)
47     {
48         return CharacterRoot(character) + "Copy Animations/";
49     }
50 }

 

posted @ 2013-01-11 11:50  Marble  阅读(1410)  评论(1编辑  收藏  举报