Unity播放序列帧,功能丰富
话不多说,直接上代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
namespace JiuBa
{
public class ImageAnimation : MonoBehaviour
{
public enum State
{
idle,
playing,
pause
}
public enum State1
{
once,
loop
}
[Header("播放方式(循环、单次)")]
public State1 condition = State1.once;
[Header("自动播放")]
public bool Play_Awake = false;
[Header("不循环播放时,播放完最后一帧时,是否停留在最后一帧")]
public bool isStop = true;
[Header("滑动屏幕使序列帧正反播放")]
public bool isDragScrenn=false;
private State play_state;
private Image image;
[Header("每秒播放的帧数(整数)")]
public float frame_number;
[Header("将序列拖进来")]
public Sprite[] sprit_arr;
private Vector2 firstPos;
private Vector2 secondPos;
private bool isDrag=false;
public UnityEvent onCompleteEvent;
private int index;
private float tim;
private float waittim;
private bool isplay;
void Awake()
{
image = GetComponent<Image>();
tim = 0;
index = 0;
waittim = 1 / frame_number;
play_state = State.idle;
isplay = false;
if (image == null)
{
print("Image为空,请添加Image组件!!!");
return;
}
if (sprit_arr.Length < 1)
{
print("sprite数组为0,请给sprite数组添加元素!!!");
}
image.sprite = sprit_arr[0];
if (Play_Awake)
{
Play();
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
Play();
}
if (Input.GetKeyDown(KeyCode.S))
{
Replay();
}
if (Input.GetKeyDown(KeyCode.D))
{
Stop();
}
if (Input.GetKeyDown(KeyCode.P))
{
Pause();
}
if (!isDrag)
{
UpMove();
print("2");
}
if (isDragScrenn)
{
if (Input.GetMouseButtonDown(0))
{
isDrag = true;
}
if (Input.GetMouseButtonUp(0))
{
isDrag = false;
}
}
}
private void UpMove()
{
if (condition == State1.once)
{
if (play_state == State.idle && isplay)
{
play_state = State.playing;
index = 0;
tim = 0;
}
if (play_state == State.pause && isplay)
{
play_state = State.playing;
tim = 0;
}
if (play_state == State.playing && isplay)
{
tim += Time.deltaTime;
if (tim >= waittim)
{
tim = 0;
index++;
if (index >= sprit_arr.Length)
{
if (!isStop)
{
index = 0;
image.sprite = sprit_arr[index];
isplay = false;
play_state = State.idle;
}
else
{
print("1");
index = sprit_arr.Length - 1;
image.sprite = sprit_arr[index];
isplay = false;
}
if (onCompleteEvent != null)
{
onCompleteEvent.Invoke();
return;
}
}
image.sprite = sprit_arr[index];
}
}
}
if (condition == State1.loop)
{
if (play_state == State.idle && isplay)
{
play_state = State.playing;
index = 0;
tim = 0;
}
if (play_state == State.pause && isplay)
{
play_state = State.playing;
tim = 0;
}
if (play_state == State.playing && isplay)
{
tim += Time.deltaTime;
if (tim >= waittim)
{
tim = 0;
index++;
if (index >= sprit_arr.Length)
{
index = 0;
}
image.sprite = sprit_arr[index];
}
}
}
}
private void OnGUI()
{
if (isDrag)
{
if (Event.current.type == EventType.MouseDown)
{
firstPos = Event.current.mousePosition;
}
if (Event.current.type == EventType.MouseDrag)
{
secondPos = Event.current.mousePosition;
}
if (secondPos.x < firstPos.x)
{
if (index >= sprit_arr.Length - 1)
{
index = 0;
}
else
{
index++;
}
}
else if (secondPos.x > firstPos.x)
{
if (index == 0)
{
index = sprit_arr.Length - 1;
}
else
{
index--;
}
}
firstPos = secondPos;
image.sprite = sprit_arr[index];
}
}
public void Play()
{
isplay = true;
}
public void Pause()
{
isplay = false;
play_state = State.pause;
}
public void Stop()
{
isplay = false;
play_state = State.idle;
index = 0;
tim = 0;
if (image == null)
{
print("Image为空,请赋值");
return;
}
image.sprite = sprit_arr[index];
}
public void Replay()
{
isplay = true;
play_state = State.playing;
index = 0;
tim = 0;
}
}
}
如果帮助了您,记得给个好评哦! 视频详解地址:https://www.bilibili.com/video/BV1qL4y1v7gj?share_source=copy_web
|