Unity loadSceneAsync


 

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.SceneManagement;
using TMPro;
using UnityEngine;

public class LoadScene : MonoBehaviour
{
    public TextMeshProUGUI lbl;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(CorouLoadScene("LoadScene2"));
    }

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

    private IEnumerator CorouLoadScene(string sceneName)
    {
        int displayProgress = 0;
        int toProgress = 0;
        AsyncOperation op = SceneManager.LoadSceneAsync(sceneName);
        op.allowSceneActivation = false;

        while(op.progress < 0.9f){
            toProgress = (int)op.progress * 100;
            while(displayProgress < toProgress)
            {
                ++displayProgress;

                setLoadingPercentage(displayProgress);

                yield return new WaitForEndOfFrame();
            }
        }

        toProgress = 100;

        while(displayProgress < toProgress)
        {
            ++displayProgress;
            setLoadingPercentage(displayProgress);
            yield return new WaitForEndOfFrame();

        }
        
        op.allowSceneActivation = true;
    }

    private void setLoadingPercentage(int progress)
    {
        lbl.text = progress.ToString();
    }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM