Unity 場景加載 DontDestroyOnLoad

參考
https://www.bilibili.com/video/BV1Ut4y1m7Zv?p=19
Unity異步加載場景SceneManager.LoadSceneAsync與AsyncOperation的使用

一、同步加載 SceneManager.LoadScene

https://docs.unity3d.com/cn/2019.4/ScriptReference/SceneManagement.SceneManager.LoadScene.html

注意:在大多數(shù)情況下,為了避免在加載時出現(xiàn)暫停或性能中斷現(xiàn)象, 您應(yīng)該使用此命令的異步版,即: LoadSceneAsync。

public static void LoadScene (int sceneBuildIndex, 
SceneManagement.LoadSceneMode mode= LoadSceneMode.Single);

public static void LoadScene (string sceneName, 
SceneManagement.LoadSceneMode mode= LoadSceneMode.Single);
1.名稱或路徑或索引
  • sceneName 要加載的場景的名稱或路徑。
  • sceneBuildIndex Build Settings 中要加載場景的索引。

提供的 sceneName 可以只是場景名稱(不包含 .unity 擴展名),也可以是 BuildSettings 窗口中顯示的路徑(仍然不包含 .unity 擴展名)。如果僅提供場景名稱,此方法將加載場景列表中匹配的第一個場景。如果有多個名稱相同但路徑不同的場景,應(yīng)該使用完整路徑。

2.LoadSceneMode

https://docs.unity3d.com/cn/2019.4/ScriptReference/SceneManagement.LoadSceneMode.html

  • Single 關(guān)閉所有當前加載的場景 并加載一個場景。
  • Additive 將場景添加到當前加載的場景。

參考04:Unity 5.3多場景編輯功能簡介

用戶可以通過LoadSceneMode來指定不同的加載模式。LoadSceneMode.Single在加載之前會卸載其他所有的場景,LoadSceneMode.Additive則是加載的時候不關(guān)閉之前的場景。
還有一點很重要的是LoadScene()并不是完全同步的,它只能保證在下一幀開始之前加載完畢。所以在此推薦大家使用LoadSceneAsync()這個異步的加載方法。

使用 SceneManager.LoadScene 時,不會立即加載場景,而是在下一幀加載。這種半異步的行為可能會導(dǎo)致幀卡頓,并可能令人困惑,因為加載無法立即完成。

參考Unity SceneManager.LoadScene之后不能馬上SetActiveScene

        SceneManager.LoadScene("Scene2", LoadSceneMode.Additive);
        var scene = SceneManager.GetSceneByName("Scene2");
        Debug.Log(scene.name);
        //SceneManager.SetActiveScene(scene);
        SceneManager.sceneLoaded += (Scene sc, LoadSceneMode loadSceneMode) =>
        {
            SceneManager.SetActiveScene(scene);
        };

二、異步加載 SceneManager.LoadSceneAsync

https://docs.unity3d.com/cn/2019.4/ScriptReference/SceneManagement.SceneManager.LoadSceneAsync.html

public static AsyncOperation LoadSceneAsync (string sceneName, 
SceneManagement.LoadSceneMode mode= LoadSceneMode.Single);

public static AsyncOperation LoadSceneAsync (int sceneBuildIndex, 
SceneManagement.LoadSceneMode mode= LoadSceneMode.Single);

public static AsyncOperation LoadSceneAsync (string sceneName, 
SceneManagement.LoadSceneParameters parameters);

public static AsyncOperation LoadSceneAsync (int sceneBuildIndex, 
SceneManagement.LoadSceneParameters parameters);

parameters 用于將各種參數(shù)(除了名稱和索引)收集到單個位置的結(jié)構(gòu)。

1.返回值A(chǔ)syncOperation

您可以 yield 直到異步操作繼續(xù),或手動檢查它已完成 (isDone) 還是正在進行 (progress)。

  • allowSceneActivation 允許在場景準備就緒后立即激活場景。當我們不允許時,即使加載完成,也不會跳轉(zhuǎn)。
  • isDone 操作是否已完成?(只讀)。實際上用到比較少,較多的還是用進度。因為這個屬性是要加載完畢并且開啟跳轉(zhuǎn)后,跳轉(zhuǎn)成功才會變成完成
  • priority Priority 允許您調(diào)整執(zhí)行異步操作調(diào)用的順序。
  • progress 獲取操作進度(只讀)。實際上到0.9就已經(jīng)加載完畢。
2.官方加載示例
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Example : MonoBehaviour
{
    void Update()
    {
        // Press the space key to start coroutine
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Use a coroutine to load the Scene in the background
            StartCoroutine(LoadYourAsyncScene());
        }
    }

    IEnumerator LoadYourAsyncScene()
    {
        // The Application loads the Scene in the background as the current Scene runs.
        // This is particularly good for creating loading screens.
        // You could also load the Scene by using sceneBuildIndex. In this case Scene2 has
        // a sceneBuildIndex of 1 as shown in Build Settings.

        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync("Scene2");

        // Wait until the asynchronous scene fully loads
        while (!asyncLoad.isDone)
        {
            yield return null;
        }
    }
}
三、進度條實例

參考Unity進度條 異步加載SceneManager.LoadSceneAsync
新建LoadingScene,添加一個最大值為100的Slider,和一個Text

image.png

然后將腳本掛在GameObject上即可:

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoadingSceneScript : MonoBehaviour
{
    public Slider slider;
    public Text text;//百分制顯示進度加載情況

    void Start()
    {
        StartCoroutine(loginMy());
    }

    IEnumerator loginMy()
    {
        int displayProgress = 0;
        int toProgress = 0;
        AsyncOperation op = SceneManager.LoadSceneAsync("Scenes/modelScene");
        op.allowSceneActivation = false;
        while (op.progress < 0.9f) //此處如果是 <= 0.9f 則會出現(xiàn)死循環(huán)所以必須小0.9
        {
            toProgress = (int)op.progress * 100;
            while (displayProgress < toProgress)
            {
                ++displayProgress;
                SetLoadingPercentage(displayProgress);
                yield return new WaitForEndOfFrame();//ui渲染完成之后
            }
        }
        toProgress = 100;
        while (displayProgress < toProgress)
        {
            ++displayProgress;
            SetLoadingPercentage(displayProgress);
            yield return new WaitForEndOfFrame();
        }
        op.allowSceneActivation = true;

    }

    private void SetLoadingPercentage(int displayProgress)
    {
        slider.value = displayProgress;
        text.text = displayProgress.ToString() + "%";
    }
}

如果報錯,檢查一下要加載的場景是否添加到BuildSettings:


image.png
四、DontDestroyOnLoad

參考
https://docs.unity3d.com/cn/2019.4/ScriptReference/Object.DontDestroyOnLoad.html
DontDestroyOnLoad的使用

加載新的 Scene 會銷毀所有現(xiàn)有的 Scene 對象。調(diào)用 Object.DontDestroyOnLoad 可以在關(guān)卡加載期間保留 Object。如果目標 Object 是組件或 GameObject,Unity 還會保留 Transform 的所有子項。 Object.DontDestroyOnLoad 不會返回值。

public class ExampleClass :MonoBehaviour{
   void Awake() {
      DontDestroyOnLoad(transform.gameObject);
   }
}

由于使用DontDestroyOnLoad的物體不會被釋放掉,假設(shè)我們寫了上面的代碼,而物體所在的游戲場景又可以重復(fù)進入的時候,游戲物體就會在每次進入場景的時候創(chuàng)建一次,甚至可以無限創(chuàng)建下去,這樣的處理明顯不妥。

1.方案1

在每次調(diào)用DontDestroyOnLoad的時候,都去判斷場景中是否有對應(yīng)的物體,如果沒有再去創(chuàng)建

void Awake() {
if(Gameobject.Find("GlobalController"))
 DontDestroyOnLoad(transform.gameObject);
}
2.方案2

把DontDestroyOnLoad的調(diào)用寫到最初始的場景,并且保證相應(yīng)的場景中不存在再次進入的可能性

3.方案3

把使用DontDestroyOnLoad的腳本進行靜態(tài)初始化,在靜態(tài)初始化的時候進行DontDestroyOnLoad操作

public class Global:MonoBehaviour
{
public static Globalinstance;
static Global()
{
GameObjectgo=newGameObject("Globa");
DontDestroyOnLoad(go);
instance=go.AddComponent();
}

public voidDoSomeThings()
{
Debug.Log("DoSomeThings");
}

voidStart()
{
Debug.Log("Start");
}
}
4.官方示例

該示例中的 scene1 開始播放來自 AudioSource 的背景音樂。在 scene2 加載時,音樂繼續(xù)播放。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Object.DontDestroyOnLoad example.
//
// This script example manages the playing audio. The GameObject with the
// "music" tag is the BackgroundMusic GameObject. The AudioSource has the
// audio attached to the AudioClip.

public class DontDestroy : MonoBehaviour
{
    void Awake()
    {
        GameObject[] objs = GameObject.FindGameObjectsWithTag("music");

        if (objs.Length > 1)
        {
            Destroy(this.gameObject);
        }

        DontDestroyOnLoad(this.gameObject);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容