自己写的UnitY实用小工具或脚本——读取Texture序列帧动画

本文介绍了一种使用Unity实现序列帧动画的方法,包括加载纹理资源、控制播放速度及播放模式等关键技术点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

也许你会喷我,为啥要用序列帧做动画,又占内存而且也不好控制。作为程序员,有什么办法呢!我们的使命就是完成上面给出的一切要求。在接到这个任务的时候,我并没有想到更好的办法(如果你有的话欢迎你告诉我)。序列帧还不止一套,最多的一套有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 frame
	void Update () {
	
	}
 public enum AnimPalyStyle : byte
    {
        Once, Loop, Poop
    }
}

功能有播放一次、循环播放、和Poop播放三种效果,可以控制速度等如图所示:
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值