Unity播放序列帧,功能丰富

Unity播放序列帧,功能丰富

话不多说,直接上代码

/***********************************
*    Description:描述这是一个图片序列帧播放脚本,适用于少量的一组序列帧,且分辨率较小,若出现花屏,打包不成功等情况,请先排除序列帧分辨率过大、序列帧组数过多
*    Mountpoint:将其挂载在Image组件上
*    Date:2021.11
*    Version:
*    Author:九八
***********************************/
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;

        //自身的image组件
        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;
                                //return;
                            }
                            //此处可添加结束回调函数
                            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];
            }
        }
            

        /// <summary>
        /// 播放
        /// </summary
        public void Play()
        {
            isplay = true;
        }
        /// <summary>
        /// 暂停
        /// </summary>
        public void Pause()
        {
            isplay = false;
            play_state = State.pause;
        }
        /// <summary>
        /// 停止
        /// </summary>
        public void Stop()
        {
            isplay = false;
            play_state = State.idle;
            index = 0;
            tim = 0;
            if (image == null)
            {
                print("Image为空,请赋值");
                return;
            }
            image.sprite = sprit_arr[index];
        }
        /// <summary>
        /// 重播
        /// </summary>
        public void Replay()
        {
            isplay = true;
            play_state = State.playing;
            index = 0;
            tim = 0;
        }

    }
}

如果帮助了您,记得给个好评哦!
视频详解地址:https://www.bilibili.com/video/BV1qL4y1v7gj?share_source=copy_web

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unity_九八

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值