今天的主要任务是完善NormalPanel, 搭建PopupPanel。

在编写PanelManager的过程中,发现了一个bug。昨天把panelPath直接传给了ResourceManager.GetInstance().LoadAsset<GameObject>(path);

今天做了修改,并且添加了初始化dictPanelPath的方法,为此在SysDefine新添加了一个类,PanelNameStr。在Helper类里添加了以一个方法GetPathByName。

部分修改代码如下,偷了个懒,直接全部复制粘贴了:

PanelManager.cs

 /*
2018.12.30修改记录:1.增加了SetDictPanelPath()方法
2.修改了CreatePanel()方法
3.增加了DestroyPanel()方法
*/
using System.Collections.Generic;
using UnityEngine;
public class PanelManager
{
//本类实例
private static PanelManager _instance;
//存储面板名字和对应的路径字典
public static Dictionary<string, string> dictPanelPath;
//存储已显示的面板字典
public static Dictionary<string, BasePanel> dictCurPanel;
//存储已隐藏的面板字典
public static Dictionary<string, BasePanel> dictHidePanel;
//存储Popup类型面板的字典
public static Dictionary<string, Stack<BasePanel>> dictPopupPanel; //单例模式
private PanelManager() { }
public static PanelManager GetInstance()
{
if(_instance == null)
{
_instance = new PanelManager(); InitProperties();
SetDictPanelPath();
}
return _instance;
}
//初始化字段
private static void InitProperties()
{
dictPanelPath = new Dictionary<string, string>();
dictCurPanel = new Dictionary<string, BasePanel>();
dictHidePanel = new Dictionary<string, BasePanel>();
dictPopupPanel = new Dictionary<string, Stack<BasePanel>>();
}
private static void SetDictPanelPath()
{
dictPanelPath.Add(PanelNameStr.LogOnPanel, PrefabPathStr.logOnPanelPath);
dictPanelPath.Add(PanelNameStr.RegisterPanel, PrefabPathStr.registerPanelPath);
}
/// <summary>
/// 创建一个面板
/// 先检查dictHidePanel集合里是否存在此面板,有则取出,显示,并加入dictCurPanel集合
/// 没有,则创建一个,然后加如dictCurPanel集合。
/// </summary>
/// <param name="panelName">要创建的面板的名字</param>
/// <returns></returns>
public BasePanel CreatePanel(string panelName)
{
BasePanel basePanel = null;
dictHidePanel.TryGetValue(panelName, out basePanel);
if(basePanel != null)
{
basePanel.Open();
//添加到正在显示的面板集合
dictCurPanel.Add(panelName, basePanel);
return basePanel;
}
else
{ string path = Helper.GetInstance().GetPathByName(panelName); //根据存储路径,加载预制体
GameObject go = ResourceManager.GetInstance().LoadAsset<GameObject>(path);
if(go != null)
{
basePanel = go.GetComponent<BasePanel>();
if(basePanel != null)
{
//添加到正在显示的面板集合
dictCurPanel.Add(panelName, basePanel);
}
else
{
Debug.LogError(GetType()+"你可能忘记挂载了BasePanel类型的脚本");
}
return basePanel;
}
else
{
Debug.Log(GetType()+"请检查配置文件,预制体不存在"); }
}
return null;
} /// <summary>
/// 1.从dictCurPanel集合中取出对应的面板
/// 2.隐藏
/// 3.加入dictHidePanel集合
/// </summary>
/// <param name="PanelName"></param>
public void DestroyPanel(string panelName)
{
BasePanel basePanel = null;
basePanel = dictCurPanel[panelName];
if(basePanel == null)
{
Debug.LogError(GetType()+"面板不存在,请检查配置文件");
return;
}
else
{
//关闭面板
basePanel.Close();
//加入dictHidePanel集合
dictHidePanel.Add(panelName, basePanel);
}
} }

为了测试DestroyPanel,新建了一个面板RegisterPanel。

在LogOnPanel.cs类里测试

 using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class LogOnPanel : BasePanel
{
private void Awake()
{
this.panelType = EPanelType.Normal;
} public void OnStartGameBtnClick()
{
//TO DO
}
//测试DestroyPanel()方法
public void OnRegisterBtnClick()
{
//隐藏自身
PanelManager.GetInstance().DestroyPanel(PanelNameStr.LogOnPanel);
//显示注册面板
PanelManager.GetInstance().CreatePanel(PanelNameStr.RegisterPanel);
} }

另外在设计过程中和最初的想法有些出入,本来打算用工厂方法模式成产各种类型的Panel,现在类型已经在Awake方法里注册了,而且也不麻烦,所以把NormalPanel.cs,PopupPanel.cs,HideOtherPanel.cs删除了,我要去学习Shader了,下午继续更PopupPanel的搭建。

就在我光顾“五谷轮回之所”的时候想到我的代码还有一个严重的Bug,对象池技术中有生有死,有死有生,相互转化。而我的代码做了形式上的转化,却没有做内存处理。当从隐藏面板集合中取出一个面板显示的时候,就是由死转化到生,需要做两个处理,一是从dictHidePanel中移除这个键值对,而是向dictCurPanel中加入这个键值对。同理,当从当前显示面板中取出一个面板隐藏时,就是由生到死,也需要做对应的两个处理。修改代码如下:

  //添加到正在显示的面板集合
dictCurPanel.Add(panelName, basePanel);
//从dictHidePanel集合中移除
dictHidePanel.Remove(panelName);

这是CreatePane()方法中修改的代码。

 //从dictCurPanel集合中移除
dictCurPanel.Remove(panelName);
//加入dictHidePanel集合
dictHidePanel.Add(panelName, basePanel);

这是DestroyPanel()方法中修改的代码。

UI框架搭建DAY2的更多相关文章

  1. Element UI 框架搭建

    Element UI 框架搭建 1.webpack 全局安装 npm install -g webpack 2.淘宝镜像cnpm安装 npm install -g cnpm --registry=ht ...

  2. UI框架搭建DAY1

    分析:UI框架主要是为了用户(使用框架的程序猿)更快捷.方便地开发UI,UI框架的好处还在于解耦,使得程序更具有灵活性. UI框架的核心是窗口的管理,窗口管理的主要任务就是显示窗口和关闭窗口. 因为窗 ...

  3. ASP.NET MVC搭建项目后台UI框架—1、后台主框架

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

  4. ASP.NET MVC搭建项目后台UI框架—11、自动加载下拉框查询

    ASP.NET MVC搭建项目后台UI框架—1.后台主框架 需求:在查询记录的时候,输入第一个字,就自动把以这个字开头的相关记录查找出来,输入2个字就过滤以这两个子开头的记录,依次类推. 突然要用到这 ...

  5. 从零开始,搭建博客系统MVC5+EF6搭建框架(4)上,前后台页面布局页面实现,介绍使用的UI框架以及JS组件

    一.博客系统进度回顾以及页面设计 1.1页面设计说明 紧接前面基础基本完成了框架搭建,现在开始设计页面,前台页面设计我是模仿我博客园的风格来设计的,后台是常规的左右布局风格. 1.2前台页面风格 主页 ...

  6. ASP.NET MVC搭建项目后台UI框架—2、菜单特效

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

  7. ASP.NET MVC搭建项目后台UI框架—3、面板折叠和展开

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

  8. ASP.NET MVC搭建项目后台UI框架—4、tab多页签支持

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

  9. ASP.NET MVC搭建项目后台UI框架—5、Demo演示Controller和View的交互

    目录 ASP.NET MVC搭建项目后台UI框架—1.后台主框架 ASP.NET MVC搭建项目后台UI框架—2.菜单特效 ASP.NET MVC搭建项目后台UI框架—3.面板折叠和展开 ASP.NE ...

随机推荐

  1. 消息中间件系列四:RabbitMQ与Spring集成

    一.RabbitMQ与Spring集成  准备工作: 分别新建名为RabbitMQSpringProducer和RabbitMQSpringConsumer的maven web工程 在pom.xml文 ...

  2. git报错You are not allowed to force push code to a protected branch on this project

    当我们有时候回滚了代码,想强制push到远程仓库的时候, git push origin --force 会报如下错误: You are not allowed to force push code ...

  3. 关于QT Graphics View开启OpenGL渲染后复选框、微调框等无法正常显示的问题

    之前学习QT Graphics View框架,除了基本的图元外,还可以通过QGraphicsProxyWidget类添加QT的基本Widget(如按钮.复选框.单选框等),常使用的场景类接口如下: Q ...

  4. iOS开发之--复制粘贴功能

    复制粘贴功能,代码如下: 1.复制功能 UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = ...

  5. 使用react-navigation提示undefind is not a function

    在学习react-natrive的时候,看到导航跳转一章,遂试了一下demo: 但是呢,在安卓模拟器上却报错了: 找了许多方法,包括降低版本都不行,后来修改了一下导出就可以了:

  6. [原]Chef_Server and Chef_WorkStation and Chef_Client Install Guide[by haibo]

    一.Prerequisite OS  :  CentOS-7.0-1406-x86_64-DVD.iso Time Server :   NTP Server SERVER NAME IP PLAN ...

  7. MySQL 分页数据错乱重复

    select xx from table_name wheere xxx order by 字段A limit offset;, 表数据总共 48 条,分页数量正常,但出现了结果混杂的情况,第一页的数 ...

  8. B - 吉哥系列故事——恨7不成妻

    单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=72 77=71 ...

  9. A - 不要62

    杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍 ...

  10. Solve Error: "errcode": 85005, "errmsg": "appid not bind weapp hint"

    在使用微信官方给的添加自定义菜单的示例代码: { "button":[ { "type":"click", "name" ...