最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Unity實(shí)現(xiàn)場(chǎng)景加載功能

 更新時(shí)間:2021年10月12日 16:44:10   作者:SOLA&AIR  
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)場(chǎng)景加載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

unity場(chǎng)景加載分為同步加載和異步加載,供大家參考,具體內(nèi)容如下

同步加載 loadScene

首先將前置工作做好。
創(chuàng)建一個(gè)項(xiàng)目工程,然后創(chuàng)建三個(gè)場(chǎng)景 loading00、loading01、loading02。每個(gè)場(chǎng)景分別創(chuàng)建一個(gè)cube、Sphere、Capsule 。然后打開(kāi)File -> Build Settings, 然后將創(chuàng)建的 loading00、loading01、loading02拖拽到Scenes In Build中。如下圖:

然后再創(chuàng)建一個(gè) loading.cs 腳本,然后寫(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()
    {
     // 同步加載場(chǎng)景
        SceneManager.LoadScene(1, LoadSceneMode.Additive);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

SceneManager.LoadScene(1, LoadSceneMode.Additive);
第一個(gè)參數(shù)是表示加載的場(chǎng)景序號(hào),第二個(gè)參數(shù)是 加載場(chǎng)景后,是否刪除舊場(chǎng)景。

場(chǎng)景序號(hào)就是在 BuildSettings 中的序號(hào)如下圖:

當(dāng)然,第一個(gè)參數(shù)也可以寫(xiě)場(chǎng)景的路徑:
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()
    {
     // 同步加載場(chǎng)景
        SceneManager.LoadScene("Scenes/loading01", LoadSceneMode.Additive);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

與上述代碼想過(guò)是一致的。

異步加載

異步加載需要先介 AsyncOperation 類(lèi)

SceneManager.LoadSceneAsync(1, LoadSceneMode.Additive) 這個(gè)是異步加載的函數(shù),其參數(shù)的用法跟同步加載的用法一致,然后,返回值 是一個(gè) As yncOperation 類(lèi)。

AsyncOperation 的用處:

  • AsyncOperation.isDone 這個(gè)是用來(lái)判斷加載是否完成。這個(gè)屬性是加載完并且跳轉(zhuǎn)成功后才會(huì)變成完成
  • AsyncOperation.allowSceneActivation 這個(gè)是加載完后,是否允許跳轉(zhuǎn),當(dāng)為false時(shí),即使場(chǎng)景加載完了,也不會(huì)跳轉(zhuǎn)
  • AsyncOperation.progress 這個(gè)時(shí)表示加載場(chǎng)景的進(jìn)度。實(shí)際上的值時(shí) 0 - 0.9, 當(dāng)值為0.9的時(shí)候,場(chǎng)景就已經(jīng)加載完成了。

我們要異步加載場(chǎ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;    // 這里打開(kāi)限制
        yield return null;

        if(asyncOperation.isDone)
        {
            Debug.Log("完成加載");
        }
    }

    // Update is called once per frame
    void Update()
    {
      
    }
}

異步和同步的區(qū)別

異步不會(huì)阻塞線程,可以在加載過(guò)程中繼續(xù)去執(zhí)行其他的一些代碼,(比如同時(shí)加載進(jìn)度)而同步會(huì)阻塞線程,只有加載完畢顯示后才能繼續(xù)執(zhí)行之后的一些代碼。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:

相關(guān)文章

最新評(píng)論

汕尾市| 罗江县| 高要市| 都昌县| 沧源| 舒城县| 张掖市| 济宁市| 永寿县| 时尚| 邵武市| 宣威市| 突泉县| 郸城县| 彭山县| 荔波县| 习水县| 景宁| 肃宁县| 东宁县| 文化| 璧山县| 宜州市| 龙川县| 扎兰屯市| 东兴市| 措美县| 马边| 永清县| 永寿县| 铁力市| 左云县| 隆化县| 塔城市| 武功县| 嘉鱼县| 澄城县| 怀来县| 左贡县| 会昌县| 和政县|