public class AddEvent : MonoBehaviour
{
public Animation animation;
public Animator animator;
private void Start()
{
// Add();
AddAnimator();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
//animation.Play("Open");
//重复调用播放,play不能一直重复调用播放
animator.PlayInFixedTime("Open",0,0);
}
if (Input.GetKeyDown(KeyCode.S))
{
//animation.Play("Close");
animator.PlayInFixedTime("Close",0,0);
}
}
public void AddAnimator()
{
//获取所有的Clip
var clips = animator.runtimeAnimatorController.animationClips;
for (int i = 0; i < clips.Length; i++)
{
if (clips[i].name == "Open")
{
clips[i].events = new[] {new AnimationEvent() {functionName = "OpenDebug", time = clips[i].length}};
continue;
}
if (clips[i].name == "Close")
{
clips[i].events = new[] {new AnimationEvent() {functionName = "CloseDebug", time = clips[i].length}};
continue;
}
}
}
public void Add()
{
var openClip = animation.GetClip("Open");
openClip.events = new[] {new AnimationEvent() {functionName = "OpenDebug", time = openClip.length}};
var closeClip = animation.GetClip("Close");
closeClip.events = new[] {new AnimationEvent() {functionName = "CloseDebug", time = closeClip.length}};
}
public void OpenDebug()
{
Debug.Log("执行了Open的操作");
}
public void CloseDebug()
{
Debug.Log("执行了Close的操作");
}
}
脚本和动画组件保持在同一物体上