using UnityEngine;
using System.Collections;
using System.Resources;
public class movepic:MonoBehaviour{
public Texture2D[] texAll; //图片序列储存的图片组,注意需要定义这个组的size大小为图片序列的数量
public GameObject plane; //指定为图片序列需要在哪个gameobject物体播放
public string path; //图片序列存放的路径,注意只能存放在Assets的Resources文件夹中,如果没有就自己建立一个。如果是放在Resources文件夹中的img文件夹中,这个填写为img.
// 如果是放在Resources文件夹中的img文件夹中的1文件夹中,这个填写为img/1.
public string fileName ; //图片序列文件的文件名称, 比如图片序列为 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;
void Start()
{
texAll[0]=(Texture2D)Resources.Load(path+"/"+fileName+"_0", typeof(Texture2D)); //首先加载第一张图片显示
}
void Update()
{
//ios系统版本
#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
//pc版本
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)
{
texAll[index2]=(Texture2D)Resources.Load(path+"/"+fileName+"_"+index2, typeof(Texture2D)); //给图片序列加载当前图片
suliang++; //加载图片计数增加1
}
if (suliang>buff) //如果图片计数大于设定值
{
for(var i=0;i
{
if (i!=index2)
{
texAll[i]=null;
}
}
Resources.UnloadUnusedAssets(); //清理缓存
suliang=1; //重新计数图片数量
}
plane.renderer.material.mainTexture = texAll[index2]; //赋值图片序列给物体中的材质
}
}
movepic
最新推荐文章于 2024-06-22 20:17:37 发布