unity3d_实现幻灯片效果2

上一篇使用的是静态方式进行的加载,采用的数据结构为 数组

该篇文章则是使用动态加载的方式实现:

this.objsOfRouses = Resources.LoadAll("images",typeof(Texture));

该方法会安排图片在文件在顺序进行加载。

如图:


加载顺序如下:


using UnityEngine;

using System.Collections;


public class imageAnimation : MonoBehaviour {
//
private Object[] objsOfRouses;

private Texture2D[] texturesLoaded;

private Material materialOfPanel;

private int frameCounter =0;

public float delayTime =0.5f;

private float currentTime=0.0f;
private float endTime =0.0f;

public GameObject dongWu;
void Awake()
{
//get the material of this panel
this.materialOfPanel = this.renderer.material;


}

// Use this for initialization
void Start () 
{
//set the starting time
currentTime =Time.time;//0.0s
//get images form the Resourse floder
//the return value is object
this.objsOfRouses = Resources.LoadAll("images",typeof(Texture));
int theLength =objsOfRouses.Length;

//put the images into material arrary 
texturesLoaded =new Texture2D[theLength];
for (int i=0; i<theLength; i++)
{
this.texturesLoaded[i] =(Texture2D)this.objsOfRouses[i];
Debug.Log("the image'name is -->"+this.texturesLoaded[i].name);
}
}
 
// Update is called once per frame
void Update () {
 
changePic();
 

}

//
void changePic(){
endTime = Time.time;
float timeOffset = endTime - currentTime;
if (timeOffset>0.3){
if (this.frameCounter<this.objsOfRouses.Length){
this.materialOfPanel.mainTexture = texturesLoaded[frameCounter];
++frameCounter;
}else{
frameCounter=0;
this.materialOfPanel.mainTexture = texturesLoaded[frameCounter];
}
//when changge one image,get the new time
currentTime =Time.time;
}

 
}


}


最后效果如下:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值