unity异步加载场景过渡

#region MyRegion
/* Author: Wilson
 * Description: 场景切换控制,硬切和异步加载
 * Date: 2019.5.15
 */
#endregion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;


/// 
/// 场景切换控制
/// 
public class LoadingControl : MonoBehaviour
{
    public static LoadingControl instance;
    public string text;

    public GameObject LoadPanel;
    public Image bar;

    int currentProgress; 
    int targetProgress;  

    private void Awake()
    {
        DontDestroyOnLoad(gameObject);
        if (instance == null)
        {
            instance = this;
        }
        else
        {
            Destroy(gameObject);
        }
    }

    public void StartLoading(string scene)
    {
        LoadPanel.SetActive(true);
        currentProgress = 0;
        targetProgress = 0;
        StartCoroutine(LoadingScene(scene));

    }

    /// 
    /// Loading panel
    /// 
    /// 
    /// 
    public IEnumerator LoadingScene(string sceneNum)
    {
        AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneNum); 
        asyncOperation.allowSceneActivation = false;                          
        while (asyncOperation.progress < 0.9f)                                
        {
            targetProgress = (int)(asyncOperation.progress * 100); 
            yield return LoadProgress();
        }
        targetProgress = 100; 
        yield return LoadProgress();
        asyncOperation.allowSceneActivation = true; 
    }

    private IEnumerator LoadProgress()
    {
        while (currentProgress < targetProgress)
        {
            ++currentProgress;                            
            text = currentProgress.ToString() + "%";
            bar.fillAmount = currentProgress / 100f;
            yield return new WaitForEndOfFrame(); 
        }
        if (currentProgress >= 99)
        {
            LoadPanel.SetActive(false);
        }
    }
}

 

你可能感兴趣的:(unity,.Net,framework)