0完美对称0的专栏

异步加载的方法很多,这里我介绍一下我前段时间学到的一个异步加载方法,感觉挺好用,有兴趣的可以看看,下面就直接贴代码:

/// <summary>/// Async to load./// Created By Hong Youwei/// 2015.3.25/// </summary>using UnityEngine;using System.Collections;public class AsyncToLoad : MonoBehaviour {public GameObject background;// 获取游戏背景public GameObject loadingBar;// 进度条对象UISlider slider;// 进度条脚本UILabel lable_Percentage;// 显示进度的百分比的标签// Use this for initializationvoid Start () {UISprite sprite = background.GetComponent<UISprite> ();sprite.spriteName = GameInfo.loadSceneName;LoadScene();}// 调用加载界面的加载协程public void LoadScene() {slider = loadingBar.GetComponent<UISlider>();lable_Percentage = loadingBar.GetComponentInChildren<UILabel>();StartCoroutine(StartLoading(GameInfo.loadSceneName));//StartLoading后面括号填的是要加载的界面的名字,我这里填的是我自己的界面名字加载,使用的话,记得设置为你自己的界面名字比如StartCoroutine(StartLoading("Level1"));}// 异步加载协同程序IEnumerator StartLoading(string name) {int displayProgress = 0;// 进度条显示值int toProgress = 0;// 进度条要达到的值AsyncOperation aop = Application.LoadLevelAsync(name);aop.allowSceneActivation = false;// 设置不允许加载完成就进入界面// 判断是否进度小于0.9,如果是的话循环输出进度条的值while (aop.progress < 0.9f){toProgress = (int)(aop.progress * 100);while (displayProgress < toProgress){++displayProgress;SetLoadingPercentage(displayProgress);}yield return new WaitForEndOfFrame();// 每次循环结束执行完当前帧才继续下一个循环}// Unity就只会加载场景到90%,剩下的10%要等到allowSceneActivation设置为true后才加载,所以设置增加进度条效果,让进度条平滑显示达到100%toProgress = 100;while (displayProgress < toProgress){++displayProgress;SetLoadingPercentage(displayProgress);yield return new WaitForEndOfFrame();}yield return new WaitForSeconds(0.2f);//延缓进入游戏时间,如果场景较小加载很快,为了看到进度效果,做了个小小延迟,不需要的可以去掉aop.allowSceneActivation = true;// 设置加载完成可以进入界面}// 设置加载进度条信息void SetLoadingPercentage(int _value){slider.value = _value / 100.0f;lable_Percentage.text = _value + "%";}}以上有一部分是直接调用了NGUI插件的UISprite做背景,调用UILable做一个百分比数字显示,调用UISlider作为一个进度条

,将会错过更好的风景,保持一份平和,保持一份清醒。

0完美对称0的专栏

相关文章:

你感兴趣的文章:

标签云: