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/

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. GstAppSrc简介

    Description The appsrc element can be used by applications to insert data into a GStreamer pipeline. ...

  2. Docker实践中遇到的坑

    1.docker容器中后台运行退出执行curl+p+q,再次进入执行命令docker attach 容器id. 2.容器中exit退出后,还原方法为docker ps -a 查看历史运行容器,dock ...

  3. csrf防范笔记

    1.验证Http的refer字段 http有一个refer字段,用以记录该http请求的来源地址 好处: 简单便捷,后台开发人员只需要设置一个拦截器 缺点: Referer 的值是由浏览器提供的,虽然 ...

  4. 虫草医药网站html模板

    虫草医药网站html模板是一款宝王虫草医药网站模板html源码整站下载. 模板地址:http://www.huiyi8.com/sc/8783.html

  5. legend2---开发日志15(功能需求明确,设计好类和结构的好处是)

    legend2---开发日志15(功能需求明确,设计好类和结构的好处是) 一.总结 一句话总结: 极快简化编程,节约大量时间 1.多个类型的物品,比如商店和寻宝的丹药,装备,特性书,英雄石等等 应该怎 ...

  6. 分享知识-快乐自己:spring_Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson

    心路历程: 在Controller中return 对象的时候,对象中的属性值中文部分在浏览器中 显示为问号?? 然后结果是这样的:?? 尝试排查原因: 中文乱码常有以下三种: 1.request.re ...

  7. C语言中mktime函数功能及用法

    今天联系写一个日历的程序,需要算出月份中的第一天是星期几,用到了mktime()这个函数,感觉这个函数挺有用的,分享给大家. 原型:time_t mktime(struct tm *) 其中的tm结构 ...

  8. Linux Shell: 统计系统中占用Swap 的程序PID和占用大小

    #!/bin/bash  SUM=0 OVERALL=0 for DIR in `find /proc/ -maxdepth 1 -type d -regex "^/proc/[0-9]+& ...

  9. bzoj 2946 公共串 —— 后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2946 建出 n-1 个后缀自动机一起跑呗. 代码如下: #include<cstdio ...

  10. rt-thread的定时器管理源码分析

    1 前言 rt-thread可以采用软件定时器或硬件定时器来实现定时器管理的,所谓软件定时器是指由操作系统提供的一类系统接口,它构建在硬件定时器基础之上,使系统能够提供不受数目限制的定时器服务.而硬件 ...