fix:代码提交
This commit is contained in:
88
Assets/Scripts/Anim/SpriteAnim.cs
Normal file
88
Assets/Scripts/Anim/SpriteAnim.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class SpriteAnim : MonoBehaviour
|
||||
{
|
||||
public bool isLoop = true;
|
||||
public float timer = 0.1f;
|
||||
public Sprite[] frames;
|
||||
|
||||
private int index = 0;
|
||||
|
||||
private SpriteRenderer spriteRender;
|
||||
private Image image;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
spriteRender = GetComponent<SpriteRenderer>();
|
||||
image = GetComponent<Image>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
index = 0;
|
||||
Play();
|
||||
}
|
||||
|
||||
|
||||
public void Play()
|
||||
{
|
||||
CancelInvoke("PlayAnim");
|
||||
InvokeRepeating("PlayAnim", 0f, timer);
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
CancelInvoke("PlayAnim");
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
if (index >= frames.Length)
|
||||
{
|
||||
CancelInvoke("PlayAnim");
|
||||
return;
|
||||
}
|
||||
index = 0;
|
||||
if (spriteRender != null)
|
||||
{
|
||||
spriteRender.sprite = frames[index];
|
||||
}
|
||||
if (image != null)
|
||||
{
|
||||
image.sprite = frames[index];
|
||||
}
|
||||
}
|
||||
|
||||
void PlayAnim()
|
||||
{
|
||||
|
||||
if (index >= frames.Length)
|
||||
{
|
||||
CancelInvoke("PlayAnim");
|
||||
return;
|
||||
}
|
||||
if (spriteRender != null)
|
||||
{
|
||||
spriteRender.sprite = frames[index];
|
||||
}
|
||||
if (image != null)
|
||||
{
|
||||
image.sprite = frames[index];
|
||||
}
|
||||
index++;
|
||||
if (index >= frames.Length)
|
||||
{
|
||||
if (isLoop)
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
CancelInvoke("PlayAnim");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user