UGUI 序列帧动画脚本

2023-10-11 15:48
文章标签 脚本 动画 ugui 序列帧

本文主要是介绍UGUI 序列帧动画脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

using System.Collections;
using System.Collections.Generic;
using System;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;

public enum SequencePatternType
{
    /// <summary>
    /// 正循环//自动播放
    /// </summary>
    ForthLoop,

    /// <summary>
    /// 反循环//自动播放
    /// </summary>
    BackLoop,

    /// <summary>
    /// 正反循环//自动播放
    /// </summary>
    ForthBackLoop,

    /// <summary>
    /// 正一次//按钮点击事件
    /// </summary>
    ForthOne,

    /// <summary>
    /// 反一次//按钮点击事件
    /// </summary>
    BackOne,

    /// <summary>
    /// 正反一次//按钮点击事件
    /// </summary>
    ForthBackOne,

    /// <summary>
    /// 空
    /// </summary>
    Max
}

/// <summary>
/// forth back pattern type
/// </summary>
public enum ForthBackState
{
    Forth,
    ForthStop,
    BackStop,
    Back
}

/// <summary>
/// 序列帧动画播放
/// 19.06.28 by Rocketjie
/// </summary>
public class SequenceSprite : MonoBehaviour {

    /// <summary>
    /// sprites 播放模式
    /// </summary>
    [Tooltip("Sequence Pattern (Default Is Loop)")]
    public SequencePatternType PatternType = SequencePatternType.ForthLoop;

    /// <summary>
    /// SpriteList(当SpriteList添加图片后不再读取Texture2D)
    /// </summary>
    [Tooltip("Sequence Sprite List")]
    public List<Sprite> SpriteList = new List<Sprite>();

    /// <summary>
    /// Texture2D
    /// 图集中图片数量大于1;
    /// 图集中图片名称格式为:字母 + “_” + 数字(数字必须以0开始);
    /// 图集中图片名称格式例如:effect_0、effect_1、effect_2 ;
    /// 当SpriteList没有添加图片时读取Texture2D
    /// </summary>
    [Tooltip("Texture2D Atlas")]
    public Texture2D Texture2DAtlas = null;

    /// <summary>
    /// 间隔时间
    /// </summary>
    [Tooltip("Sequence Sprite Turn Time")]
    public float TurnTime = 0.05f;

    /// <summary>
    /// 序列帧播放 sprite
    /// </summary>
    [Tooltip("Sprite Is Self Component (Default)")]
    public Image TargetSprite = null;

    /// <summary>
    /// 序列帧播放 触发按钮
    /// </summary>
    [Tooltip("Button Is Self Component (Default)")]
    public Button TargetButton = null;

    private int img_index = 0;
    private float deltTime = 0;
    private SequencePatternType currentType = SequencePatternType.Max;

    private void Awake()
    {
        if (TargetSprite == null)
        {
            TargetSprite = gameObject.GetComponent<Image>();
        }

        if (TargetButton == null)
        {
            TargetButton = gameObject.GetComponent<Button>();
        }

        if (SpriteList.Count <= 0 && Texture2DAtlas != null)
        {
            string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(Texture2DAtlas));
            string path = rootPath + "/" + Texture2DAtlas.name + ".PNG";
            TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;
            if(texImp.spritesheet.Length <= 1)
            {
                Logger.LogError("Texture2D Format incorrect");
                return;
            }

            int indexOf = 0;
            for (int index = 0; index < texImp.spritesheet.Length; index++)
            {
                foreach (SpriteMetaData temp in texImp.spritesheet)
                {
                    indexOf = temp.name.IndexOf('_');
                    if (indexOf == -1)
                    {
                        Logger.LogError("Texture2D Format incorrect");
                        return;
                    }
                    else
                    {
                        if (int.Parse(temp.name.Substring(indexOf + 1)) == index)
                        {
                            Sprite spr = Sprite.Create(Texture2DAtlas, temp.rect, Vector2.zero);
                            SpriteList.Add(spr);
                        }
                    }
                }
            }
        }
    }


    // Use this for initialization
    void Start ()
    {
        if(TargetButton != null)
        {
            TargetButton.onClick.AddListener(OnClick);
        }
    }

    /// <summary>
    /// button click
    /// </summary>
    private void OnClick()
    {
        switch (PatternType)
        {
            case SequencePatternType.BackOne:
                SetBackOneState();
                break;
            case SequencePatternType.ForthOne:
                SetForthOneState();
                break;
            case SequencePatternType.ForthBackOne:
                SetForthBackOneState();
                break;
        }
    }


    /// <summary>
    /// 设置 forth one state
    /// </summary>
    private void SetForthOneState()
    {
        if (forthOne_State == ForthBackState.ForthStop)
        {
            forthOne_State = ForthBackState.Forth;
        }
    }

    /// <summary>
    /// 设置 back one state
    /// </summary>
    private void SetBackOneState()
    {
        if (backOne_State == ForthBackState.BackStop)
        {
            backOne_State = ForthBackState.Back;
        }
    }

    /// <summary>
    /// 设置 forth back one state
    /// </summary>
    private void SetForthBackOneState()
    {
        if (forthBackOneState == ForthBackState.ForthStop)
        {
            forthBackOneState = ForthBackState.Back;
        }
        else if (forthBackOneState == ForthBackState.BackStop)
        {
            forthBackOneState = ForthBackState.Forth;
        }
    }

    /// <summary>
    /// 检测 pattern type
    /// </summary>
    private void CheckPatterType()
    {
        if (currentType != PatternType)
        {
            currentType = PatternType;

            switch (PatternType)
            {
                case SequencePatternType.ForthLoop:
                    img_index = 0;
                    break;
                case SequencePatternType.BackLoop:
                    img_index = SpriteList.Count - 1;
                    break;
                case SequencePatternType.ForthBackLoop:
                    img_index = 0;
                    forthBack_loopState = ForthBackState.Forth;
                    break;
                case SequencePatternType.ForthOne:
                    img_index = 0;
                    forthOne_State = ForthBackState.ForthStop;
                    break;
                case SequencePatternType.BackOne:
                    img_index = SpriteList.Count - 1;
                    backOne_State = ForthBackState.BackStop;
                    break;
                case SequencePatternType.ForthBackOne:
                    img_index = 0;
                    forthBackOneState = ForthBackState.BackStop;
                    break;
            }
        }
    }


    private void SetImageSprite()
    {
        if (img_index >= 0 && img_index < SpriteList.Count)
        {
            TargetSprite.sprite = SpriteList[img_index];
        }
        else
        {
            Logger.LogError("SequenceSprite SpriteList'img_index is error !");
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (TargetSprite == null || SpriteList.Count <= 1)
        {
            return;
        }

        CheckPatterType();

        switch (PatternType)
        {
            case SequencePatternType.ForthLoop:
                SequenceForthLoop();
                break;

            case SequencePatternType.BackLoop:
                SequenceBackLoop();
                break;

            case SequencePatternType.ForthBackLoop:
                SequenceForthBackLoop();
                break;

            case SequencePatternType.ForthOne:
                SequenceForthOne();
                break;

            case SequencePatternType.BackOne:
                SequenceBackOne();
                break;
            case SequencePatternType.ForthBackOne:
                SequenceForthBackOne();
                break;
        }
    }


    #region Forth Loop
    /// <summary>
    /// Forth Loop
    /// </summary>
    private void SequenceForthLoop()
    {
        deltTime += Time.deltaTime;

        if (deltTime > TurnTime)
        {
            deltTime = 0;
            SetImageSprite();//Forth Loop
            img_index++;

            if (img_index == SpriteList.Count - 1)
            {
                img_index = 0;
            }
        }
    }
    #endregion Forth Loop

 

    #region Back Loop
    /// <summary>
    /// Back Loop
    /// </summary>
    private void SequenceBackLoop()
    {
        deltTime += Time.deltaTime;

        if (deltTime > TurnTime)
        {
            deltTime = 0;
            SetImageSprite();//Back Loop
            img_index--;

            if (img_index == 0)
            {
                img_index = SpriteList.Count - 1;
            }
        }

    }
    #endregion Back Loop

 

    #region Forth Back Loop

    /// <summary>
    /// Forth Back Loop 状态
    /// </summary>
    private ForthBackState forthBack_loopState = ForthBackState.Forth;

    /// <summary>
    /// Forth Back Loop
    /// </summary>
    private void SequenceForthBackLoop()
    {
        deltTime += Time.deltaTime;

        if (deltTime > TurnTime)
        {
            deltTime = 0;
            SetImageSprite();//Forth Back Loop

            if (forthBack_loopState == ForthBackState.Forth)
            {
                img_index++;
            }
            else if (forthBack_loopState == ForthBackState.Back)
            {
                img_index--;
            }

            if (img_index >= SpriteList.Count - 1)
            {
                img_index = SpriteList.Count - 1;
                forthBack_loopState = ForthBackState.Back;
            }
            else if (img_index <= 0)
            {
                img_index = 0;
                forthBack_loopState = ForthBackState.Forth;
            }
        }
    }
    #endregion Forth Back Loop

 

    #region Forth One

    /// <summary>
    /// Forth One 状态
    /// </summary>
    private ForthBackState forthOne_State = ForthBackState.ForthStop;

    /// <summary>
    /// Forth One
    /// </summary>
    private void SequenceForthOne()
    {
        if (forthOne_State == ForthBackState.Forth)
        {
            deltTime += Time.deltaTime;

            if (deltTime > TurnTime)
            {
                deltTime = 0;
                SetImageSprite();//Forth One
                img_index++;

                if (img_index == SpriteList.Count - 1)
                {
                    img_index = 0;
                    forthOne_State = ForthBackState.ForthStop;
                }
            }
        }
    }
    #endregion Forth One

 

    #region Back One

    /// <summary>
    /// Back One 状态
    /// </summary>
    private ForthBackState backOne_State = ForthBackState.BackStop;

    /// <summary>
    /// Back One
    /// </summary>
    private void SequenceBackOne()
    {
        if (backOne_State == ForthBackState.Back)
        {
            deltTime += Time.deltaTime;

            if (deltTime > TurnTime)
            {
                deltTime = 0;
                SetImageSprite();//Back One
                img_index--;

                if (img_index == 0)
                {
                    img_index = SpriteList.Count - 1;
                    backOne_State = ForthBackState.BackStop;
                }
            }
        }
    }
    #endregion Back One

 

    #region Forth Back One

    /// <summary>
    /// Forth Back One 状态
    /// </summary>
    private ForthBackState forthBackOneState = ForthBackState.BackStop;

    /// <summary>
    /// forth back one
    /// </summary>
    private void SequenceForthBackOne()
    {
        if (forthBackOneState == ForthBackState.Forth)
        {
            deltTime += Time.deltaTime;

            if (deltTime > TurnTime)
            {
                deltTime = 0;
                SetImageSprite();//Forth Back One (Forth)
                img_index++;
            }

            if (img_index == SpriteList.Count - 1)
            {
                forthBackOneState = ForthBackState.ForthStop;
            }
        }
        else if (forthBackOneState == ForthBackState.Back)
        {
            deltTime += Time.deltaTime;
            if (deltTime > TurnTime)
            {
                deltTime = 0;
                SetImageSprite();//Forth Back One (Back)
                img_index--;
            }

            if (img_index == 0)
            {
                forthBackOneState = ForthBackState.BackStop;
            }
        }
    }
    #endregion Forth Back One
}

 

这篇关于UGUI 序列帧动画脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/189119

相关文章

Java调用Python脚本实现HelloWorld的示例详解

《Java调用Python脚本实现HelloWorld的示例详解》作为程序员,我们经常会遇到需要在Java项目中调用Python脚本的场景,下面我们来看看如何从基础到进阶,一步步实现Java与Pyth... 目录一、环境准备二、基础调用:使用 Runtime.exec()2.1 实现步骤2.2 代码解析三、

Python脚本轻松实现检测麦克风功能

《Python脚本轻松实现检测麦克风功能》在进行音频处理或开发需要使用麦克风的应用程序时,确保麦克风功能正常是非常重要的,本文将介绍一个简单的Python脚本,能够帮助我们检测本地麦克风的功能,需要的... 目录轻松检测麦克风功能脚本介绍一、python环境准备二、代码解析三、使用方法四、知识扩展轻松检测麦

基于Python Playwright进行前端性能测试的脚本实现

《基于PythonPlaywright进行前端性能测试的脚本实现》在当今Web应用开发中,性能优化是提升用户体验的关键因素之一,本文将介绍如何使用Playwright构建一个自动化性能测试工具,希望... 目录引言工具概述整体架构核心实现解析1. 浏览器初始化2. 性能数据收集3. 资源分析4. 关键性能指

shell脚本批量导出redis key-value方式

《shell脚本批量导出rediskey-value方式》为避免keys全量扫描导致Redis卡顿,可先通过dump.rdb备份文件在本地恢复,再使用scan命令渐进导出key-value,通过CN... 目录1 背景2 详细步骤2.1 本地docker启动Redis2.2 shell批量导出脚本3 附录总

Oracle数据库定时备份脚本方式(Linux)

《Oracle数据库定时备份脚本方式(Linux)》文章介绍Oracle数据库自动备份方案,包含主机备份传输与备机解压导入流程,强调需提前全量删除原库数据避免报错,并需配置无密传输、定时任务及验证脚本... 目录说明主机脚本备机上自动导库脚本整个自动备份oracle数据库的过程(建议全程用root用户)总结

linux下shell脚本启动jar包实现过程

《linux下shell脚本启动jar包实现过程》确保APP_NAME和LOG_FILE位于目录内,首次启动前需手动创建log文件夹,否则报错,此为个人经验,供参考,欢迎支持脚本之家... 目录linux下shell脚本启动jar包样例1样例2总结linux下shell脚本启动jar包样例1#!/bin

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

Golang实现Redis分布式锁(Lua脚本+可重入+自动续期)

《Golang实现Redis分布式锁(Lua脚本+可重入+自动续期)》本文主要介绍了Golang分布式锁实现,采用Redis+Lua脚本确保原子性,持可重入和自动续期,用于防止超卖及重复下单,具有一定... 目录1 概念应用场景分布式锁必备特性2 思路分析宕机与过期防止误删keyLua保证原子性可重入锁自动