异步加载场景及Loading进度条制作

实现功能:点击开始游戏以后UI界面进入Loading界面,Loading结束以后自动进入游戏场景。

在这之前先在Build Settings中Add要使用的场景

在场景A中添加StartGame方法:

Application.LoadLevel(1);//同步加载Loading界面(因为Loading界面资源较少速度快所以此处用同步方法)

在Loading场景中加入进度条图片:分为上下两层,上层负责显示进度

 

将上层的进度条Image组件中的Image Ttpe改为Filled

 

接下来再Loading场景Cam中添加以下代码(通过改变Fill Amount显示Loading效果):

public class Loading : MonoBehaviour {
AsyncOperation asyncOperation; //声明一个异步变量
public GameObject progress; //声明进度条对象
public GameObject Text; //声明进度条上的显示文本

//对以上变量进行初始化

void Start()
{ progress.SetActive(true);
progress = GameObject.FindWithTag("Fader");
Text = GameObject.FindWithTag("Text");
Text.GetComponent<Text>().text = "0";
progress.GetComponent<Image>().fillAmount = 0f;
DontDestroyOnLoad(gameObject);
StartCoroutine(loadScene()); //开启异步任务,进入loadScene方法
}
void Update()
{
Text.GetComponent<Text>().text = (float)asyncOperation.progress*100+10+"%"; //文本更新异步进度
progress.GetComponent<Image>().fillAmount = (float)asyncOperation.progress+.1f;//进度条更新异步进度
}
IEnumerator loadScene()
{
yield return asyncOperation = Application.LoadLevelAsync(2);//读取完毕自动进入下一个场景
}

}

posted @ 2015-08-25 10:24  一傻小冲  阅读(716)  评论(0编辑  收藏  举报