LoadScene場景異步加載


LoadScene場景異步加載

using UnityEngine;

using System.Collections;

using UnityEngine.SceneManagement;

using UnityEngine.UI;

public class LoadScene: MonoBehaviour

{

    public Slider Slider;//定義滑動條

    private AsyncOperation async;

    private int number = 0;

    private int num = 0;

    private void Start()

    {

         //賦初始值

        Slider.value = 0;

         //開啟協程

        StartCoroutine("start");

    }

    void Update()

    {

         //滑動條的移動

        Slider.value = number / 100f;

    }

         //協程

    private IEnumerator start()

    {

         //要加載的大場景Scene

        async = SceneManager.LoadSceneAsync("Scene");

         //初始加載場景不激活

        async.allowSceneActivation = false;

         //增加滑動平滑性

        while (async.progress < 0.9f)

        {

               //加載進度

            num = (int)async.progress * 100;

            while (number < num)

            {

                ++number;

                //等待時長

                yield return new WaitForSeconds(0);

            }

        }       

        num = 100;

         //增加平滑效果

        while (number < num)

        {

            ++number;

             //等待時長

            yield return new WaitForSeconds(0);

        }

         //加載場景激活

        async.allowSceneActivation = true;

    }

}

注:加載的場景Scene需要要將場景添加到構建設置,請使用菜單File-> Build Settings ...之后才可使用.......


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM