unity Loading界面

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

public class LoadingPanel : MonoBehaviour
{
    public Text txt;
    public Image blur;
    public Slider slider;

    private Material blurMat;
    private AsyncOperation async;
    private float targetValue = 1;
    private float blurValue = 5;

    void Start()
    {
        slider.value = 0;

        blurMat = blur.material;
        blurMat.SetFloat("_Size", blurValue);

        StartCoroutine(LoadSceneAsync());
    }

    IEnumerator LoadSceneAsync()
    {
        yield return new WaitForSeconds(0.5f);

        async = SceneManager.LoadSceneAsync(2);

        async.allowSceneActivation = false;

        yield return async;
    }

    void Update()
    {
        if (async == null) return;

        if (targetValue != slider.value)
        {
            if (slider.value > 0.89f)
            {
                slider.value = Mathf.Lerp(slider.value, targetValue, Time.deltaTime);
                blurMat.SetFloat("_Size", 0);
                if (Mathf.Abs(slider.value - targetValue) <= 0.02f)
                {
                    slider.value = targetValue;
                    async.allowSceneActivation = true;
                }
            }
            else
            {
                slider.value = Mathf.Lerp(slider.value, async.progress, Time.deltaTime);
                blurMat.SetFloat("_Size", blurValue - blurValue * slider.value);
            }
        }
        txt.text = string.Format("{0}%", (slider.value * 100).ToString("0"));
    }
}

你可能感兴趣的:(unity,游戏引擎)