📜  加载屏幕统一 - C# 代码示例

📅  最后修改于: 2022-03-11 14:49:02.490000             🧑  作者: Mango

代码示例1
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;

public class LoadingScreen : MonoBehaviour
{
    [HideInInspector]
    private TextMeshProUGUI loadingText;
    [HideInInspector]
    private Slider loadingSlider;

    public void LoadScene(int sceneNumber)
    {
        StartCoroutine(LoadSceneAsync(sceneNumber));
    }

    IEnumerator LoadSceneAsync(int sceneIndex)
    {
        AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);

        while (!operation.isDone)
        {
            Debug.Log("Operation progress");

            float progress = Mathf.Clamp01(operation.progress / 0.9f);

            loadingText.text = Mathf.RoundToInt(progress * 100) + "%";
            loadingSlider.value = progress;

            yield return new WaitForEndOfFrame();
        }
    }
}