Unity简单的场景跳转管理

这里是用一个栈结构储存,因为栈的先进先出,对于返回来说十分方便,不需要自己写返回界面再去考虑调用哪个场景,用Back就完事了,不过必须统一用这个跳转场景才有记录。然后我们有一个加载场景和一个进图条。
我们先拼界面 创建一个Image 然后调整锚点 按着Alt点击右下角就自动全屏了,然后换一个颜色


image.png

然后创建一个Slider拉长
这个控制小球去掉,没有用


image.png

找到这个缩短到看不见吧
image.png

拖动Fill Area把两头和图拉齐
image.png

然后就是这个效果


GIF.gif
/**
 *Copyright(C) 2019 by #COMPANY#
 *All rights reserved.
 *FileName:     #SCRIPTFULLNAME#
 *Author:       #AUTHOR#
 *Version:      #VERSION#
 *UnityVersion:#UNITYVERSION#
 *Date:         #DATE#
 *Description:   
 *History:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using System;

public class LoadManager : Singleton<LoadManager>
{
    //加载场景的名字
    const string LoadSceneName = "LoadScene";
    Stack<string> sence = new Stack<string>();
    LoadScene loadScene = new LoadScene();
    //初始化添加第一个场景
    public void Init()
    {
        string startSceneName = SceneManager.GetActiveScene().name;
        if (!sence.Contains(startSceneName))
        {
            sence.Push(startSceneName);
        }

    }
    /// <summary>
    /// 加载界面 回调可以再加载场景释放静态资源
    /// 或者加载资源(不过这个没什么太大必要,下个场景脚本start也会在Load进度条之内)
    /// </summary>
    /// <param name="name"></param>
    public void Load(string name, Action callBack = null)
    {
        sence.Push(name);
        loadScene.callBack = callBack;
        loadScene.nowScene = GetNowSence();
        SceneManager.LoadScene(LoadSceneName);
    }

    //得到当前场景
    public string GetNowSence()
    {
        return sence.Peek();
    }

    /// <summary>
    /// 返回
    /// </summary>
    public void Back(Action callBack = null)
    {
        //如果就没有场景就退出程序(因为默认开打unity界面不会储存)
        if (sence.Count == 1)
            Application.Quit();
        else if (sence.Count > 1)
        {
            sence.Pop();
            loadScene.callBack = callBack;
            loadScene.nowScene = GetNowSence();
            SceneManager.LoadScene(LoadSceneName);
        }
    }

    //清空栈
    public void Clear()
    {
        sence.Clear();
    }

    //返回加载逻辑类
    public LoadScene LoadScene()
    {
        return loadScene;
    }
}

public class LoadScene
{
    public Action callBack;
    public string nowScene;
    //进度
    float displayProgress = 0;
    //加载场景
    public IEnumerator StartLoading()
    {
        yield return new WaitForSeconds(0.01f);

        displayProgress = 0;
        float toProgress = 0;

        AsyncOperation op = SceneManager.LoadSceneAsync(nowScene);
        //在允许之前不要让场景激活
        op.allowSceneActivation = false;
        while (op.progress < 0.9f)
        {
            //这里是用一个变量储存进度慢慢加载方式瞬间加载满 平缓过渡
            toProgress = op.progress;
            while (displayProgress < toProgress)
            {
                displayProgress += 0.01f;
                yield return new WaitForEndOfFrame();//等待这一帧调用完
            }
        }
        callBack?.Invoke();
        //下面为我们为了防止加载过快 会在0.9停顿一下,不然可能加载界面一闪而过
        toProgress = 1;
        while (displayProgress < toProgress)
        {
            displayProgress += 0.01f;
            yield return new WaitForEndOfFrame();
        }
        callBack = null;
        op.allowSceneActivation = true;
    }
    //获得进度条
    public float GetProgress()
    {
        return displayProgress;
    }
}


还有加载页面需要挂载的脚本 只用挂在Canvas上

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoadFunc : MonoBehaviour
{
    Slider slider;
    void Awake()
    {
        slider = transform.Find("Slider").GetComponent<Slider>();
        StartCoroutine(LoadManager.Instance.LoadScene().StartLoading());
    }

    private void Update()
    {
        slider.value = LoadManager.Instance.LoadScene().GetProgress();
    }

}

然后随便做两个场景测试
入口场景


image.png
/**
 *Copyright(C) 2019 by #COMPANY#
 *All rights reserved.
 *FileName:     #SCRIPTFULLNAME#
 *Author:       #AUTHOR#
 *Version:      #VERSION#
 *UnityVersion:#UNITYVERSION#
 *Date:         #DATE#
 *Description:   
 *History:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        LoadManager.Instance.Init();
        transform.Find("Button").GetComponent<Button>().onClick.AddListener(() => {
            LoadManager.Instance.Load("NewScene");
        });
    }
}


返回场景


image.png
/**
 *Copyright(C) 2019 by #COMPANY#
 *All rights reserved.
 *FileName:     #SCRIPTFULLNAME#
 *Author:       #AUTHOR#
 *Version:      #VERSION#
 *UnityVersion:#UNITYVERSION#
 *Date:         #DATE#
 *Description:   
 *History:
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewScene : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        transform.Find("Button").GetComponent<Button>().onClick.AddListener(() => {
            LoadManager.Instance.Back();
        });
    }
}


打开Build Settings 界面把场景都拖进来

image.png

然后进入返回都没问题
GIF.gif

这个是GitHub
https://github.com/1004019267/LoadManager

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。