今天的主要任务是完善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. 解决SVN 每次操作都需要重输入用户名密码问题

    把目录C:\Users\当前账号\AppData\Roaming\Subversion\auth下的文件删除,然后重启hbuilder或eclipse工具,重新输入账号密码之后,保存即可解决该问题.

  2. r里面如何实现两列数据合并为一列

    library(dplyr) unite(mtcars, "vs_am", vs, am) Merging Data Adding Columns To merge two dat ...

  3. Ubuntu系统监控indicator-sysmonitor

    参考: http://www.cnblogs.com/EasonJim/p/7130171.html 安装indicator-sysmonitor sudo add-apt-repository pp ...

  4. 64位 windows10,安装配置MYSQL8.0.13

    MySQL的安装配置过程,一查网上一大堆,但是每个人在安装配置的过程中都会碰到一些问题,因为安装的版本不一样,有些命令可能就不适用了.所以安装之前一定先确认好你的版本号. 下面开始安装MYSQL8.0 ...

  5. android控件RecyclerView中,如何显示自定义分割线以及最后一项去除分割线

    在控件RecyclerView中,分割线DividerItemDecoration类的使用经常见,如果是使用自带的分割线,只需要这样写即可 RecyclerView mRecyclerView; mR ...

  6. TortoiseGit功能介绍

    TortoiseGit功能介绍 使用方便 强大的提交对话框 每个项目设置 最小日志消息长度,以避免意外提交空日志消息 用于拼写检查的语言 与问题跟踪系统集成 有用的工具 有多种语言版本 Tortois ...

  7. 遍历文件,读取.wxss文件,在头部添加一条注释

    change.pl #!/usr/bin/perl use autodie; use utf8; use Encode qw(decode encode); use v5.26; my $path = ...

  8. git最佳实践之feature和hotfix分支

    先来复习一波,git的最佳分支管理流程: 再简单复习各个分支: master: 主分支,主要用来版本发布. develop:日常开发分支,该分支正常保存了开发的最新代码. feature:具体的功能开 ...

  9. 大臣的旅费---树的直径(dfs)

    很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国内的各大城市. 为节省经费,T国的大臣们经过思考,制定了一套优秀的修建方案,使得任何一个大城市都能从首都直接或者 ...

  10. return & finally 执行顺序 这是我读到的最合理的解释

    新词:return [expression]  栈顶元素 局部变量的快照 java方法是在栈幀中执行,栈幀是线程私有栈的单位,执行方法的线程会为每一个方法分配一小块栈空间来作为该方法执行时的内存空间, ...