1. http://blog.csdn.net/aa20274270/article/details/52528449
  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. class AnimationClipInfoProperties
  107. {
  108. SerializedProperty m_Property;
  109.  
  110. private SerializedProperty Get(string property) { return m_Property.FindPropertyRelative(property); }
  111.  
  112. public AnimationClipInfoProperties(SerializedProperty prop) { m_Property = prop; }
  113.  
  114. public string name { get { return Get("name").stringValue; } set { Get("name").stringValue = value; } }
  115. public string takeName { get { return Get("takeName").stringValue; } set { Get("takeName").stringValue = value; } }
  116. public float firstFrame { get { return Get("firstFrame").floatValue; } set { Get("firstFrame").floatValue = value; } }
  117. public float lastFrame { get { return Get("lastFrame").floatValue; } set { Get("lastFrame").floatValue = value; } }
  118. public int wrapMode { get { return Get("wrapMode").intValue; } set { Get("wrapMode").intValue = value; } }
  119. public bool loop { get { return Get("loop").boolValue; } set { Get("loop").boolValue = value; } }
  120.  
  121. // Mecanim animation properties
  122. public float orientationOffsetY { get { return Get("orientationOffsetY").floatValue; } set { Get("orientationOffsetY").floatValue = value; } }
  123. public float level { get { return Get("level").floatValue; } set { Get("level").floatValue = value; } }
  124. public float cycleOffset { get { return Get("cycleOffset").floatValue; } set { Get("cycleOffset").floatValue = value; } }
  125. public bool loopTime { get { return Get("loopTime").boolValue; } set { Get("loopTime").boolValue = value; } }
  126. public bool loopBlend { get { return Get("loopBlend").boolValue; } set { Get("loopBlend").boolValue = value; } }
  127. public bool loopBlendOrientation { get { return Get("loopBlendOrientation").boolValue; } set { Get("loopBlendOrientation").boolValue = value; } }
  128. public bool loopBlendPositionY { get { return Get("loopBlendPositionY").boolValue; } set { Get("loopBlendPositionY").boolValue = value; } }
  129. public bool loopBlendPositionXZ { get { return Get("loopBlendPositionXZ").boolValue; } set { Get("loopBlendPositionXZ").boolValue = value; } }
  130. public bool keepOriginalOrientation { get { return Get("keepOriginalOrientation").boolValue; } set { Get("keepOriginalOrientation").boolValue = value; } }
  131. public bool keepOriginalPositionY { get { return Get("keepOriginalPositionY").boolValue; } set { Get("keepOriginalPositionY").boolValue = value; } }
  132. public bool keepOriginalPositionXZ { get { return Get("keepOriginalPositionXZ").boolValue; } set { Get("keepOriginalPositionXZ").boolValue = value; } }
  133. public bool heightFromFeet { get { return Get("heightFromFeet").boolValue; } set { Get("heightFromFeet").boolValue = value; } }
  134. public bool mirror { get { return Get("mirror").boolValue; } set { Get("mirror").boolValue = value; } }
  135.  
  136. public AnimationEvent GetEvent(int index)
  137. {
  138. AnimationEvent evt = new AnimationEvent();
  139. SerializedProperty events = Get("events");
  140.  
  141. if (events != null && events.isArray)
  142. {
  143. if (index < events.arraySize)
  144. {
  145. evt.floatParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("floatParameter").floatValue;
  146. evt.functionName = events.GetArrayElementAtIndex(index).FindPropertyRelative("functionName").stringValue;
  147. evt.intParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("intParameter").intValue;
  148. evt.objectReferenceParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("objectReferenceParameter").objectReferenceValue;
  149. evt.stringParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("data").stringValue;
  150. evt.time = events.GetArrayElementAtIndex(index).FindPropertyRelative("time").floatValue;
  151. }
  152. else
  153. {
  154. Debug.LogWarning("Invalid Event Index");
  155. }
  156. }
  157.  
  158. return evt;
  159. }
  160.  
  161. public void SetEvent(int index, AnimationEvent animationEvent)
  162. {
  163. SerializedProperty events = Get("events");
  164.  
  165. if (events != null && events.isArray)
  166. {
  167. if (index < events.arraySize)
  168. {
  169. events.GetArrayElementAtIndex(index).FindPropertyRelative("floatParameter").floatValue = animationEvent.floatParameter;
  170. events.GetArrayElementAtIndex(index).FindPropertyRelative("functionName").stringValue = animationEvent.functionName;
  171. events.GetArrayElementAtIndex(index).FindPropertyRelative("intParameter").intValue = animationEvent.intParameter;
  172. events.GetArrayElementAtIndex(index).FindPropertyRelative("objectReferenceParameter").objectReferenceValue = animationEvent.objectReferenceParameter;
  173. events.GetArrayElementAtIndex(index).FindPropertyRelative("data").stringValue = animationEvent.stringParameter;
  174. events.GetArrayElementAtIndex(index).FindPropertyRelative("time").floatValue = animationEvent.time;
  175. }
  176.  
  177. else
  178. {
  179. Debug.LogWarning("Invalid Event Index");
  180. }
  181. }
  182. }
  183.  
  184. public void ClearEvents()
  185. {
  186. SerializedProperty events = Get("events");
  187.  
  188. if (events != null && events.isArray)
  189. {
  190. events.ClearArray();
  191. }
  192. }
  193.  
  194. public int GetEventCount()
  195. {
  196. int ret = 0;
  197.  
  198. SerializedProperty curves = Get("events");
  199.  
  200. if (curves != null && curves.isArray)
  201. {
  202. ret = curves.arraySize;
  203. }
  204.  
  205. return ret;
  206. }
  207.  
  208. public void SetEvents(AnimationEvent[] newEvents)
  209. {
  210. SerializedProperty events = Get("events");
  211.  
  212. if (events != null && events.isArray)
  213. {
  214. events.ClearArray();
  215.  
  216. foreach (AnimationEvent evt in newEvents)
  217. {
  218. events.InsertArrayElementAtIndex(events.arraySize);
  219. SetEvent(events.arraySize - 1, evt);
  220. }
  221. }
  222. }
  223.  
  224. public AnimationEvent[] GetEvents()
  225. {
  226. AnimationEvent[] ret = new AnimationEvent[GetEventCount()];
  227. SerializedProperty events = Get("events");
  228.  
  229. if (events != null && events.isArray)
  230. {
  231. for (int i = 0; i < GetEventCount(); ++i)
  232. {
  233. ret[i] = GetEvent(i);
  234. }
  235. }
  236.  
  237. return ret;
  238.  
  239. }
  240.  
  241. }

主要参考:

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

Unity3D 自动添加Fbx Animation Event的更多相关文章

  1. Unity3D研究院之使用Animation编辑器编辑动画(五十四)

     Unity提供了Animation编辑器,它可以为我们编辑物理动画.举个例子比如场景中有一个来回摇动的秋千,这个秋千在项目中完全只起到衬托作用,它不会与别的游戏对象有任何交互.如果这个秋千也用代码来 ...

  2. Unity3D研究院之使用Animation编辑器编辑动画

     Unity提供了Animation编辑器,它可以为我们编辑物理动画.举个例子比如场景中有一个来回摇动的秋千,这个秋千在项目中完全只起到衬托作用,它不会与别的游戏对象有任何交互.如果这个秋千也用代码来 ...

  3. unity5, animation event

    一,给导入的fbx动画添加animation event: 如下图,在双击状态机中的idle状态,打开右面的面板,点开Events项会出现一个时间轴,点击下方播放器的播放按钮或者拖动播放器时间轴上的红 ...

  4. Zabbix网络自动发现规则和自动添加hosts及link模板

    Version: zabbix 3.0 一.配置网络发现规则 Device uniqueness criteria:选择主机名作为唯一标识(Configuation Hosts中显示的NAME) 二. ...

  5. 关于用Max导出Unity3D使用的FBX文件流程注解

    原地址:http://hi.baidu.com/phpstyle/item/c167a4c0694670b10d0a7b87 关于用Max导出Unity3D使用的FBX文件流程注解(转载) (2011 ...

  6. python logging详解及自动添加上下文信息

    之前写过一篇文章日志的艺术(The art of logging),提到了输出日志的时候记录上下文信息的重要性,我认为上下文信息包括: when:log事件发生的时间 where:log事件发生在哪个 ...

  7. zabbix server的Discover功能,实现zabbix agent 大批量的自动添加,并链接到指定的模版(3)

    一.需求 zabbix 服务器可以手动加入zabbix-agent客户端,对于少量的机器,这没有什么.但到了线上,我们有大量的服务器需要监控时,如果再一个个的手动加的话,工作量势必会增加很多.这时,z ...

  8. 前端自动化工具gulp自动添加版本号

    之前,我介绍了学习安装并配置前端自动化工具Gulp,觉得gulp确实比grunt的配置简单很多,于是我决定再深入学习一下gulp,就去网上查了资料,发现gulp还可以自动添加版本号,这个功能就为我平时 ...

  9. Gulp自动添加版本号(转载)

    本文转载自: gulp自动添加版本号

随机推荐

  1. SAP 4代增强

    *20170325 160000 以下之外, 还有:.替代, -用过一次:.BTE -没用过,需要学习: 第二代增强和第三代增强的差别: 1.Tcode 不同:第二代: CMOD 增强管理,SMOD ...

  2. Mysql 外键级联

    如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表.外键是用来实现参照完整性的,不同的外键约束方式将可以使两张表紧密的结合起来,特别是修改或者删除的级联操作将使得日常 ...

  3. redis下载及安装服务

    1 . 要安装Redis,首先要获取安装包. Windows的Redis安装包需要到以下GitHub链接找到. 链接:https://github.com/MSOpenTech/redis 打开网站后 ...

  4. iOS开发的10个奇袭

    1.关于关键字volatile 一个定义为volatile的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了.精确地说就是,优化器在用到这个变量时必须每次都小心地重新读取这 ...

  5. PHP echo() 函数

    实例 输出文本: <?php echo "Hello world!"; ?> 定义和用法 echo() 函数输出一个或多个字符串. 注释:echo() 函数实际不是一个 ...

  6. 【python】用python脚本Paramiko实现远程执行命令、下载、推送/上传文件功能

    Paramiko: paramiko模块,基于SSH用于连接远程服务器并执行相关操作. SSHClient: 用于连接远程服务器并执行基本命令 SFTPClient: 用于连接远程服务器并执行上传下载 ...

  7. 《CSS权威指南(第三版)》---第四章 值和单位

    本章主要讲解的是一些属性声明用的值: CSS中的值主要有数字,百分数,颜色, 1.颜色: rgb(100%,100%,100%)  OR  rgb(255,255,255) OR #FF0000 WE ...

  8. Exception of type 'System.OutOfMemoryException' was thrown

    最近刚换了服务器,开始测试的时候未发现什么问题,可是一旦同一时间段操作的人比较多的时候,就会抛出如下错误: Server Error in '/' Application. Exception of ...

  9. 打造vim成类source insight

    一.Ubuntu14.04下配置 1.配置vimrc文件 输入:version课查看vimrc文件及位置: system vimrc file: "$VIM/vimrc" user ...

  10. BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1634 题意: 约翰留下他的N只奶牛上山采木.可是,当他回来的时候,他看到了一幕惨剧:牛们正 ...