当前位置:   article > 正文

unity序列帧动画_unity 序列帧动画

unity 序列帧动画
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class FrameAnimator : MonoBehaviour
{
    /// <summary>
    /// 序列帧
    /// </summary>
    public Sprite[] Frames
    {
        get => frames;
        set => frames = value;
    }
    [SerializeField] private Sprite[] frames = null;


    /// <summary>
    /// 帧率,为正时正向播放,为负时反向播放
    /// </summary>
    public float Framerate
    {
        get => framerate;
        set => framerate = value;
    }
    [SerializeField] private float framerate = 20.0f;

    /// <summary>
    /// 是否忽略timeScale
    /// </summary>
    public bool IgnoreTimeScale
    {
        get => ignoreTimeScale;
        set => ignoreTimeScale = value;
    }
    [SerializeField] private bool ignoreTimeScale = true;

    /// <summary>
    /// 是否循环
    /// </summary>
    public bool Loop
    {
        get => loop;
        set => loop = value;
    }
    [SerializeField] private bool loop = true;

    //动画曲线
    [SerializeField]
    private AnimationCurve curve = new AnimationCurve(new Keyframe(0, 1, 0, 0), new Keyframe(1, 1, 0, 0));

    /// <summary>
    /// 结束事件
    /// 在每次播放完一个周期时触发
    /// 在循环模式下触发此事件时,当前帧不一定为结束帧
    /// </summary>
    public event Action FinishEvent;

    //目标Image组件
    private Image image;

    //目标SpriteRenderer组件
    private SpriteRenderer spriteRenderer;

    //当前帧索引
    private int currentFrameIndex = 0;

    //下一次更新时间
    private float timer = 0.0f;

    //当前帧率,通过曲线计算而来
    private float currentFramerate = 20.0f;

    /// <summary>
    /// 重设动画
    /// </summary>
    public void Reset()
    {
        currentFrameIndex = framerate < 0 ? frames.Length - 1 : 0;
    }

    /// <summary>
    /// 从停止的位置播放动画
    /// </summary>
    public void Play()
    {
        this.enabled = true;
    }

    /// <summary>
    /// 暂停动画
    /// </summary>
    public void Pause()
    {
        this.enabled = false;
    }

    /// <summary>
    /// 停止动画,将位置设为初始位置
    /// </summary>
    public void Stop()
    {
        Pause();
        Reset();
    }

    //自动开启动画
    void Start()
    {
        image = this.GetComponent<Image>();
        spriteRenderer = this.GetComponent<SpriteRenderer>();
    #if UNITY_EDITOR
        if (image == null && spriteRenderer == null)
        {
            Debug.LogWarning("No available component found. 'Image' or 'SpriteRenderer' required.", this.gameObject);
        }
    #endif
    }

    void Update()
    {
        //帧数据无效,禁用脚本
        if (frames == null || frames.Length == 0)
        {
            this.enabled = false;
        }
        else
        {
            //从曲线值计算当前帧率
            float curveValue = curve.Evaluate((float) currentFrameIndex / frames.Length);
            float curvedFramerate = curveValue * framerate;
            //帧率有效
            if (curvedFramerate != 0)
            {
                //获取当前时间
                float time = ignoreTimeScale ? Time.unscaledTime : Time.time;
                //计算帧间隔时间
                float interval = Mathf.Abs(1.0f / curvedFramerate);
                //满足更新条件,执行更新操作
                if (time - timer > interval)
                {
                    //执行更新操作
                    DoUpdate();
                }
            }
            #if UNITY_EDITOR
            else
            {
                Debug.LogWarning("Framerate got '0' value, animation stopped.");
            }
            #endif
        }
    }

    //具体更新操作
    private void DoUpdate()
    {
        //计算新的索引
        int nextIndex = currentFrameIndex + (int) Mathf.Sign(currentFramerate);
        //索引越界,表示已经到结束帧
        if (nextIndex < 0 || nextIndex >= frames.Length)
        {
            //广播事件
            FinishEvent?.Invoke();

            //非循环模式,禁用脚本
            if (loop == false)
            {
                currentFrameIndex = Mathf.Clamp(currentFrameIndex, 0, frames.Length - 1);
                this.enabled = false;
                return;
            }
        }

        //钳制索引
        currentFrameIndex = nextIndex % frames.Length;
        //更新图片
        if (image is not null)
        {
            image.sprite = frames[currentFrameIndex];
        }
        else if (spriteRenderer is not null)
        {
            spriteRenderer.sprite = frames[currentFrameIndex];
        }

        //设置计时器为当前时间
        timer = ignoreTimeScale ? Time.unscaledTime : Time.time;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/138218
推荐阅读
相关标签
  

闽ICP备14008679号