自己写的UnitY实用小工具或脚本——读取Texture序列帧动画
来源:互联网 发布:江苏大学网络教学平台 编辑:程序博客网 时间:2024/05/01 02:23
也许你会喷我,为啥要用序列帧做动画,又占内存而且也不好控制。作为程序员,有什么办法呢!我们的使命就是完成上面给出的一切要求。在接到这个任务的时候,我并没有想到更好的办法(如果你有的话欢迎你告诉我)。序列帧还不止一套,最多的一套有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<Texture2D> m_Tex2DcollectBg=new List<Texture2D>(); //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<UITexture>(); //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<ResourceRequest>(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<m_Tex2DcollectBg.Count-1;j++) { yield return new WaitForSeconds(playSpeed); m_uiTexCollectBg.mainTexture = m_Tex2DcollectBg[j]; } } else { int j = 0; while (true) //for (int j = 0; j < max; j++) { if (playStyle == AnimPalyStyle.Loop) { yield return new WaitForSeconds(playSpeed); m_uiTexCollectBg.mainTexture = m_Tex2DcollectBg[j]; j++; if (j == m_Tex2DcollectBg.Count-1) { j = 0; } //Debug.Log(j); } else if(playStyle==AnimPalyStyle.Poop) { while(j<max) { if (j == -1) j = 0; yield return new WaitForSeconds(playSpeed); m_uiTexCollectBg.mainTexture = m_Tex2DcollectBg[j]; j++; } while(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<Texture2D>(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 framevoid Update () {}
public enum AnimPalyStyle : byte { Once, Loop, Poop }}
功能有播放一次、循环播放、和Poop播放三种效果,可以控制速度等如图所示:
0 0
- 自己写的UnitY实用小工具或脚本——读取Texture序列帧动画
- 自己写的UnitY实用小工具或脚本——文本管理器
- 自己写的UnitY实用小工具或脚本——AR阴影以及Mask遮挡
- Unity实用小工具或脚本—加载外部图片的三种方式
- Unity实用小工具或脚本—自制2D碰撞体
- 写的一个UGUI下Image控件做序列帧动画的小工具
- 自己写的小工具---JarCleaner
- 自己写个小工具——图片水印生成器
- 实用的小工具
- 内核小工具自己写
- ubuntu 的使用(三)—— 实用小工具
- JS_小工具_自己写了一个画像素画的小工具
- 自己写的阅读时使用的小工具
- C#写的浮点或汉字转十六进制小工具
- 自己写的国际化时Properties处理小工具
- 自己js写的分页小工具供大家参考
- 自己写的自动化部署小工具 : Webhooks
- java--自己的小工具
- PowerDesigner使用教程
- Android实现兼容所有手机从本地SD上拿到到URL
- HDU 3033------I love sneakers!
- EasyDarwin+ffmpeg进行PC(摄像头+麦克风)流媒体直播服务
- 《Redis Sentinel机制与用法(一)》
- 自己写的UnitY实用小工具或脚本——读取Texture序列帧动画
- 6. head 区的设置
- 关于QT下资源使用和资源占用内存过多的问题
- Failed to load platform plugin "windows"
- 第十三节 网络编程
- GRE写作必备句型
- POJ 3279 Fliptile (二进制+搜索)
- NSArray(不可变数组)、NSMutableArray(可变数组)、数组排序、NSNumber(多态:数值和对象互转)、NSValue(将结构体转换成对象)
- EJB笔记---JBoss 开发环境搭配