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;
}
}
}
感谢大家的观看,您的点赞和关注是我最大的动力
不定时更新知识点和干货呦~