自己写的UnitY实用小工具或脚本——读取Texture序列帧动画
发布日期:2021-06-29 04:53:49
浏览次数:2
分类:技术文章
本文共 3613 字,大约阅读时间需要 12 分钟。
也许你会喷我,为啥要用序列帧做动画,又占内存而且也不好控制。作为程序员,有什么办法呢!我们的使命就是完成上面给出的一切要求。在接到这个任务的时候,我并没有想到更好的办法(如果你有的话欢迎你告诉我)。序列帧还不止一套,最多的一套有500多张,最少的也有100多张,有作为移动端的背景动画,也有在电脑端的动画效果序列帧。
using UnityEngine;using System.Collections;using EnumState;using System.Collections.Generic;public class SpriteAnim : MonoBehaviour { private int m_currentTexIndex = 0; public int m_max; private Listm_Tex2DcollectBg=new List (); //private Texture2D[] m_Tex2DcollectBg = new Texture2D[500]; private UITexture m_uiTexCollectBg; private float m_timeCounter; public string path; public float playSpeed; public bool isAsync; public AnimPalyStyle playStyle; public delegate void AnimOnFinished(); public int skipIndex = 1; void Start() { //path = "Compositiontable/合成台_0000"; // StartCoroutine(LoadTexture(m_max, "Compositiontable/合成台_0000")); } void OnEnable() { m_uiTexCollectBg = this.GetComponent (); //StopAllCoroutines(); if (isAsync) StartCoroutine(LoadTextureAsync(m_max, path)); else StartCoroutine(LoadTexture(m_max, path)); m_uiTexCollectBg.mainTexture = null; } IEnumerator LoadTextureAsync(int max, string name) { ResourceRequest request; int nameLenght = name.Length; for (int i = 0; i < max; i+=skipIndex) { name = name.Substring(0, nameLenght - i.ToString().Length + 1) + i; // Debug.Log(name); request = Resources.LoadAsync(name); //request = Resources.Load (name); yield return request; Texture2D temp= request.asset as Texture2D; m_uiTexCollectBg.mainTexture =temp; if (!m_Tex2DcollectBg.Contains(temp)) { m_Tex2DcollectBg.Add(request.asset as Texture2D); } } if (playStyle == AnimPalyStyle.Once) { for(int j=0;j 0) { if (j == m_Tex2DcollectBg.Count) j = m_Tex2DcollectBg.Count - 1; yield return new WaitForSeconds(playSpeed); m_uiTexCollectBg.mainTexture = m_Tex2DcollectBg[j]; j--; } } } } } IEnumerator LoadTexture(int max ,string name) { Texture2D tempTex; int j = 0; int nameLenght = name.Length; for (; j < max; j++) { name = name.Substring(0, nameLenght - j.ToString().Length + 1) + j; // Debug.Log(name); tempTex = Resources.Load (name); yield return new WaitForSeconds(playSpeed); m_uiTexCollectBg.mainTexture = tempTex; if (!m_Tex2DcollectBg.Contains(tempTex)) { m_Tex2DcollectBg.Add(tempTex); } if(playStyle==AnimPalyStyle.Once) this.enabled = false; } j = 0; if (playStyle == AnimPalyStyle.Loop) { for (; j < max; j++) { yield return new WaitForSeconds(playSpeed); m_uiTexCollectBg.mainTexture = m_Tex2DcollectBg[j]; if (j == max - 1) { j = 0; } } } else if (playStyle == AnimPalyStyle.Poop) { while (true) { while (j < max) { if (j == -1) j = 0; yield return new WaitForSeconds(playSpeed); m_uiTexCollectBg.mainTexture = m_Tex2DcollectBg[j]; j++; } while (j > -1) { if (j == max) j = max - 1; yield return new WaitForSeconds(playSpeed); m_uiTexCollectBg.mainTexture = m_Tex2DcollectBg[j]; j--; } } } } // Update is called once per frame void Update () { }
public enum AnimPalyStyle : byte { Once, Loop, Poop }}功能有播放一次、循环播放、和Poop播放三种效果,可以控制速度等如图所示:
转载地址:https://blog.csdn.net/zhangxiao13627093203/article/details/46928939 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月12日 15时50分16秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
解密小米生态链:从构建到定义产品
2019-04-29
张一鸣的流量生意
2019-04-29
重新定义马桶
2019-04-29
从移动互联网困局漫谈人工智能的未来
2019-04-29
如何设计一款让用户“上瘾”的产品?
2019-04-29
墙裂推荐几款桌面应用
2019-04-29
如何避免掉进心理陷阱?
2019-04-29
从价值观选择到重构思维模型
2019-04-29
如何识别推理过程中的谬误?
2019-04-29
【周末分享】基因编辑技术的现状与未来
2019-04-29
脑机融合,当梦想照进现实
2019-04-29
请回答2019
2019-04-29
Qt开发的图标登录游戏设计
2019-04-29
spring4使用外部属性文件配置问题
2019-04-29
easyconnect一直初始化无法连上问题
2019-04-29
安装 SPRING TOOL SUITE以及没有namespace解决办法
2019-04-29
java.lang.NoSuchFieldException
2019-04-29
mysql找到相对应mysql-connector-java-xxx.jar的方法
2019-04-29
Android studio成功运行自带模拟器问题详解,捎带中文用户名路径有感
2019-04-29