场景切换管理

文章介绍了在Unity中使用SceneLoadManager类实现场景切换时的回调功能,包括加载过程中的进度更新和加载完成后的行为。关键代码展示了如何在Update方法中管理和显示加载进度,以及场景加载UI的控制。
摘要由CSDN通过智能技术生成

设计思路:

        对于场景切换,个人认为有两个比较关键的要点是,场景加载时的回调,一般和update绑定,常用于场景切换时的进度条。另外一个为场景加载完成后的回调。

关键代码:

        SceneLoadManager.cs

        因为需要每帧调用,故需要继承MonoBehaviour

public class SceneLoadManager : MonoBehaviour
{
}

        切换场景提供两个回调函数,一个为场景加载时的回调函数,每帧调用。另外一个为场景加载完成后的回调函数。

    public static void LoadScene(string sceneName, UnityAction<float> onProgress, UnityAction onFinish, LoadSceneMode mode = LoadSceneMode.Single)
    {
        GameObject sceneLoadManager = GameObject.Find("SceneLoadManager");
        if (sceneLoadManager == null)
        {
            sceneLoadManager = new GameObject("SceneLoadManager");
            sceneLoadManager.AddComponent<SceneLoadManager>();
            DontDestroyOnLoad(sceneLoadManager);
        }
        //UIManager.Instance.ClearAllWindows();
        _asyncOperation = SceneManager.LoadSceneAsync(sceneName, mode);
        _onProgress = onProgress;

        _asyncOperation.completed += delegate (AsyncOperation asyncOperation)
        {
            onFinish();
            OnCloseLoadSceneUi();
            _asyncOperation = null;
            _onProgress = null;
        };
        
    }

    void Update()
    {
        if (_asyncOperation != null)
        {
            if (_onProgress != null)
            {
                _onProgress(_asyncOperation.progress);
                OnOpenLoadSceneUi(_asyncOperation.progress);
            }
        }
    }

        在update中调用场景加载的ui,一般是一个背景图加一个进度条。在场景加载完成时关闭。

    void Update()
    {
        if (_asyncOperation != null)
        {
            if (_onProgress != null)
            {
                _onProgress(_asyncOperation.progress);
                OnOpenLoadSceneUi(_asyncOperation.progress);
            }
        }
    }


    private static void OnOpenLoadSceneUi(float progress)
    {
        if (!UIManager.Instance.CheckIsCurView(UIPanelType.UiLoadScene))
        {
            UIManager.OpenWindows(UIPanelType.UiLoadScene);
        }
        UIGameLogin curView = UIManager.Instance.GetCurView() as UIGameLogin;
        curView.UpdateView(progress);
    }


    private static void OnCloseLoadSceneUi()
    {
        if (!UIManager.Instance.CheckIsCurView(UIPanelType.UiLoadScene))
        {
            UIManager.CloseWindows();
        }

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值