Unity Animator 逐帧播放动画实现

2021-01-05  本文已影响0人  _灯下影子

C#脚本

需求:Scroll 滑动促使特效跟随播放。
效果:上滑-》太阳升起, 下滑-》太阳下落

using UnityEngine;
namespace BBQ
{

    public class AnimationExtention : MonoBehaviour
    {
        //预制中关联Animator 动画文件
        public Animator animator;
        /// <summary>
        /// 播放动画
        /// </summary>
        public void Play()
        {
            animator.Play(0);
            animator.speed = 1;
        }
        
        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            animator.speed = 0;
        }
        
        /// <summary>
        /// 恢复
        /// </summary>
        public void Resume()
        {
            animator.speed = 1;
        }
        /// <summary>
        /// time: flipxPer  动画进度百分比(percent)
        /// </summary>
        /// <param name="time"></param>
        public void SetAniNormalizedTime(float time)
        {
            //  time  范围 0~1
            animator.speed = 1;
            animator.Play(0, 0, time);
            animator.speed = 0;
        }

        // Update is called once per frame
        void Update () {
            #region 测试代码
            // if (Input.GetKeyDown(KeyCode.R)) {
            //     animator.Play(0);
            //     animator.speed = 1;
            // }
            // if (Input.GetKeyDown(KeyCode.A)) {
            //     this.SetAniNormalizedTime(0.1f);
            // }
            // if (Input.GetKeyDown(KeyCode.S)) {
            //     this.SetAniNormalizedTime(0.2f);
            // }
            // if (Input.GetKeyDown(KeyCode.H))
            // {
            //     this.SetAniNormalizedTime(0.9f);
            // }
            #endregion
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读