2019-07-11 【UnityTips】代码添加动画事件
2019-07-11 本文已影响0人
持刀的要迟到了
1.外部导入得带骨骼(.FBX)得动画事件不能保存,因为是只读得。
https://answers.unity.com/questions/187907/how-to-add-keyframes-on-imported-read-only-animati.html
2.非只读动画可以在编辑器模式下保存。代码如下:
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class AnimEventGenerator : MonoBehaviour
{
public AnimEventGeneratorAsset asset;
public bool Generate = false;
private void Update()
{
if(Generate)
{
Generate = false;
foreach (var x in asset.clips)
{
ResetEvent(x);
}
}
}
void ResetEvent(AnimClipEvents data)
{
int eventCount = data.events.Length;
AnimationEvent[] eventList = new AnimationEvent[eventCount];
AnimationClip clip = data.clip;
for(int i = 0;i < eventCount; i ++)
{
AnimationEvent e = new AnimationEvent();
e.functionName = data.events[i].funcName;
e.time = data.events[i].frame / clip.frameRate;
e.intParameter = data.events[i].intParam;
Debug.Log(e.functionName + " " + e.time + " " + e.intParameter);
eventList[i] = e;
}
Debug.Log(clip.name);
AnimationUtility.SetAnimationEvents(clip, eventList);
//EditorUtility.SetDirty(clip);
}
}
using UnityEngine;
[CreateAssetMenu(menuName = "AnimHelper/AnimEventGeneratorAsset")]
public class AnimEventGeneratorAsset : ScriptableObject
{
[SerializeField]
public AnimClipEvents[] clips;
}
using System;
using UnityEngine;
[CreateAssetMenu(menuName = "AnimHelper/AnimClipEvents")]
public class AnimClipEvents : ScriptableObject
{
public AnimationClip clip;
public int frameCount;
public AnimEvent[] events;
}
[Serializable]
public class AnimEvent
{
public string funcName;
public int frame;
public int intParam;
}