首先需要对模型进行设置:
关于这 3 中格式的介绍,可以查看这个:
https://blog.csdn.net/weixin_38239050/article/details/79648042
具体代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Reflection;
public class AutoAddAnimationEvent
{
private static string[] animtionEventsName = new string[3] { "OnAnimationActionStart", "OnAnimationActionIn", "OnAnimationActionEnd" };
[MenuItem("Assets/AddAnimationEvent")]
private static void ADDEvent()
{
List<AnimationEvent> events = new List<AnimationEvent>();
foreach (Object item in Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets))
{
if (item.name.Contains("@"))
{
Selection.activeObject = item;
events.Clear();
ModelImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(item)) as ModelImporter;
ModelImporterClipAnimation[] animation = importer.clipAnimations;
if(animation.Length <= 0) //给一个初始值
{
animation = importer.defaultClipAnimations;
}
if (animation.Length > 0)
{
ModelImporterClipAnimation clipAnimation = animation[0];
for (int index = 0; index < 3; index++)
{
AnimationEvent _event = new AnimationEvent();
_event.functionName = animtionEventsName[index];
//设置 回调事件的时间。
_event.time = index == 0 ? clipAnimation.firstFrame / clipAnimation.lastFrame + 0.1f : index == 1 ? clipAnimation.lastFrame / clipAnimation.lastFrame / 2 : clipAnimation.lastFrame / clipAnimation.lastFrame - 0.12f;
_event.floatParameter = 0;
_event.intParameter = 0;
events.Add(_event);
}
animation[0].events = events.ToArray();
importer.clipAnimations = animation;
importer.SaveAndReimport();
}
}
}
AssetDatabase.Refresh();
}
}
最后效果:
点击这些事件点,就可以看到具体的事件了。