VR游乐园学习(5)-场景加载界面

1、目录结构

2、控制脚本

LoadingPanel.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LoadingPanel : MonoBehaviour
{
    public static LoadingPanel Instance;

    private Image img_LoadingBar;
    private AsyncOperation m_Ao;
    private bool m_IsLoad = false;

    private void Awake()
    {
        Instance = this;
        transform.localScale = Vector3.zero;
        img_LoadingBar = transform.Find("img_LoadingBar").GetComponent<Image>();
    }
    public void LoadScene()
    {
        transform.DOScale(Vector3.one, 0.3f).OnComplete(() =>
        {
            StartCoroutine("Load");
        });
    }
    IEnumerator Load()
    {
        int displayProgress = -1;
        int toProgress = 100;

        while (displayProgress < toProgress)
        {
            displayProgress++;
            ShowProgress(displayProgress); 

            if (m_IsLoad == false)
            {
                m_Ao = SceneManager.LoadSceneAsync(2 + GameItemSpawn.Instance.Index);
                m_Ao.allowSceneActivation = false;
                m_IsLoad = true;
            }
            yield return new WaitForEndOfFrame();
        }
        if (displayProgress == 100)
        {
            m_Ao.allowSceneActivation = true;
            StopCoroutine("Load");
        }
    }
    private void ShowProgress(int progress)
    {
        img_LoadingBar.fillAmount = progress * 0.01f;
    }
}

猜你喜欢

转载自www.cnblogs.com/dream-seeker-201907/p/11606404.html