Unity 异步加载场景,进度条显示

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

public class Globe
{
    public static string nextSceneName;
}

public class AsyncLoadScene : MonoBehaviour
{
    public Text loadingText;
    public Image progressBar;

    private int curProgressValue = 0;

    private AsyncOperation operation;

    // Use this for initialization
    void Start()
    {
      //加载的场景名称 Globe.nextSceneName
= "Main_Day"; if (SceneManager.GetActiveScene().name == "Main") { //启动协程 StartCoroutine(AsyncLoading()); } } IEnumerator AsyncLoading() { operation = SceneManager.LoadSceneAsync(Globe.nextSceneName); Debug.Log(operation.progress); //阻止当加载完成自动切换 operation.allowSceneActivation = false; yield return operation; } void Update() { curProgressValue = (int)(operation.progress * 100); loadingText.text = curProgressValue + "%";//实时更新进度百分比的文本显示 progressBar.fillAmount = curProgressValue / 100f;//实时更新滑动进度图片的fillAmount值 //operation.progress 在这最大值为0.89(有的是0.9) if (operation.progress >=0.89) { operation.allowSceneActivation = true;//启用自动加载场景 } } }
posted @ 2020-01-15 17:16  D个人笔记  阅读(287)  评论(0编辑  收藏  举报