Unity:切换场景过度

创建一个Canvas挂载以下代码,代码下有一个带射线碰撞的透明Image名字叫"stopSelectAll"并设置为隐藏和一个不带射线碰撞的加载页面图片的Image名字叫"Loading"

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
 
public class SceneChangeManager : MonoBehaviour
{
    private static SceneChangeManager main;
 
    public static SceneChangeManager Main
    {
        get { return main; }
    }
 
    private CanvasGroup canvasGroup;
 
    private GameObject stopSelectAll;
 
    private bool isloading;

    private AsyncOperation operation;
 
    private Action LoadAction;
    // Start is called before the first frame update
    void Awake()
    {
        main = this;
        canvasGroup = transform.Find("Loading").GetComponent();
        stopSelectAll = transform.Find("stopSelectAll").gameObject;
        DontDestroyOnLoad(gameObject);
        LoadAction = () => { };
        SceneManager.sceneLoaded += (scene, mode) =>
        {
            stopSelectAll.SetActive(false);
            StartCoroutine(CloseLoading(canvasGroup));
            isloading = false;
            LoadAction();
            LoadAction = () => { };
        };
    }
 
    public void LoadScene(string sceneName)
    {
        if (!isloading)
        {
            SetProgress(0);
            stopSelectAll.SetActive(true);
            StartCoroutine(OpenLoading(canvasGroup, () => { StartCoroutine(LoadingScene(sceneName)); }));
            isloading = true;
        }
    }
    
    public void LoadScene(string sceneName,Action action)
    {
        LoadAction = action;
        LoadScene(sceneName);
    }
    public void LoadSceneNum(string sceneName)
    {
        SceneManager.LoadSceneAsync(sceneName);
    }

    private IEnumerator OpenLoading(CanvasGroup canvasGroup,Action action)
    {
        while (true)
        {
            yield return new WaitForSeconds(0.01f);
            canvasGroup.alpha += 0.04f;
            if (canvasGroup.alpha >= 1.0f)
            {
                action();
                yield break;
            }
        }
    }
    private IEnumerator CloseLoading(CanvasGroup canvasGroup)
    {
        while (true)
        {
            yield return new WaitForSeconds(0.01f);
            canvasGroup.alpha -= 0.04f;
            if (canvasGroup.alpha <= 0.0f)
            {
               yield break;
            }
        }
    }

    IEnumerator LoadingScene(string name)
    {
        operation = SceneManager.LoadSceneAsync(name);
        operation.allowSceneActivation = true;
        while (!operation.isDone)
        {
            SetProgress(operation.progress);
            yield return new WaitForEndOfFrame();
        }
    }

    /// 
    /// 设置进度条进度
    /// 
    /// 进度最大值为0.9
    void SetProgress(float number)
    {
        
    }
}

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