⭐ Unity 序列帧播放脚本

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

public class FrameImgControl : MonoBehaviour
{
    private Image img;
    [Header("序列帧")] 
    [SerializeField] private Sprite[] sprites;
    /// <summary>
    /// 计时器
    /// </summary>
    private float m_timer = 0;
    /// <summary>
    /// 下标
    /// </summary>
    private int m_index = 0;
    /// <summary>
    /// 是否播放完毕
    /// </summary>
    private bool isOver = false;
    /// <summary>
    /// 切帧速度
    /// </summary>
    [Header("切帧速度")]
    [SerializeField] private float _framingSpeed = 0.025f;
    /// <summary>
    /// 是否显示子孩子
    /// </summary>
    [Header("显示子对象")]
    [SerializeField] private bool _isShowChild;
    [SerializeField] private bool _isShowChooseChild;
    [SerializeField] private int _chooseChild;
    /// <summary>
    /// 是否重复播放
    /// </summary>
    [Header("重复播放")]
    [SerializeField] private bool _isReplay;
    [SerializeField] private int _replayIndex;
    [SerializeField]private float _replayWaitTime;
    /// <summary>
    /// 是否重复播放
    /// </summary>
    [Header("正反播放")]
    [SerializeField] private bool _isPacReplay;
    private bool _isPlayOver = false;

    /// <summary>
    /// 反向播放
    /// </summary>
    [Header("反向播放")]
    [SerializeField] private bool _isReversePlay;

    void Start()
    {
        if(!_isReversePlay)
        {
            m_index = 0;
            img = transform.GetComponent<Image>();
            img.sprite = sprites[0];
        }
        else
        {
            m_index = sprites.Length - 1;
            img = transform.GetComponent<Image>();
            img.sprite = sprites[m_index];
        }

    }

    void Update()
    {
        if (!isOver)
        {
            if(!_isReversePlay)
            {
                SequenceFramePlay();
            }
            else
            {
                ReversePlay();
            }
            
        }
    }

    public void SequenceFramePlay()
    {
        m_timer += Time.deltaTime;
        if(m_timer >= _framingSpeed)
        {
            if (m_index >= sprites.Length - 2)
            {
                if(_isPacReplay)
                {
                    _isPlayOver = true;

                }
                else
                {
                    if (!_isReplay)
                    {
                        isOver = true;
                        m_index = 0;
                    }
                    else
                    {
                        //m_index = _replayIndex;
                        isOver = true;
                        StartCoroutine(WaitReplayTime());
                    }
                }
                if (_isShowChild)
                {
                    if(_isShowChooseChild)
                    {
                        for (int i = 0; i < _chooseChild; i++)
                        {
                            if (!transform.GetChild(i).gameObject.activeSelf)
                            {
                                transform.GetChild(i).gameObject.SetActive(true);
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < transform.childCount; i++)
                        {
                            if (!transform.GetChild(i).gameObject.activeSelf)
                            {
                                transform.GetChild(i).gameObject.SetActive(true);
                            }
                        }
                    }
                }
            }
            if(m_index <= 0)
            {
                if (_isPacReplay)
                {
                    _isPlayOver = false;
                }
            }

            if (!_isPlayOver)
            {
                m_index++;
            }
            else if(_isPlayOver && _isPacReplay)
            {
                m_index--;
            }
            m_timer = 0;
        }

        img.sprite = sprites[m_index];
    }

    IEnumerator WaitReplayTime()
    {
        yield return new WaitForSeconds(_replayWaitTime);
        isOver = false;
        m_index = _replayIndex;

    }
    //反向播放
    public void ReversePlay()
    {
        m_timer += Time.deltaTime;
        if (m_timer >= _framingSpeed)
        {
            m_timer = 0;
            m_index--;
            if (m_index <= 0)
            {
                isOver = true;
                m_index = 0;
            }
        }

        img.sprite = sprites[m_index];
    }

    private void OnDisable()
    {
        if(!_isReversePlay)
        {
            isOver = false;
            m_timer = 0;
            m_index = 0;
        }
        else
        {
            isOver = false;
            m_timer = 0;
            m_index = sprites.Length - 1;
        }

    }
}

感谢大家的观看,您的点赞和关注是我最大的动力

不定时更新知识点和干货呦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值