• UI(UGUI)框架(二)-------------UIManager单例模式与开发BasePanel面板基类/UIManage统一管理UI面板的实例化/开发字典扩展类


    UIManage单实例:

     1   /// 单例模式的核心
     2     /// 1,定义一个静态的对象 在外界访问 在内部构造
     3     /// 2,构造方法私有化
     4 
     5     private static UIManager _instance;
     6 
     7     public static UIManager Instance
     8     {
     9         get
    10         {
    11             if (_instance == null)
    12             {
    13                 _instance = new UIManager();
    14             }
    15             return _instance;
    16         }
    17     }
    18  //构造方法私有化
    19     private UIManager()
    20     {
    21        //解析json文件
    22         ParseUIPanelTypeJson();
    23     }

    创建GameRoot脚本:启动UI(作为启动面板的入口)

     1 using UnityEngine;
     2 using System.Collections;
     3 
     4 public class GameRoot : MonoBehaviour {
     5 
     6     // Use this for initialization
     7     void Start () {
     8         UIManager.Instance.PushPanel(UIPanelType.MainMenu);
     9     }
    10     
    11 
    12 }

     BasePanel面板基类(面板共有的基类,将面板相同的属性与行为抽象出来):

     1 using UnityEngine;
     2 using System.Collections;
     3 
     4 public class BasePanel : MonoBehaviour {
     5     /// <summary>
     6     /// 界面被显示出来
     7     /// </summary>
     8     public virtual void OnEnter()
     9     {
    10 
    11     }
    12 
    13     /// <summary>
    14     /// 界面暂停
    15     /// </summary>
    16     public virtual void OnPause()
    17     {
    18 
    19     }
    20 
    21     /// <summary>
    22     /// 界面继续
    23     /// </summary>
    24     public virtual void OnResume()
    25     {
    26 
    27     }
    28 
    29     /// <summary>
    30     /// 界面不显示,退出这个界面,界面被关系
    31     /// </summary>
    32     public virtual void OnExit()
    33     {
    34 
    35     }
    36 }

    每一个项目里面的面板不一样,也就自己要去创建面板类(继承于BasePanel面板基类):

     

    要在UIManage管理所有的面板:

     1  /// <summary>
     2     /// 根据面板类型 得到实例化的面板
     3     /// </summary>
     4     /// <returns></returns>
     5     private BasePanel GetPanel(UIPanelType panelType)
     6     {
     7         //表示还没创建过这个面板,创建面板
     8         if (panelDict == null)
     9         {
    10             panelDict = new Dictionary<UIPanelType, BasePanel>();
    11         }
    12 
    13         //BasePanel panel;
    14         //panelDict.TryGetValue(panelType, out panel);//TODO
    15         //看以前是否创建过这个类型的面板 TryGet:根据类型得到
    16         BasePanel panel = panelDict.TryGet(panelType);
    17 
    18         if (panel == null)
    19         {
    20             //如果找不到,那么就找这个面板的prefab的路径,然后去根据prefab去实例化面板
    21             //麻烦的方法根据Key得到value,因此将其作为扩展功能写在扩展类当中
    22             //string path;
    23             //panelPathDict.TryGetValue(panelType, out path);
    24             string path = panelPathDict.TryGet(panelType);
    25             GameObject instPanel = GameObject.Instantiate(Resources.Load(path)) as GameObject;
    26             instPanel.transform.SetParent(CanvasTransform,false); //false:保持面板panel的局部rotation location的正常 
    27             panelDict.Add(panelType, instPanel.GetComponent<BasePanel>());//把实例化好的面板保存起来
    28             return instPanel.GetComponent<BasePanel>();
    29         }
    30         else
    31         {
    32             return panel;
    33         }
    34 
    35     }

    开发扩展字典类DictionaryExtension:

     1 using UnityEngine;
     2 using System.Collections;
     3 using System.Collections.Generic;
     4 //扩展方法的语法知识:
     5 /// <summary>:注释自动出来的方法,写好函数之后在函数上方加///系统会自动出来,///要在单独一行,前面没有代码
     6 /// 对Dictory(系统内置的一个类)的扩展方法
     7 /// </summary>
     8 public static class DictionaryExtension  {//扩展类也一定要是静态的
     9     //用一个字典类型的就可以调用这个方法,此方法是字典的扩展方法,dict表示调用这个方法的字典(自动)
    10     /// <summary>
    11     /// 尝试根据key得到value,得到了的话直接返回value,没有得到直接返回null
    12     /// 定义在字典外边,this Dictionary<Tkey,Tvalue> dict表示调用这个方法的字典
    13     /// this Dictionary<Tkey,Tvalue> dict 这个字典表示我们要获取值的字典
    14     /// </summary>
    15     /// //扩展方法一定要是静态方法
    16     public static Tvalue TryGet<Tkey, Tvalue>(this Dictionary<Tkey, Tvalue> dict, Tkey key)
    17     {
    18         Tvalue value;
    19         dict.TryGetValue(key, out value);
    20         return value;
    21     }
    22     
    23 }
  • 相关阅读:
    多线程(一)--线程的运行
    多线程(二)--锁
    守护线程与用户线程
    SWD接口
    RS485,CAN
    tcp/ip协议
    开关电源与线性稳压电源
    与gps相比,北斗的三频信号有什么优势
    射频识别技术(RFID)
    wifi发射模块芯片各个管脚功能,蓝牙和wifi信号互相干扰,2.4GHZ无线技术
  • 原文地址:https://www.cnblogs.com/dsh20134584/p/7348172.html
Copyright © 2020-2023  润新知