Unity教程之-自定义扩展Editor读取Texture序列帧动画

 

本篇文章我们来学习下自定义扩展Editor读取Texture序列帧动画,也许你会喷我,为啥要用序列帧做动画,又占内存而且也不好控制。作为程序员,有什么办法呢!我们的使命就是完成上面给出的一切要求。在接到这个任务的时候,我并没有想到更好的办法(如果你有的话欢迎你告诉我)。序列帧还不止一套,最多的一套有500多张,最少的也有100多张,有作为移动端的背景动画,也有在电脑端的动画效果序列帧。功能有播放一次、循环播放、和Poop播放三种效果,可以控制速度等如图所示:

好了废话不说,贴上代码


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 frame
void Update () {

}

好了,本篇unity3d教程到此结束,下篇我们再会!