F-UI

it2025-10-01  5

简介

细分4种类型,普通,固定,过渡,通知普通类型细分,采用栈式管理消息处理采用委托与监听的方式 UIMessage注册管理UIFormLogin注册实现

UIType

定义相关枚举

using System.Collections; using System.Collections.Generic; using UnityEngine; public class UIType { public UIFormType FormType = UIFormType.Normal; } public enum UIFormType { Fixed, //少 Full, //登陆、进入战斗界面,不需要进栈出栈 Normal, //背包、个人信息界面,进栈出栈,主界面作为第一个元素 Notice, //通知小窗 }

UIBase

各UI模块父类

using System.Collections; using System.Collections.Generic; using UnityEngine; public class UIBase : MonoBehaviour { public UIType UIType { get; set; } = new UIType(); /// <summary> /// 打开 入栈 初始化 /// </summary> public void Open() { gameObject.SetActive(true); } /// <summary> /// 关闭 出栈 /// </summary> public void Close() { gameObject.SetActive(false); } /// <summary> /// 隐藏 被遮挡 不进行栈操作 数据通信正常 /// </summary> public void Hide() { gameObject.SetActive(false); } /// <summary> /// 显现 不进行栈操作 /// </summary> public void Show() { gameObject.SetActive(true); } }

UIMgr

UI管理中心

using System.Collections; using System.Collections.Generic; using UnityEngine; public class UIMgr : MonoBehaviour { #region private member private static UIMgr _ins; /// <summary> /// UI预制体路径 /// </summary> private Dictionary<string, string> _dicFormPath; /// <summary> /// 已加载的UI /// </summary> private Dictionary<string, UIBase> _dicLoadForm; /// <summary> /// 已打开的UI(包含Fixed、Full、Normal、Notice) /// </summary> private Dictionary<string, UIBase> _dicOpenForm; /// <summary> /// UI栈Normal /// </summary> private Stack<UIBase> _staNormal; /// <summary> /// Transform 节点 /// </summary> private Transform _traUiCanvas; private Transform _traUiScript; private Transform _traUiNormal; private Transform _traUiFixed; private Transform _traUiFull; private Transform _traUiNotice; #endregion #region public method /// <summary> /// 实例 /// </summary> /// <returns></returns> public static UIMgr Ins() { if (_ins == null) _ins = new GameObject(UIDefine.UIScriptMgr).AddComponent<UIMgr>(); return _ins; } /// <summary> /// 打开UI /// </summary> /// <param uiname="uiname"></param> public void OpenUI(string uiname) { //打开的 不用管 _dicOpenForm.TryGetValue(uiname, out var uibase); if (uibase != null) return; uibase = LoadUIPrefab(uiname); if (uibase == null) { Debug.LogError("[ERROR]:OpenUI_" + uiname); return; } switch (uibase.UIType.FormType) { case UIFormType.Fixed: OpenFixed(uibase); _dicOpenForm.Add(uiname, uibase); break;//不用管 case UIFormType.Full: OpenFull(uibase); _dicOpenForm.Add(uiname, uibase); break;// case UIFormType.Normal: OpenNormal(uibase); _dicOpenForm.Add(uiname, uibase); break;//进栈出栈 case UIFormType.Notice: OpenNotice(uibase); _dicOpenForm.Add(uiname, uibase); break;// } } /// <summary> /// 关闭UI /// </summary> /// <param uiname="uiname"></param> public void CloseUI(string uiname) { _dicOpenForm.TryGetValue(uiname, out UIBase uibase); if (uibase == null) { Debug.LogError("[ERROR]:CloseUI_" + uiname); return; } switch (uibase.UIType.FormType) { case UIFormType.Fixed: CloseFixed(uibase); _dicOpenForm.Remove(uiname); break; case UIFormType.Full: CloseFull(uibase); _dicOpenForm.Remove(uiname); break; case UIFormType.Normal: CloseNormal(); break; case UIFormType.Notice: CloseNotice(uibase); _dicOpenForm.Remove(uiname); break; } } /// <summary> /// 快捷关闭Normal类型 /// </summary> public void CloseUINormal() { CloseNormal(); } #endregion #region private method private void Awake() { InitTrans(); InitData(); } /// <summary> /// 设置Trans /// </summary> private void InitTrans() { _traUiCanvas = Helper.Find(UIDefine.UICanvasTrans); _traUiScript = Helper.FindChild(_traUiCanvas, UIDefine.UIScriptTrans); _traUiFixed = Helper.FindChild(_traUiCanvas, UIDefine.UIFixedTrans); _traUiNormal = Helper.FindChild(_traUiCanvas, UIDefine.UINormalTrans); _traUiFull = Helper.FindChild(_traUiCanvas, UIDefine.UIFullTrans); _traUiNotice = Helper.FindChild(_traUiCanvas, UIDefine.UINoticeTrans); transform.SetParent(_traUiScript); DontDestroyOnLoad(_traUiCanvas); } /// <summary> /// 读取数据 /// </summary> private void InitData() { _dicFormPath = new Dictionary<string, string>(); _dicLoadForm = new Dictionary<string, UIBase>(); _dicOpenForm = new Dictionary<string, UIBase>(); _staNormal = new Stack<UIBase>(); _dicFormPath.Add("Login1", "Login1"); _dicFormPath.Add("Login2", "Login2"); _dicFormPath.Add("Login3", "Login3"); _dicFormPath.Add("Login4", "Login4"); _dicFormPath.Add("Login5", "Login5"); _dicFormPath.Add("Login6", "Login6"); _dicFormPath.Add("Login7", "Login7"); } /// <summary> /// 加载UI资源 /// </summary> /// <param uiname="uiname"></param> /// <returns></returns> private UIBase LoadUIPrefab(string uiname) { _dicFormPath.TryGetValue(uiname, out string path); if (path==null) { Debug.LogError("[ERROR]:LoadUIPrefab_" + uiname); return null; } _dicLoadForm.TryGetValue(uiname, out var uibase); if (uibase != null) return uibase; //加载 var res = Resources.Load<UIBase>(uiname); uibase = Instantiate<UIBase>(res); _dicLoadForm.Add(uiname, uibase); uibase.gameObject.name = uiname; switch (uibase.UIType.FormType) { case UIFormType.Fixed: uibase.transform.SetParent(_traUiFixed); break; case UIFormType.Full: uibase.transform.SetParent(_traUiFull); break; case UIFormType.Normal: uibase.transform.SetParent(_traUiNormal); break; case UIFormType.Notice: uibase.transform.SetParent(_traUiNotice); break; } return uibase; } //Normal----------------------------------------------------------- private void OpenNormal(UIBase uibase) { Debug.LogError("open" + _staNormal.Count); if (_staNormal.Count > 0) _staNormal.Peek().Hide(); _staNormal.Push(uibase); uibase.Open(); } private void CloseNormal() { Debug.LogError("close"+_staNormal.Count); if (_staNormal.Count == 0) { return; } _dicOpenForm.Remove(_staNormal.Peek().transform.name); _staNormal.Pop().Close(); if (_staNormal.Count >= 1) _staNormal.Peek().Show(); } //Fixed----------------------------------------------------------- private void OpenFixed(UIBase uibase) { uibase.Open(); } private void CloseFixed(UIBase uibase) { uibase.Close(); } //Full----------------------------------------------------------- private void OpenFull(UIBase uibase) { uibase.Open(); } private void CloseFull(UIBase uibase) { uibase.Close(); } //Notice----------------------------------------------------------- private void OpenNotice(UIBase uibase) { uibase.Open(); } private void CloseNotice(UIBase uibase) { uibase.Close(); } #endregion }

UIDefine

定义UI相关

using System.Collections; using System.Collections.Generic; using UnityEngine; public class UIDefine { public const string UICanvasTrans = "Canvas"; public const string UIScriptTrans = "_UIscript"; public const string UIScriptMgr = "UIMgr"; public const string UIFixedTrans = "FixedList"; public const string UINormalTrans = "NormalList"; public const string UINoticeTrans = "NoticeList"; public const string UIFullTrans = "FullList"; }

UIMessage

using System.Collections; using System.Collections.Generic; using UnityEngine; public class UIMessage { public delegate void DeleUIMsg(UIKeyValue kv); public static Dictionary<string, DeleUIMsg> _dicMsg = new Dictionary<string, DeleUIMsg>(); public static void AddListener(string target,DeleUIMsg handle) { if (!_dicMsg.ContainsKey(target)) _dicMsg.Add(target, null); _dicMsg[target] += handle; } public static void RemoveListener(string target,DeleUIMsg handle) { if (_dicMsg.ContainsKey(target)) _dicMsg[target] -= handle; } public static void SendMsg(string target,UIKeyValue kv) { _dicMsg.TryGetValue(target, out DeleUIMsg deleUIMsg); if (deleUIMsg != null) deleUIMsg?.Invoke(kv); } } public class UIKeyValue { public string Key { get; set; } public object Value { get; set; } public UIKeyValue(string key, object valve) { Key = key; Value = valve; } }

UIFormLogin

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UIFormLogin : UIBase { public Button _btn; private void Start() { UIMessage.AddListener("UIFormLogin", OnClick); } private void OnClick(UIKeyValue kv) { Debug.LogError(kv.Key+" "+kv.Value); } }

参考

https://www.cnblogs.com/LiuGuozhu/p/6476079.html

最新回复(0)