主要是利用Stack
堆栈(Stack)代表了一个后进先出的对象集合。
当需要对各项进行后进先出的访问时,则使用堆栈。
当在列表中添加一项,称为推入元素,
增加对象时要压入(push)
当从列表中移除一项时,称为弹出元素。
删除对象时要弹出(pop)
Stack 类的方法和属性
Count,代表了Stack中的元素个数
Clear(),清除所有Stack中的元素
Contains(Obj),判断obj(代表元素)是否存在于stack中
Peek(),返回在 Stack 的顶部的对象,但不移除它
Pop(),移除并返回在 Stack 的顶部的对象
Push( obj ),向 Stack 的顶部添加一个对象
ToArray(),复制所有的stack元素到一个数组中
1.使用的单利类
- using UnityEngine;
- ///
- /// 通用Mono单例模板
- ///
- ///
- public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoSingleton<T>
- {
- private static T ms_instance;
-
- public static T Instance
- {
- get
- {
- if (ms_instance == null)
- {
- ms_instance = Instantiate();
- }
-
- return ms_instance;
- }
- }
-
- protected static T Instantiate()
- {
- if (ms_instance != null) return ms_instance;
-
- // 在场景中查找T类型的Mono类
- ms_instance = (T)FindObjectOfType(typeof(T));
- if (FindObjectsOfType(typeof(T)).Length > 1)
- {
- return ms_instance;
- }
-
- if (ms_instance != null) return ms_instance;
-
- // 创建GameObject实例
- var singleton = new GameObject("[Singleton]" + typeof(T).Name);
- DontDestroyOnLoad(singleton); // 设置GameObject不被销毁
- if (singleton == null) return ms_instance;
-
- // 添加Mono单例脚本
- ms_instance = singleton.AddComponent
(); - // 初始化单例
- ms_instance.InitSingleton();
-
- return ms_instance;
- }
-
- protected virtual void InitSingleton()
- {
- }
-
- ///
- /// 使用预制体实例化时创建单例
- ///
- protected virtual void Awake()
- {
- if (ms_instance == null)
- {
- ms_instance = this as T;
-
- // 初始化单例
- InitSingleton();
- }
- else
- {
- // 如果已经存在实例,则需要销毁当前的GameObject
- GameObject.Destroy(this.gameObject);
- }
- }
-
- protected virtual void OnApplicationQuit()
- {
- ms_instance = null;
- // 销毁GameObject
- GameObject.Destroy(this.gameObject);
- }
- }
2.逻辑类
- using System.Collections.Generic;
-
- public class UIBackManage : MonoSingleton<UIBackManage>
- {
- public delegate void OnBackDelegate();
- Stack
_OnBackStack = new Stack(); -
- ///
- /// 注册 (向 Stack 的顶部添加一个对象)
- ///
- ///
- public void PushOnBack(OnBackDelegate cb)
- {
- _OnBackStack.Push(cb);
- }
- ///
- /// 移除并返回在 Stack 的顶部的对象
- ///
- public void PopOnBack()
- {
- if (_OnBackStack.Count > 0)
- {
-
- _OnBackStack.Pop();
- }
- }
- public void ClearOnBackStack()
- {
- _OnBackStack.Clear();
- }
- ///
- /// 使用返回 (移除并返回在 Stack 的顶部的对象)
- ///
- public void OnBack()
- {
- if (_OnBackStack.Count > 0)
- {
- OnBackDelegate BD = _OnBackStack.Pop();
- if (BD != null)
- {
- BD();
- }
- }
- }
- }
3.下面是 三个UI 相互 返回 调用
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
-
- ///
- /// UI 1
- ///
- public class Test02 : MonoSingleton<Test02>
- {
- public Button Fanhui;
- public Button XiayiGe;
- public GameObject MaskGo;
- void Start()
- {
- Fanhui.onClick.AddListener(OnFanhui);
- XiayiGe.onClick.AddListener(OnXiayiGe);
- }
- void OnEnable()
- {
-
- }
- public void SetSteat(bool bo)
- {
- MaskGo.SetActive(bo);
- }
- public void OnFanhui()
- {
- UIBackManage.Instance.OnBack();
- MaskGo.SetActive(false);
- }
- public void OnXiayiGe()
- {
- UIBackManage.Instance.PushOnBack(() => { SetSteat(true); });
- Test01.Instance.SetSteat(true);
- MaskGo.SetActive(false);
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
-
- ///
- /// UI 1
- ///
- public class Test02 : MonoSingleton<Test02>
- {
- public Button Fanhui;
- public Button XiayiGe;
- public GameObject MaskGo;
- void Start()
- {
- Fanhui.onClick.AddListener(OnFanhui);
- XiayiGe.onClick.AddListener(OnXiayiGe);
- }
- void OnEnable()
- {
-
- }
- public void SetSteat(bool bo)
- {
- MaskGo.SetActive(bo);
- }
- public void OnFanhui()
- {
- UIBackManage.Instance.OnBack();
- MaskGo.SetActive(false);
- }
- public void OnXiayiGe()
- {
- UIBackManage.Instance.PushOnBack(() => { SetSteat(true); });
- Test03.Instance.SetSteat(true);
- MaskGo.SetActive(false);
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- ///
- /// UI 3
- ///
- public class Test03 : MonoSingleton<Test03>
- {
- public Button Fanhui;
- public Button XiayiGe;
- public GameObject MaskGo;
- void Start()
- {
- Fanhui.onClick.AddListener(OnFanhui);
- XiayiGe.onClick.AddListener(OnXiayiGe);
- }
- void OnEnable()
- {
-
- }
- public void SetSteat(bool bo)
- {
- MaskGo.SetActive(bo);
- }
- public void OnFanhui()
- {
- UIBackManage.Instance.OnBack();
- MaskGo.SetActive(false);
- }
- public void OnXiayiGe()
- {
- UIBackManage.Instance.PushOnBack(() => { SetSteat(true); });
- Test01.Instance.SetSteat(true);
- MaskGo.SetActive(false);
- }
- }
二:以上 方式 适用于 一个UI接下一个UI 的返回 方式
如果返回按钮 在最顶上呢,只有一个返回按钮时 则不适用,
方法:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- ///
- /// ui1
- ///
- public class Test01 : MonoSingleton<Test01>
- {
- //public Button Fanhui;
- public Button XiayiGe;
- public GameObject MaskGo;
- void Start()
- {
- XiayiGe.onClick.AddListener(OnXiayiGe);
- }
- void OnEnable()
- {
-
- }
- public void SetSteat(bool bo)
- {
- MaskGo.SetActive(bo);
- }
-
- ///
- /// 打开 下一个UI(打开下一个UI时,就把关闭下一个UI和返回当前UI 的操作都注册了)
- ///
- public void OnXiayiGe()
- {
- UIBackManage.Instance.PushOnBack(() => {
- Test02.Instance.SetSteat(false);//注册
- SetSteat(true);//注册
- });
- Test02.Instance.SetSteat(true);//打开 下一个UI
- MaskGo.SetActive(false);//关闭当前UI
- }
- }
返回按钮代码:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- ///
- /// 返回按钮
- ///
- public class FanHui : MonoSingleton<FanHui>
- {
- public Button Fanhui_;
- void Start()
- {
- Fanhui_.onClick.AddListener(OnFanhui);
- }
-
- public void OnFanhui()
- {
- UIBackManage.Instance.OnBack();
- }
- }