movepic

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]; //赋值图片序列给物体中的材质

}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值