簡介
- 細(xì)分4種類型稍算,普通鸣奔,固定墨技,過渡,通知
- 普通類型細(xì)分挎狸,采用棧式管理
- 消息處理采用委托與監(jiān)聽的方式
- UIMessage注冊管理
- UIFormLogin注冊實現(xiàn)
UIType
定義相關(guān)枚舉
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIType
{
public UIFormType FormType = UIFormType.Normal;
}
public enum UIFormType
{
Fixed, //少
Full, //登陸扣汪、進(jìn)入戰(zhàn)斗界面,不需要進(jìn)棧出棧
Normal, //背包锨匆、個人信息界面崭别,進(jìn)棧出棧冬筒,主界面作為第一個元素
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>
/// 關(guān)閉 出棧
/// </summary>
public void Close()
{
gameObject.SetActive(false);
}
/// <summary>
/// 隱藏 被遮擋 不進(jìn)行棧操作 數(shù)據(jù)通信正常
/// </summary>
public void Hide()
{
gameObject.SetActive(false);
}
/// <summary>
/// 顯現(xiàn) 不進(jìn)行棧操作
/// </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預(yù)制體路徑
/// </summary>
private Dictionary<string, string> _dicFormPath;
/// <summary>
/// 已加載的UI
/// </summary>
private Dictionary<string, UIBase> _dicLoadForm;
/// <summary>
/// 已打開的UI(包含F(xiàn)ixed、Full茅主、Normal舞痰、Notice)
/// </summary>
private Dictionary<string, UIBase> _dicOpenForm;
/// <summary>
/// UI棧Normal
/// </summary>
private Stack<UIBase> _staNormal;
/// <summary>
/// Transform 節(jié)點
/// </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;//進(jìn)棧出棧
case UIFormType.Notice:
OpenNotice(uibase);
_dicOpenForm.Add(uiname, uibase);
break;//
}
}
/// <summary>
/// 關(guān)閉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>
/// 快捷關(guān)閉Normal類型
/// </summary>
public void CloseUINormal()
{
CloseNormal();
}
#endregion
#region private method
private void Awake()
{
InitTrans();
InitData();
}
/// <summary>
/// 設(shè)置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>
/// 讀取數(shù)據(jù)
/// </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相關(guān)
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);
}
}