自己写的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 List
m_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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:自己写的UnitY实用小工具或脚本——AR阴影以及Mask遮挡
下一篇:自己写的UnitY实用小工具或脚本——文本管理器

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月12日 15时50分16秒