Unity3D 动态加载 图片序列正反播放

参考来源

跟来源的电子图书翻页多了点细节上的变化。

using UnityEngine;
using System.Collections;
using System.Resources;

public class MovePic : MonoBehaviour
{
    public Texture2D[] texAll;  //图片序列储存的图片组,注意需要定义这个组的size大小为图片序列的数量
    public GameObject plane;    //指定为图片序列需要在哪个gameobject物体播放
    public string path="img";        //图片序列存放的路径,注意只能存放在Assets的Resources文件夹中,如果没有就自己建立一个。如果是放在Resources文件夹中的img文件夹中,这个填写为img.
    // 如果是放在Resources文件夹中的img文件夹中的1文件夹中,这个填写为img/1. 
    public string fileName = "3DExample7 "; //图片序列文件的文件名称, 比如图片序列为 camera1-1。png camera1-2。png camera1-3。png...... 等等 这个地方就填写名称不变的内容,camera1-  
    //不需要加后缀名.png  另外需要注意序列图片的命名格式 需要以0开始计数,同时 不要在序列号前位数加0. 
    // 比如  camera1-001。png   这需要修改为 camera1-1.png
    public bool xuhuan = true; //定义是否是循环播放
    public int buff = 4;         //定义加载几张图片后清理缓存
    int index = 0;
    int index2 = 0;
    int suliang = 0;
    float T;
    string picNum;
    void Start()
    {
        texAll=new Texture2D[100];
        texAll[0] = (Texture2D)Resources.Load(path + "/" + fileName + "001", typeof(Texture2D));  //首先加载第一张图片显示
    }
    
    void Update()
    {
        
        #if UNITY_IPHONE
        foreach (Touch touch in Input.touches)
        {                        
            if (touch.phase == TouchPhase.Moved  ) 
            {
                T = -Input.GetAxis("Mouse X");            
                if(xuhuan==true)        
                {
                    index=(index+(int)(T*0.5f))%texAll.Length;
                    Mathf.Abs(index);
                    if (index<0)
                    {
                        index2=(texAll.Length-1)+index;    
                    }
                    else
                    {
                        index2=index;
                    }
                }
                else
                {                
                    index=index+(int)(T*0.5f);
                    if (index<=0)
                    {
                        index2=0;
                        index=0;
                    }
                    else if(index>=texAll.Length)
                    {
                        index2=texAll.Length-1;
                        index=texAll.Length-1;
                    }
                    else 
                    {
                        index2=index;
                    }                
                }                    
            }
        }
        #else
        if (Input.GetMouseButton(0))//当鼠标左键点击时候
        {
            T = Input.GetAxis("Mouse X");//获取鼠标x轴移动增量
            
            if (xuhuan == true)//如果定义是循环播放
            {

                index = (index + (int)(T * 3f)) % texAll.Length;//取图片数量的余数
                if (index < 0)
                {
                    index2 = texAll.Length + index;//如果小于0那么就取最大值
                }
                else
                {
                    index2 = index;//其他的相等
                }

            }
            else//如果不是循环
            {
                index = index + (int)(T * 3f);//获取序号
                if (index <= 0)
                {
                    index2 = 0;//小于0就赋值为0
                    index = 0;
                }
                else if (index >= texAll.Length)
                {
                    index2 = texAll.Length - 1;  //大于最大值就赋值为最大值
                    index = texAll.Length - 1;
                }
                else
                {
                    index2 = index;   //其余的相等
                }
            }
        }
        #endif
        if (texAll[index2] == null)
        {
            picNum=RepairZero(index2.ToString (),3);

            texAll[index2] = (Texture2D)Resources.Load(path + "/" + fileName + picNum, typeof(Texture2D));//给图片序列加载当前图片
            suliang++;//加载图片计数增加1
        }
        if (suliang > buff)//如果图片计数大于设定值
        {
            for (var i = 0; i < texAll.Length; i++)
            {
                if (i != index2)
                {
                    texAll[i] = null;
                }
            }
            Resources.UnloadUnusedAssets();//清理缓存
            suliang = 1;//重新计数图片数量
        }
        renderer.material.mainTexture = texAll[index2];//赋值图片序列给物体中的材质
    }

    /// <summary>
    /// 指定字符串的固定长度,如果字符串小于固定长度,
    /// 则在字符串的前面补足零,可设置的固定长度最大为9位
    /// </summary>
    /// <param name="text">原始字符串</param>
    /// <param name="limitedLength">字符串的固定长度</param>
    public static string RepairZero(string text, int limitedLength)
    {
        //补足0的字符串
        string temp = "";
        
        //补足0
        for (int i = 0; i < limitedLength - text.Length; i++)
        {
            temp += "0";
        }
        
        //连接text
        temp += text;
        
        //返回补足0的字符串
        return temp;
    }

}
原文地址:https://www.cnblogs.com/bkycjj/p/3914931.html