Unity3D 自动添加Fbx Animation Event

[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. http://blog.csdn.net/aa20274270/article/details/52528449  
[csharp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using UnityEditor;  
  4. using System.Collections.Generic;  
  5. public class Test {  
  6.   
  7.     [MenuItem("Assets/生成动作帧事件")]  
  8.     public static void GenerAnimationEvent1()  
  9.     {  
  10.         List<AnimationEvent> eventGroup = new List<AnimationEvent>();  
  11.         AnimationEvent hit = new AnimationEvent();  
  12.         hit.time = 0.0f;  
  13.         hit.functionName = "hit";  
  14.         hit.messageOptions = SendMessageOptions.DontRequireReceiver;  
  15.         eventGroup.Add(hit);  
  16.   
  17.         AnimationEvent pr = new AnimationEvent();  
  18.         pr.time = 0.3f;  
  19.         pr.functionName = "pr";  
  20.         eventGroup.Add(pr);  
  21.   
  22.         GenerAnimationEvent(eventGroup.ToArray());  
  23.     }  
  24.     private static void GenerAnimationEvent(AnimationEvent[] eventGroup)  
  25.     {  
  26.         UnityEngine.Object[] selObjs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);  
  27.         if (selObjs == null || selObjs.Length == 0)  
  28.         {  
  29.             Debug.LogError("请选择需要添加帧事件的动画!");  
  30.             return;  
  31.         }  
  32.         foreach (UnityEngine.Object obj in selObjs)  
  33.         {  
  34.             if (obj.GetType() != typeof(GameObject))  
  35.                 continue;  
  36.             GameObject fbx = (GameObject)obj;  
  37.             string fbxPath = AssetDatabase.GetAssetPath(fbx);  
  38.             UnityEngine.Object[] assets = AssetDatabase.LoadAllAssetsAtPath(fbxPath);  
  39.             foreach (UnityEngine.Object objGo in assets)  
  40.             {  
  41.                 if (objGo.GetType() != typeof(AnimationClip))  
  42.                     continue;  
  43.                 if (objGo.name.Contains("Take 0"))  
  44.                     continue;  
  45.                 Debug.Log(objGo.name);  
  46.                 AnimationClip clipGo = (AnimationClip)objGo;  
  47.   
  48.                 AnimationEvent[] events = AnimationUtility.GetAnimationEvents(clipGo);  
  49.                 if (events.Length != 0)  
  50.                 {  
  51.                     Debug.Log(fbx.name + "/" + clipGo.name + "已有帧事件");  
  52.                     foreach (AnimationEvent eventGo in events)  
  53.                         Debug.Log(string.Format("functionName: {0}, time: {1}", eventGo.functionName, eventGo.time));  
  54.                     continue;  
  55.                 }  
  56.                   
  57.                 ModelImporter modelImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(clipGo)) as ModelImporter;  
  58.                 if (modelImporter == null)  
  59.                     return;  
  60.                 modelImporter.clipAnimations = modelImporter.defaultClipAnimations;  
  61.   
  62.                 SerializedObject serializedObject = new SerializedObject(modelImporter);  
  63.                 SerializedProperty clipAnimations = serializedObject.FindProperty("m_ClipAnimations");  
  64.                 Debug.Log("clipAnimations.arraySize " + clipAnimations.arraySize);  
  65.                 for (int i = 0; i < clipAnimations.arraySize; i++)  
  66.                 {  
  67.                     AnimationClipInfoProperties clipInfoProperties = new AnimationClipInfoProperties(clipAnimations.GetArrayElementAtIndex(i));  
  68.                     clipInfoProperties.SetEvents(eventGroup);  
  69.                     serializedObject.ApplyModifiedProperties();  
  70.                     AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(clipGo));  
  71.                 }  
  72.             }  
  73.         }  
  74.         AssetDatabase.Refresh();  
  75.     }  
  76.   
  77.     static void DoAddEventImportedClip(AnimationClip sourceAnimClip, AnimationClip targetAnimClip)  
  78.     {  
  79.         ModelImporter modelImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(targetAnimClip)) as ModelImporter;  
  80.         if (modelImporter == null)  
  81.             return;  
  82.   
  83.         SerializedObject serializedObject = new SerializedObject(modelImporter);  
  84.         SerializedProperty clipAnimations = serializedObject.FindProperty("m_ClipAnimations");  
  85.   
  86.         if (!clipAnimations.isArray)  
  87.             return;  
  88.   
  89.         for (int i = 0; i < clipAnimations.arraySize; i++)  
  90.         {  
  91.             AnimationClipInfoProperties clipInfoProperties = new AnimationClipInfoProperties(clipAnimations.GetArrayElementAtIndex(i));  
  92.             if (clipInfoProperties.name == targetAnimClip.name)  
  93.             {  
  94.                 AnimationEvent[] sourceAnimEvents = AnimationUtility.GetAnimationEvents(sourceAnimClip);  
  95.   
  96.                 clipInfoProperties.SetEvents(sourceAnimEvents);  
  97.                 serializedObject.ApplyModifiedProperties();  
  98.                 AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(targetAnimClip));  
  99.                 break;  
  100.             }  
  101.         }  
  102.   
  103.   
  104.   
  105.     }  
  106. }  
  107.   
  108. class AnimationClipInfoProperties  
  109. {  
  110.     SerializedProperty m_Property;  
  111.   
  112.     private SerializedProperty Get(string property) { return m_Property.FindPropertyRelative(property); }  
  113.   
  114.     public AnimationClipInfoProperties(SerializedProperty prop) { m_Property = prop; }  
  115.   
  116.     public string name { get { return Get("name").stringValue; } set { Get("name").stringValue = value; } }  
  117.     public string takeName { get { return Get("takeName").stringValue; } set { Get("takeName").stringValue = value; } }  
  118.     public float firstFrame { get { return Get("firstFrame").floatValue; } set { Get("firstFrame").floatValue = value; } }  
  119.     public float lastFrame { get { return Get("lastFrame").floatValue; } set { Get("lastFrame").floatValue = value; } }  
  120.     public int wrapMode { get { return Get("wrapMode").intValue; } set { Get("wrapMode").intValue = value; } }  
  121.     public bool loop { get { return Get("loop").boolValue; } set { Get("loop").boolValue = value; } }  
  122.   
  123.     // Mecanim animation properties  
  124.     public float orientationOffsetY { get { return Get("orientationOffsetY").floatValue; } set { Get("orientationOffsetY").floatValue = value; } }  
  125.     public float level { get { return Get("level").floatValue; } set { Get("level").floatValue = value; } }  
  126.     public float cycleOffset { get { return Get("cycleOffset").floatValue; } set { Get("cycleOffset").floatValue = value; } }  
  127.     public bool loopTime { get { return Get("loopTime").boolValue; } set { Get("loopTime").boolValue = value; } }  
  128.     public bool loopBlend { get { return Get("loopBlend").boolValue; } set { Get("loopBlend").boolValue = value; } }  
  129.     public bool loopBlendOrientation { get { return Get("loopBlendOrientation").boolValue; } set { Get("loopBlendOrientation").boolValue = value; } }  
  130.     public bool loopBlendPositionY { get { return Get("loopBlendPositionY").boolValue; } set { Get("loopBlendPositionY").boolValue = value; } }  
  131.     public bool loopBlendPositionXZ { get { return Get("loopBlendPositionXZ").boolValue; } set { Get("loopBlendPositionXZ").boolValue = value; } }  
  132.     public bool keepOriginalOrientation { get { return Get("keepOriginalOrientation").boolValue; } set { Get("keepOriginalOrientation").boolValue = value; } }  
  133.     public bool keepOriginalPositionY { get { return Get("keepOriginalPositionY").boolValue; } set { Get("keepOriginalPositionY").boolValue = value; } }  
  134.     public bool keepOriginalPositionXZ { get { return Get("keepOriginalPositionXZ").boolValue; } set { Get("keepOriginalPositionXZ").boolValue = value; } }  
  135.     public bool heightFromFeet { get { return Get("heightFromFeet").boolValue; } set { Get("heightFromFeet").boolValue = value; } }  
  136.     public bool mirror { get { return Get("mirror").boolValue; } set { Get("mirror").boolValue = value; } }  
  137.   
  138.     public AnimationEvent GetEvent(int index)  
  139.     {  
  140.         AnimationEvent evt = new AnimationEvent();  
  141.         SerializedProperty events = Get("events");  
  142.   
  143.         if (events != null && events.isArray)  
  144.         {  
  145.             if (index < events.arraySize)  
  146.             {  
  147.                 evt.floatParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("floatParameter").floatValue;  
  148.                 evt.functionName = events.GetArrayElementAtIndex(index).FindPropertyRelative("functionName").stringValue;  
  149.                 evt.intParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("intParameter").intValue;  
  150.                 evt.objectReferenceParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("objectReferenceParameter").objectReferenceValue;  
  151.                 evt.stringParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("data").stringValue;  
  152.                 evt.time = events.GetArrayElementAtIndex(index).FindPropertyRelative("time").floatValue;  
  153.             }  
  154.             else  
  155.             {  
  156.                 Debug.LogWarning("Invalid Event Index");  
  157.             }  
  158.         }  
  159.   
  160.         return evt;  
  161.     }  
  162.   
  163.     public void SetEvent(int index, AnimationEvent animationEvent)  
  164.     {  
  165.         SerializedProperty events = Get("events");  
  166.   
  167.         if (events != null && events.isArray)  
  168.         {  
  169.             if (index < events.arraySize)  
  170.             {  
  171.                 events.GetArrayElementAtIndex(index).FindPropertyRelative("floatParameter").floatValue = animationEvent.floatParameter;  
  172.                 events.GetArrayElementAtIndex(index).FindPropertyRelative("functionName").stringValue = animationEvent.functionName;  
  173.                 events.GetArrayElementAtIndex(index).FindPropertyRelative("intParameter").intValue = animationEvent.intParameter;  
  174.                 events.GetArrayElementAtIndex(index).FindPropertyRelative("objectReferenceParameter").objectReferenceValue = animationEvent.objectReferenceParameter;  
  175.                 events.GetArrayElementAtIndex(index).FindPropertyRelative("data").stringValue = animationEvent.stringParameter;  
  176.                 events.GetArrayElementAtIndex(index).FindPropertyRelative("time").floatValue = animationEvent.time;  
  177.             }  
  178.   
  179.             else  
  180.             {  
  181.                 Debug.LogWarning("Invalid Event Index");  
  182.             }  
  183.         }  
  184.     }  
  185.   
  186.   
  187.     public void ClearEvents()  
  188.     {  
  189.         SerializedProperty events = Get("events");  
  190.   
  191.         if (events != null && events.isArray)  
  192.         {  
  193.             events.ClearArray();  
  194.         }  
  195.     }  
  196.   
  197.     public int GetEventCount()  
  198.     {  
  199.         int ret = 0;  
  200.   
  201.         SerializedProperty curves = Get("events");  
  202.   
  203.         if (curves != null && curves.isArray)  
  204.         {  
  205.             ret = curves.arraySize;  
  206.         }  
  207.   
  208.         return ret;  
  209.     }  
  210.   
  211.     public void SetEvents(AnimationEvent[] newEvents)  
  212.     {  
  213.         SerializedProperty events = Get("events");  
  214.   
  215.         if (events != null && events.isArray)  
  216.         {  
  217.             events.ClearArray();  
  218.   
  219.             foreach (AnimationEvent evt in newEvents)  
  220.             {  
  221.                 events.InsertArrayElementAtIndex(events.arraySize);  
  222.                 SetEvent(events.arraySize - 1, evt);  
  223.             }  
  224.         }  
  225.     }  
  226.   
  227.     public AnimationEvent[] GetEvents()  
  228.     {  
  229.         AnimationEvent[] ret = new AnimationEvent[GetEventCount()];  
  230.         SerializedProperty events = Get("events");  
  231.   
  232.         if (events != null && events.isArray)  
  233.         {  
  234.             for (int i = 0; i < GetEventCount(); ++i)  
  235.             {  
  236.                 ret[i] = GetEvent(i);  
  237.             }  
  238.         }  
  239.   
  240.         return ret;  
  241.   
  242.     }  
  243.   
  244. }  


主要参考:

http://forum.unity3d.com/threads/animationutility-setanimationevents-not-working.243810/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值