当前位置:网站首页>Unity code registers events for animation

Unity code registers events for animation

2022-06-10 23:07:00 Wuzimu

We can add events to the animation , To automatically play the next animation , The code is as follows

    private Animator animator;
    private AnimationClip[] clips; // All the animations 

    private void Awake()
    {
    
        animator = GetComponent<Animator>();
        clips = animator.runtimeAnimatorController.animationClips;

        AddAnimationEvent(clips, "EndEvent",1);
    }
    /// <summary>
    ///  Batch register events for animation clips  clips Array of animation clips  eventName  Self defined event instance method name  timeRate When the animation is broadcast 
    /// </summary>
    private void AddAnimationEvent(AnimationClip [] clips,string eventName,float timeRate)
    {
    
        for (int i = 0; i < clips.Length; i++)
        {
    
            AnimationEvent _event = new AnimationEvent();
            _event.functionName = eventName;// Set the bound event name 
            _event.time = clips[i].length * timeRate;// Set the trigger time of the event  timeRate =1  That is, when the animation playback is completed 
            clips[i].AddEvent(_event);
        }
    }
    /// <summary>
    ///  The instance method of the event 
    /// </summary>
    private void EndEvent()
    {
    
        print(" The animation is finished ");
        // End animation , According to the choice , Auto play or no auto play 
        GameController.Instance.AutoPlay();
    }
原网站

版权声明
本文为[Wuzimu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102153541091.html