http://blog.csdn.net/aa20274270/article/details/52528449
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
public class Test { [MenuItem("Assets/生成动作帧事件")]
public static void GenerAnimationEvent1()
{
List<AnimationEvent> eventGroup = new List<AnimationEvent>();
AnimationEvent hit = new AnimationEvent();
hit.time = 0.0f;
hit.functionName = "hit";
hit.messageOptions = SendMessageOptions.DontRequireReceiver;
eventGroup.Add(hit); AnimationEvent pr = new AnimationEvent();
pr.time = 0.3f;
pr.functionName = "pr";
eventGroup.Add(pr); GenerAnimationEvent(eventGroup.ToArray());
}
private static void GenerAnimationEvent(AnimationEvent[] eventGroup)
{
UnityEngine.Object[] selObjs = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
if (selObjs == null || selObjs.Length == 0)
{
Debug.LogError("请选择需要添加帧事件的动画!");
return;
}
foreach (UnityEngine.Object obj in selObjs)
{
if (obj.GetType() != typeof(GameObject))
continue;
GameObject fbx = (GameObject)obj;
string fbxPath = AssetDatabase.GetAssetPath(fbx);
UnityEngine.Object[] assets = AssetDatabase.LoadAllAssetsAtPath(fbxPath);
foreach (UnityEngine.Object objGo in assets)
{
if (objGo.GetType() != typeof(AnimationClip))
continue;
if (objGo.name.Contains("Take 0"))
continue;
Debug.Log(objGo.name);
AnimationClip clipGo = (AnimationClip)objGo; AnimationEvent[] events = AnimationUtility.GetAnimationEvents(clipGo);
if (events.Length != 0)
{
Debug.Log(fbx.name + "/" + clipGo.name + "已有帧事件");
foreach (AnimationEvent eventGo in events)
Debug.Log(string.Format("functionName: {0}, time: {1}", eventGo.functionName, eventGo.time));
continue;
} ModelImporter modelImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(clipGo)) as ModelImporter;
if (modelImporter == null)
return;
modelImporter.clipAnimations = modelImporter.defaultClipAnimations; SerializedObject serializedObject = new SerializedObject(modelImporter);
SerializedProperty clipAnimations = serializedObject.FindProperty("m_ClipAnimations");
Debug.Log("clipAnimations.arraySize " + clipAnimations.arraySize);
for (int i = 0; i < clipAnimations.arraySize; i++)
{
AnimationClipInfoProperties clipInfoProperties = new AnimationClipInfoProperties(clipAnimations.GetArrayElementAtIndex(i));
clipInfoProperties.SetEvents(eventGroup);
serializedObject.ApplyModifiedProperties();
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(clipGo));
}
}
}
AssetDatabase.Refresh();
} static void DoAddEventImportedClip(AnimationClip sourceAnimClip, AnimationClip targetAnimClip)
{
ModelImporter modelImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(targetAnimClip)) as ModelImporter;
if (modelImporter == null)
return; SerializedObject serializedObject = new SerializedObject(modelImporter);
SerializedProperty clipAnimations = serializedObject.FindProperty("m_ClipAnimations"); if (!clipAnimations.isArray)
return; for (int i = 0; i < clipAnimations.arraySize; i++)
{
AnimationClipInfoProperties clipInfoProperties = new AnimationClipInfoProperties(clipAnimations.GetArrayElementAtIndex(i));
if (clipInfoProperties.name == targetAnimClip.name)
{
AnimationEvent[] sourceAnimEvents = AnimationUtility.GetAnimationEvents(sourceAnimClip); clipInfoProperties.SetEvents(sourceAnimEvents);
serializedObject.ApplyModifiedProperties();
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(targetAnimClip));
break;
}
} }
} class AnimationClipInfoProperties
{
SerializedProperty m_Property; private SerializedProperty Get(string property) { return m_Property.FindPropertyRelative(property); } public AnimationClipInfoProperties(SerializedProperty prop) { m_Property = prop; } public string name { get { return Get("name").stringValue; } set { Get("name").stringValue = value; } }
public string takeName { get { return Get("takeName").stringValue; } set { Get("takeName").stringValue = value; } }
public float firstFrame { get { return Get("firstFrame").floatValue; } set { Get("firstFrame").floatValue = value; } }
public float lastFrame { get { return Get("lastFrame").floatValue; } set { Get("lastFrame").floatValue = value; } }
public int wrapMode { get { return Get("wrapMode").intValue; } set { Get("wrapMode").intValue = value; } }
public bool loop { get { return Get("loop").boolValue; } set { Get("loop").boolValue = value; } } // Mecanim animation properties
public float orientationOffsetY { get { return Get("orientationOffsetY").floatValue; } set { Get("orientationOffsetY").floatValue = value; } }
public float level { get { return Get("level").floatValue; } set { Get("level").floatValue = value; } }
public float cycleOffset { get { return Get("cycleOffset").floatValue; } set { Get("cycleOffset").floatValue = value; } }
public bool loopTime { get { return Get("loopTime").boolValue; } set { Get("loopTime").boolValue = value; } }
public bool loopBlend { get { return Get("loopBlend").boolValue; } set { Get("loopBlend").boolValue = value; } }
public bool loopBlendOrientation { get { return Get("loopBlendOrientation").boolValue; } set { Get("loopBlendOrientation").boolValue = value; } }
public bool loopBlendPositionY { get { return Get("loopBlendPositionY").boolValue; } set { Get("loopBlendPositionY").boolValue = value; } }
public bool loopBlendPositionXZ { get { return Get("loopBlendPositionXZ").boolValue; } set { Get("loopBlendPositionXZ").boolValue = value; } }
public bool keepOriginalOrientation { get { return Get("keepOriginalOrientation").boolValue; } set { Get("keepOriginalOrientation").boolValue = value; } }
public bool keepOriginalPositionY { get { return Get("keepOriginalPositionY").boolValue; } set { Get("keepOriginalPositionY").boolValue = value; } }
public bool keepOriginalPositionXZ { get { return Get("keepOriginalPositionXZ").boolValue; } set { Get("keepOriginalPositionXZ").boolValue = value; } }
public bool heightFromFeet { get { return Get("heightFromFeet").boolValue; } set { Get("heightFromFeet").boolValue = value; } }
public bool mirror { get { return Get("mirror").boolValue; } set { Get("mirror").boolValue = value; } } public AnimationEvent GetEvent(int index)
{
AnimationEvent evt = new AnimationEvent();
SerializedProperty events = Get("events"); if (events != null && events.isArray)
{
if (index < events.arraySize)
{
evt.floatParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("floatParameter").floatValue;
evt.functionName = events.GetArrayElementAtIndex(index).FindPropertyRelative("functionName").stringValue;
evt.intParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("intParameter").intValue;
evt.objectReferenceParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("objectReferenceParameter").objectReferenceValue;
evt.stringParameter = events.GetArrayElementAtIndex(index).FindPropertyRelative("data").stringValue;
evt.time = events.GetArrayElementAtIndex(index).FindPropertyRelative("time").floatValue;
}
else
{
Debug.LogWarning("Invalid Event Index");
}
} return evt;
} public void SetEvent(int index, AnimationEvent animationEvent)
{
SerializedProperty events = Get("events"); if (events != null && events.isArray)
{
if (index < events.arraySize)
{
events.GetArrayElementAtIndex(index).FindPropertyRelative("floatParameter").floatValue = animationEvent.floatParameter;
events.GetArrayElementAtIndex(index).FindPropertyRelative("functionName").stringValue = animationEvent.functionName;
events.GetArrayElementAtIndex(index).FindPropertyRelative("intParameter").intValue = animationEvent.intParameter;
events.GetArrayElementAtIndex(index).FindPropertyRelative("objectReferenceParameter").objectReferenceValue = animationEvent.objectReferenceParameter;
events.GetArrayElementAtIndex(index).FindPropertyRelative("data").stringValue = animationEvent.stringParameter;
events.GetArrayElementAtIndex(index).FindPropertyRelative("time").floatValue = animationEvent.time;
} else
{
Debug.LogWarning("Invalid Event Index");
}
}
} public void ClearEvents()
{
SerializedProperty events = Get("events"); if (events != null && events.isArray)
{
events.ClearArray();
}
} public int GetEventCount()
{
int ret = 0; SerializedProperty curves = Get("events"); if (curves != null && curves.isArray)
{
ret = curves.arraySize;
} return ret;
} public void SetEvents(AnimationEvent[] newEvents)
{
SerializedProperty events = Get("events"); if (events != null && events.isArray)
{
events.ClearArray(); foreach (AnimationEvent evt in newEvents)
{
events.InsertArrayElementAtIndex(events.arraySize);
SetEvent(events.arraySize - 1, evt);
}
}
} public AnimationEvent[] GetEvents()
{
AnimationEvent[] ret = new AnimationEvent[GetEventCount()];
SerializedProperty events = Get("events"); if (events != null && events.isArray)
{
for (int i = 0; i < GetEventCount(); ++i)
{
ret[i] = GetEvent(i);
}
} return ret; } }

主要参考:

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

最新文章

  1. 华为oj 字符串最后一个单词的长度
  2. 例子:Camera Color Picker Sample (YCbCr-&gt;ARGB)
  3. sqlserver 视图能否有变量
  4. andorid 中如何实现双击事件
  5. 在C#中实现软件自动升级
  6. HTTP_X_FORWARDED_FOR 和 REMOTE_ADDR的使用 php
  7. struts.custom.i18n.resources国际化详解(一)
  8. hdu4506小明系列故事——师兄帮帮忙 (用二进制,大数高速取余)
  9. HDU 5857 Median
  10. %3f URL --&gt; &#39;?&#39;拼接引发的问题
  11. XML 特殊字符处理和 CDATA
  12. Python 数据分析Windows环境搭建
  13. 消息中间件--ActiveMQ&amp;JMS消息服务
  14. Centos系统中彻底删除Mysql数据库
  15. Vue使用vue-echarts图表
  16. WeX5学习笔记 - 01
  17. 关于VXLAN的认识-----基础知识
  18. 18个分形图形的GIF动画演示
  19. ubuntu开机自动运行用Qt写的程序
  20. css position小结

热门文章

  1. PHP接口中的静态变量、常量与类中静态变量、常量的区别
  2. View 视图动画基础
  3. HDU - 1213 How Many Tables 【并查集】
  4. Express的基本使用
  5. PS图片透明处理方法
  6. C# 计时器 以“天时分秒毫秒”形式动态增加显示
  7. jira与wiki官方文档记录
  8. Linux_服务器_06_VMware虚拟机下安装CentOS7.0图文教程
  9. Android 在Activity中对SQLite的操作
  10. 用rem适配移动端