国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 編程設計 > 正文

Unity3D 使用 WWW 加載場景并顯示進度條

2020-07-14 13:22:51
字體:
供稿:網(wǎng)友

Unity3D 加載場景有很多種方式,做一些小的 DEMO 的時候往往是直接使用 Application.LoadLevel 或者 Application.LoadLevelAsync 函數(shù)加載場景,具體可查看(http://www.xuanyusong.com/archives/1427),但是這種辦法不適合在真正的 Unity3D 開發(fā)中,因為前一種需要把所有的場景都打包,這在某些情況下是不現(xiàn)實的,比如開發(fā)頁游,我們不可能把所有的場景都打包讓用戶下載,我們需要一個場景一個場景的加載,這時候我們可以使用 WWW 先通過 HTTP 加載場景到本地緩存,然后再使用 Application.LoadLevel 或者 Application.LoadLevelAsync 函數(shù)加載場景,使用這種加載方式,不僅不需要 Build Settings -> Add Current 處理加載場景,進度條的顯示也更加容易,但是使用這種方式,需要先把場景打包成 unity3d(查看詳情) 或者 assetbundle(查看詳情) 文件。

先把測試場景搭建好,如圖:

然后添加一個 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)
{
// 更新進度
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 = "場景加載出錯!";
}
if (this.www.isDone)
{
this.lblStatus.text = "場景正在初始化,請等待。。。";
Application.LoadLevelAsync("MainScene");
}
}
}

然后把這個腳本掛載到游戲場景的一個對象中,設置好相關屬性,如圖:

運行我們的游戲,可以查看進度條的加載情況,當加載完成,自動跳轉(zhuǎn)到下一個場景中,如圖:

因為前面我封裝了一個 WWW 加載管理器(查看詳情),我們可以直接拿來使用,我們建立一個新的 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)
{
// 更新進度
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 = "場景加載出錯!";
}
if (this.www.isDone)
{
this.lblStatus.text = "場景正在初始化,請等待。。。";
Application.LoadLevelAsync("MainScene");
}
}
}

然后我們把原先的腳本從場景移除,掛載這個新的腳本,如圖:

運行游戲,可以看到與上面同樣的加載效果。

百度網(wǎng)盤下載地址:http://pan.baidu.com/s/1hq7pgd2 密碼: lc4y

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 开封县| 陈巴尔虎旗| 商洛市| 阳信县| 丽水市| 吐鲁番市| 吴忠市| 黎川县| 循化| 阳高县| 临颍县| 特克斯县| 隆德县| 鹤庆县| 施甸县| 贡嘎县| 大名县| 吉木萨尔县| 青龙| 逊克县| 安吉县| 高阳县| 通渭县| 汕尾市| 泰安市| 阿城市| 通化县| 扎囊县| 车险| 山东省| 西宁市| 育儿| 甘肃省| 瑞丽市| 色达县| 晋宁县| 神农架林区| 长沙市| 夏河县| 光泽县| 观塘区|