Unity3D 使用 WWW 加載場景并顯示進(jìn)度條

Unity3D 加載場景有很多種方式,做一些小的 DEMO 的時(shí)候往往是直接使用 Application.LoadLevel 或者 Application.LoadLevelAsync 函數(shù)加載場景,具體可查看(http://www.xuanyusong.com/archives/1427),但是這種辦法不適合在真正的 Unity3D 開發(fā)中,因?yàn)榍耙环N需要把所有的場景都打包,這在某些情況下是不現(xiàn)實(shí)的,比如開發(fā)頁游,我們不可能把所有的場景都打包讓用戶下載,我們需要一個(gè)場景一個(gè)場景的加載,這時(shí)候我們可以使用 WWW 先通過 HTTP 加載場景到本地緩存,然后再使用 Application.LoadLevel 或者 Application.LoadLevelAsync 函數(shù)加載場景,使用這種加載方式,不僅不需要 Build Settings -> Add Current 處理加載場景,進(jìn)度條的顯示也更加容易,但是使用這種方式,需要先把場景打包成 unity3d(查看詳情) 或者 assetbundle(查看詳情) 文件。
先把測試場景搭建好,如圖:
然后添加一個(gè) C# 腳本,取名 UseWww.cs,全部代碼如下:
using UnityEngine;
using System.Collections;
public class UseWww : MonoBehaviour
{
public UISlider progressBar;
public UILabel lblStatus;
private WWW www;
private string scenePath;
void Awake()
{
this.scenePath = "file:///" + Application.dataPath + "/Assets/MainScene.unity3d";
// 開始加載場景
this.StartCoroutine (this.BeginLoader ());
}
void Update()
{
if (this.www != null && this.progressBar != null && !this.www.isDone)
{
// 更新進(jìn)度
this.progressBar.value = this.www.progress;
}
}
private IEnumerator BeginLoader()
{
this.lblStatus.text = "場景加載中,請稍候。。。";
// 加載場景使用 WWW.LoadFromCacheOrDownload,函數(shù),這樣加載完成才能使用 Application.LoadLevel 或者 Application.LoadLevelAsync
this.www = WWW.LoadFromCacheOrDownload (scenePath, Random.Range(0, 100));
yield return this.www;
if(!string.IsNullOrEmpty(this.www.error))
{
this.lblStatus.text = "場景加載出錯(cuò)!";
}
if (this.www.isDone)
{
this.lblStatus.text = "場景正在初始化,請等待。。。";
Application.LoadLevelAsync("MainScene");
}
}
}
然后把這個(gè)腳本掛載到游戲場景的一個(gè)對象中,設(shè)置好相關(guān)屬性,如圖:
運(yùn)行我們的游戲,可以查看進(jìn)度條的加載情況,當(dāng)加載完成,自動(dòng)跳轉(zhuǎn)到下一個(gè)場景中,如圖:
因?yàn)榍懊嫖曳庋b了一個(gè) WWW 加載管理器(查看詳情),我們可以直接拿來使用,我們建立一個(gè)新的 C# 腳本,取名 UseWwwLoaderManager.cs,全部代碼如下:
using UnityEngine;
using System.Collections;
public class UseWww : MonoBehaviour
{
public UISlider progressBar;
public UILabel lblStatus;
private WWW www;
private string scenePath;
void Awake()
{
this.scenePath = "file:///" + Application.dataPath + "/Assets/MainScene.unity3d";
// 開始加載場景
this.StartCoroutine (this.BeginLoader ());
}
void Update()
{
if (this.www != null && this.progressBar != null && !this.www.isDone)
{
// 更新進(jìn)度
this.progressBar.value = this.www.progress;
}
}
private IEnumerator BeginLoader()
{
this.lblStatus.text = "場景加載中,請稍候。。。";
// 加載場景使用 WWW.LoadFromCacheOrDownload,函數(shù),這樣加載完成才能使用 Application.LoadLevel 或者 Application.LoadLevelAsync
this.www = WWW.LoadFromCacheOrDownload (scenePath, Random.Range(0, 100));
yield return this.www;
if(!string.IsNullOrEmpty(this.www.error))
{
this.lblStatus.text = "場景加載出錯(cuò)!";
}
if (this.www.isDone)
{
this.lblStatus.text = "場景正在初始化,請等待。。。";
Application.LoadLevelAsync("MainScene");
}
}
}
然后我們把原先的腳本從場景移除,掛載這個(gè)新的腳本,如圖:
運(yùn)行游戲,可以看到與上面同樣的加載效果。
百度網(wǎng)盤下載地址:http://pan.baidu.com/s/1hq7pgd2 密碼: lc4y
相關(guān)文章
Unity3D 場景導(dǎo)出成 XML 并解析還原場景
這篇文章主要介紹了Unity3D 場景導(dǎo)出成 XML 并解析還原場景,中間部分代碼取自互聯(lián)網(wǎng),并進(jìn)行了修改,有需要的朋友可以參考下2014-10-20Unity3D游戲開發(fā) 宣雨松著 PDF掃描版[27MB]
《Unity3D游戲開發(fā)》通過實(shí)例詳細(xì)介紹了如何使用Unity 進(jìn)行游戲開發(fā),書中先簡要介紹了Unity 環(huán)境搭建、編輯器和GUI 游戲界面相關(guān)的知識(shí),接著介紹了如何使用C# 和JavaScri2014-05-10Unity3D中自動(dòng)調(diào)用的方法總結(jié)
這篇文章主要介紹了Unity3D中自動(dòng)調(diào)用的方法總結(jié),需要的朋友可以參考下2014-04-24- 在本場景用到的拖尾效果可以查看我的另一篇文章,里面有詳細(xì)的介紹,刀光效果來自 Unity3D Assets 商店,只是把原作者的例子代碼整理了一下,變得非常簡單實(shí)用的類。2014-10-20
Unity3D 實(shí)現(xiàn)怪物巡邏、按路線行走操作
這篇文章主要介紹了Unity3D 實(shí)現(xiàn)怪物巡邏、按路線行走操作,由于之前沒什么經(jīng)驗(yàn),就只能按照自己的想法很愚笨的實(shí)現(xiàn)的,也算拋磚引玉,如果讀者知道如何更簡單的實(shí)現(xiàn)方式,2014-10-20