Unity3D 动态加载 图片序列正反播放
生活随笔
收集整理的這篇文章主要介紹了
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.pngpublic 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_IPHONEforeach (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;} } }}#elseif (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就賦值為0index = 0;}else if (index >= texAll.Length){index2 = texAll.Length - 1; //大于最大值就賦值為最大值index = texAll.Length - 1;}else{index2 = index; //其余的相等 }}}#endifif (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 = "";//補足0for (int i = 0; i < limitedLength - text.Length; i++){temp += "0";}//連接texttemp += text;//返回補足0的字符串return temp;}}?
轉載于:https://www.cnblogs.com/bkycjj/p/3914931.html
總結
以上是生活随笔為你收集整理的Unity3D 动态加载 图片序列正反播放的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HDU4911 Inversion 解题
- 下一篇: 在Hadoop集群上,搭建HBase集群