Unity實(shí)現(xiàn)場景加載功能
unity場景加載分為同步加載和異步加載,供大家參考,具體內(nèi)容如下
同步加載 loadScene
首先將前置工作做好。
創(chuàng)建一個(gè)項(xiàng)目工程,然后創(chuàng)建三個(gè)場景 loading00、loading01、loading02。每個(gè)場景分別創(chuàng)建一個(gè)cube、Sphere、Capsule 。然后打開File -> Build Settings, 然后將創(chuàng)建的 loading00、loading01、loading02拖拽到Scenes In Build中。如下圖:
然后再創(chuàng)建一個(gè) loading.cs 腳本,然后寫上這段代碼:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class loading : MonoBehaviour { // Start is called before the first frame update void Start() { // 同步加載場景 SceneManager.LoadScene(1, LoadSceneMode.Additive); } // Update is called once per frame void Update() { } }
SceneManager.LoadScene(1, LoadSceneMode.Additive);
第一個(gè)參數(shù)是表示加載的場景序號,第二個(gè)參數(shù)是 加載場景后,是否刪除舊場景。
場景序號就是在 BuildSettings 中的序號如下圖:
當(dāng)然,第一個(gè)參數(shù)也可以寫場景的路徑:
SceneManager.LoadScene(“Scenes/loading01”, LoadSceneMode.Additive);
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class loading : MonoBehaviour { // Start is called before the first frame update void Start() { // 同步加載場景 SceneManager.LoadScene("Scenes/loading01", LoadSceneMode.Additive); } // Update is called once per frame void Update() { } }
與上述代碼想過是一致的。
異步加載
異步加載需要先介 AsyncOperation 類
SceneManager.LoadSceneAsync(1, LoadSceneMode.Additive) 這個(gè)是異步加載的函數(shù),其參數(shù)的用法跟同步加載的用法一致,然后,返回值 是一個(gè) As yncOperation 類。
AsyncOperation 的用處:
- AsyncOperation.isDone 這個(gè)是用來判斷加載是否完成。這個(gè)屬性是加載完并且跳轉(zhuǎn)成功后才會(huì)變成完成
- AsyncOperation.allowSceneActivation 這個(gè)是加載完后,是否允許跳轉(zhuǎn),當(dāng)為false時(shí),即使場景加載完了,也不會(huì)跳轉(zhuǎn)
- AsyncOperation.progress 這個(gè)時(shí)表示加載場景的進(jìn)度。實(shí)際上的值時(shí) 0 - 0.9, 當(dāng)值為0.9的時(shí)候,場景就已經(jīng)加載完成了。
我們要異步加載場景的話,一般都是需要用協(xié)程一起工作
代碼如下:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class loading : MonoBehaviour { // Start is called before the first frame update void Start() { StartCoroutine(Load()); } IEnumerator Load() { AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(1, LoadSceneMode.Additive); asyncOperation.allowSceneActivation = false; // 這里限制了跳轉(zhuǎn) // 這里就是循環(huán)輸入進(jìn)度 while(asyncOperation.progress < 0.9f) { Debug.Log(" progress = " + asyncOperation.progress); } asyncOperation.allowSceneActivation = true; // 這里打開限制 yield return null; if(asyncOperation.isDone) { Debug.Log("完成加載"); } } // Update is called once per frame void Update() { } }
異步和同步的區(qū)別
異步不會(huì)阻塞線程,可以在加載過程中繼續(xù)去執(zhí)行其他的一些代碼,(比如同時(shí)加載進(jìn)度)而同步會(huì)阻塞線程,只有加載完畢顯示后才能繼續(xù)執(zhí)行之后的一些代碼。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法
這篇文章主要介紹了C#監(jiān)控文件夾并自動(dòng)給圖片文件打水印的方法,涉及C#針對文件夾及圖片操作的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05VS2019下安裝和破解?DevExpress?19.2?插件的詳細(xì)教程
這篇文章主要介紹了VS2019?安裝并破解?DevExpress?19.2?插件的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03C# Winform使用擴(kuò)展方法實(shí)現(xiàn)自定義富文本框(RichTextBox)字體顏色
這篇文章主要介紹了C# Winform使用擴(kuò)展方法實(shí)現(xiàn)自定義富文本框(RichTextBox)字體顏色,通過.NET的靜態(tài)擴(kuò)展方法來改變RichTextBox字體顏色,需要的朋友可以參考下2015-06-06C#類型轉(zhuǎn)換之自定義隱式轉(zhuǎn)換和顯式轉(zhuǎn)換
本文主要為大家介紹了一個(gè)新的類型轉(zhuǎn)換方法:通過自定義隱式轉(zhuǎn)換,把不一樣的數(shù)據(jù)類型反序列化為一樣的數(shù)據(jù)類型,需要的同學(xué)可以參考一下2022-03-03