obsidian/笔记文件/2.笔记/通过代码添加动画帧事件.md
2025-03-26 00:02:56 +08:00

997 B
Raw Permalink Blame History

#unity/日常积累

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RbotArmController : MonoBehaviour
{
	// 指定animation 动画控制器,在脚本所在的物体上直接拖拽即可
    public Animator armAnimator;
    private void Start()
    {
    	// 创建一个事件
        AnimationEvent evt = new AnimationEvent();
        // 绑定触发事件后要执行的方法名
        evt.functionName = "PrintEvent";
        // 执行方法后要传入的参数
        evt.intParameter = 12345;
        // 设置事件关键帧的位置当事件过了1.3秒后执行
        evt.time = 1.3f;
        // 设置目标动画剪辑
        AnimationClip clip = armAnimator.runtimeAnimatorController.animationClips[0];
        // 绑定事件
        clip.AddEvent(evt);
    }
    // 触发事件后,要执行的方法
    public void PrintEvent(int i)
    {
        print("PrintEvent: " + i + " called at: " + Time.time);
    }
}