Unity:切换场景过度

60 篇文章 2 订阅

创建一个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<CanvasGroup>();
        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();
        }
    }

    /// <summary>
    /// 设置进度条进度
    /// </summary>
    /// <param name="number">进度最大值为0.9</param>
    void SetProgress(float number)
    {
        
    }
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值