117 lines
3.0 KiB
C#
117 lines
3.0 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.Events;
|
|||
|
|
using UnityEngine.UI;
|
|||
|
|
/********************************************************************************
|
|||
|
|
*Create By CG
|
|||
|
|
*Function 图片序列帧动画
|
|||
|
|
*********************************************************************************/
|
|||
|
|
namespace ZXK.UTility
|
|||
|
|
{
|
|||
|
|
public class SpriteImgAnm : MonoBehaviour
|
|||
|
|
{
|
|||
|
|
[Header("自动播放")]
|
|||
|
|
public bool PlayAwake;
|
|||
|
|
[Header("循环播放")]
|
|||
|
|
public bool Loop = false;
|
|||
|
|
[Tooltip("序列帧的图片")]
|
|||
|
|
[SerializeField]
|
|||
|
|
private Sprite[] m_frames;
|
|||
|
|
[Header("播放速度")]
|
|||
|
|
public float Speed = 0.05f;
|
|||
|
|
[Header("插入事件位置")]
|
|||
|
|
public int ActionFrame = -1;
|
|||
|
|
[SerializeField]
|
|||
|
|
private UnityEvent m_frameEvent;
|
|||
|
|
|
|||
|
|
private SpriteRenderer _container;
|
|||
|
|
private Image _containerImg;
|
|||
|
|
private int _ticked;
|
|||
|
|
private float _time;
|
|||
|
|
private bool _doAnim;
|
|||
|
|
|
|||
|
|
public bool DoAnim { get { return _doAnim; } set { _doAnim = value; } }
|
|||
|
|
|
|||
|
|
private void Awake()
|
|||
|
|
{
|
|||
|
|
_container = GetComponent<SpriteRenderer>();
|
|||
|
|
if (_container == null)
|
|||
|
|
{
|
|||
|
|
_containerImg = GetComponent<Image>();
|
|||
|
|
}
|
|||
|
|
Init();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Init()
|
|||
|
|
{
|
|||
|
|
_ticked = 0;
|
|||
|
|
_time = 0;
|
|||
|
|
_doAnim = PlayAwake;
|
|||
|
|
SetContainerSprite(m_frames[0]);
|
|||
|
|
}
|
|||
|
|
private void SetContainerSprite(Sprite sprite)
|
|||
|
|
{
|
|||
|
|
if (_container != null)
|
|||
|
|
_container.sprite = sprite;
|
|||
|
|
else
|
|||
|
|
_containerImg.sprite = sprite;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Play()
|
|||
|
|
{
|
|||
|
|
_ticked = 0;
|
|||
|
|
_time = 0;
|
|||
|
|
_doAnim = true;
|
|||
|
|
SetContainerSprite(m_frames[0]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Pause()
|
|||
|
|
{
|
|||
|
|
_doAnim = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Resume()
|
|||
|
|
{
|
|||
|
|
_doAnim = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Stop()
|
|||
|
|
{
|
|||
|
|
_ticked = 0;
|
|||
|
|
_time = 0;
|
|||
|
|
_doAnim = false;
|
|||
|
|
SetContainerSprite(m_frames[0]);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void Update()
|
|||
|
|
{
|
|||
|
|
if (_doAnim)
|
|||
|
|
{
|
|||
|
|
_time += Time.deltaTime;
|
|||
|
|
if (_time > Speed)
|
|||
|
|
{
|
|||
|
|
_ticked++;
|
|||
|
|
if (_ticked == m_frames.Length)
|
|||
|
|
{
|
|||
|
|
if (Loop)
|
|||
|
|
{
|
|||
|
|
_ticked = 0;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_ticked = m_frames.Length - 1;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
_time = 0;
|
|||
|
|
|
|||
|
|
if (_ticked == ActionFrame)
|
|||
|
|
m_frameEvent.Invoke();
|
|||
|
|
|
|||
|
|
SetContainerSprite(m_frames[_ticked]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|