F-UIMgr

簡介

  • 細(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);
    }
}

參考

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市诀姚,隨后出現(xiàn)的幾起案子响牛,更是在濱河造成了極大的恐慌,老刑警劉巖赫段,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件呀打,死亡現(xiàn)場離奇詭異,居然都是意外死亡糯笙,警方通過查閱死者的電腦和手機(jī)贬丛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來炬丸,“玉大人瘫寝,你說我怎么就攤上這事〕砭妫” “怎么了焕阿?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長首启。 經(jīng)常有香客問我暮屡,道長,這世上最難降的妖魔是什么毅桃? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任褒纲,我火速辦了婚禮,結(jié)果婚禮上钥飞,老公的妹妹穿的比我還像新娘莺掠。我一直安慰自己,他們只是感情好读宙,可當(dāng)我...
    茶點故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布彻秆。 她就那樣靜靜地躺著,像睡著了一般结闸。 火紅的嫁衣襯著肌膚如雪唇兑。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天桦锄,我揣著相機(jī)與錄音扎附,去河邊找鬼。 笑死结耀,一個胖子當(dāng)著我的面吹牛留夜,可吹牛的內(nèi)容都是我干的匙铡。 我是一名探鬼主播,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼碍粥,長吁一口氣:“原來是場噩夢啊……” “哼慰枕!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起即纲,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎博肋,沒想到半個月后低斋,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盈蛮,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡喊巍,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了锥涕。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片病游。...
    茶點故事閱讀 40,505評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡唇跨,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出衬衬,到底是詐尸還是另有隱情买猖,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布滋尉,位于F島的核電站玉控,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏狮惜。R本人自食惡果不足惜高诺,卻給世界環(huán)境...
    茶點故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望碾篡。 院中可真熱鬧虱而,春花似錦、人聲如沸开泽。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽眼姐。三九已至诅迷,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間众旗,已是汗流浹背罢杉。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留贡歧,地道東北人滩租。 一個月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓赋秀,卻偏偏與公主長得像,于是被迫代替她去往敵國和親律想。 傳聞我的和親對象是個殘疾皇子猎莲,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,515評論 2 359

推薦閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,111評論 1 32
  • Unity UI架構(gòu)設(shè)計理念 1.以ARPG為例,多個場景會反復(fù)出現(xiàn)相同的“UI窗體”技即,造成多個場景中反復(fù)加載相同...
    Magic_Dong閱讀 14,998評論 2 29
  • 微距下的花著洼,鏡頭廣告做的很好,拍出來而叼,暈身笤,還是專業(yè)相機(jī)不一樣吧,這個鏡頭就是玩玩而已葵陵。先練練手液荸。發(fā)現(xiàn)單位的花也開了...
    芩hot閱讀 170評論 2 1
  • 昨天娇钱,看到格格《從北京到昆明,我終于成為學(xué)習(xí)狂——記混沌大學(xué)超級團(tuán)長成長營》绊困,記錄了很多金句文搂,其中這一句“世界因你...
    flyinrain12123閱讀 935評論 0 5
  • PercentLayout 百分比布局,控制子view 在布局文件占用的大小考抄,多適配方案的一種比較好的選擇细疚。 擁有...
    chendroid閱讀 758評論 0 1