当前位置: 首页 > 工具软件 > Asyncload > 使用案例 >

【脚本】Unity异步加载AsyncLoad场景进度条显示

汤枫
2023-12-01
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class WSceneLoadAysc : MonoBehaviour
{
    private Slider LoadingSlider;
    private Text LoadText;
    private float AsyncLoadVaule;
    private AsyncOperation async = null;

    void Start()
    {
        LoadText = transform.Find("LoadTip").GetComponent<Text>();
        LoadingSlider = transform.Find("LoadValue").GetComponent<Slider>();

        async = SceneManager.LoadSceneAsync("SeaIslandScene");
        async.allowSceneActivation = false;

        StartCoroutine(AsyncLoad());
    }

    IEnumerator AsyncLoad()
    {
        while (!async.isDone)
        {
            if (async.progress < 0.9f)
            {
                AsyncLoadVaule = async.progress;
            }
            else
            {
                AsyncLoadVaule = 1.0f;
            }

            LoadingSlider.value = AsyncLoadVaule;

            LoadText.text = (int)(LoadingSlider.value * 100) + "%";

            if (AsyncLoadVaule >= 0.9) 
            {
                async.allowSceneActivation = true;
                yield break;
            }
        }
    }
}

 类似资料: