当前位置: 首页 > article >正文

⭐ Unity 序列帧播放脚本

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;
        }

    }
}

感谢大家的观看,您的点赞和关注是我最大的动力

不定时更新知识点和干货呦~


http://www.kler.cn/news/365580.html

相关文章:

  • 笔记本电脑充不进去电怎么回事 笔记本电脑充不上电解决
  • Android 判断手机放置的方向
  • 重生之“我打数据结构,真的假的?”--3.栈和队列(无习题)
  • 微信小程序性能优化 ==== 合理使用 setData 纯数据字段
  • 深入了解 kotlinx-datetime:配置与使用指南
  • 同世界,共北斗|遨游通讯亮相第三届北斗规模应用国际峰会!
  • NVIDIA发布Nemotron-70B-Instruct,超越GPT-4o和Claude 3.5的AI模型
  • 提示词高级阶段学习day3.2结构化 Prompt 的优势
  • 什么是3D展厅?有哪些应用场景?
  • 0160____declspec(noreturn)的用法
  • vue3中watch的用法以及使用场景以及与watchEffect的使用对比
  • 数据清洗-总结
  • 如何从iconfont中获取字体图标并应用到微信小程序中去?
  • 【个人记录】新电脑的配置截图和电源选项的修改
  • Python爬虫:从入门到精通
  • 微信小程序中关闭默认的 `navigationBar`,并使用自定义的 `nav-bar` 组件
  • 文案语音图片视频管理分析系统-视频矩阵
  • 使用frp0.61.0透传局域网的https服务到自有域名
  • AWS免费账号付费了能退吗
  • 英语语法笔记
  • 低代码(Low Code)是什么?解决了哪些问题?适合在什么业务场景中用?
  • 基于SSM+小程序的垃圾分类管理系统(垃圾2)
  • 青少年编程与数学 02-002 Sql Server 数据库应用 08课题、索引的操作
  • supermall项目上拉加载bug分析
  • Word中Normal.dotm样式模板文件
  • [RK3566-Android11] 使用SPI方式点LED灯带-JE2815/WS2812,实现呼吸/渐变/随音量变化等效果