UI窗口类设计框架
所有窗口统一继承一个类WindowBase,之后就可以把所有窗口表示为WindowBase,就可以知道所有窗口的类型是WindowBase,
然后,产生一个管理所有窗口的类WindowsManager,保存所有WindowBase窗口类和GameObject对象的放在字典里,
所有窗口的显示和关闭 通过一个中介者来执行, 也就是说不直接调用窗口类来显示,而是通过一个中介者
中介者类
WindowsEvent,通过一个委托来实现,
WindowBase窗口类初始化时添加一对监听窗口打开关闭消息, 在收到有窗口打开关闭的消息时, 就调用显示关闭窗口代码,销毁时 移除委托的监听
要显示窗口时,就
WindowsEvent产生一个打开窗口消息
总结: 基于统一管理思想
所有窗口类的类型为
WindowBase ,通过
WindowsManager 管理
所有窗口的显示关闭动作调用 通过唯一的类
WindowsEvent
public
class
WindowBase
{
private
GameObject
window;
//...窗口其他东西
public
WindowBase()
{
WindowsEvent
.AddListener(
EwindowEventType
.eGameEvent_Setting_Enter, Show);
WindowsEvent
.AddListener(
EwindowEventType
.eGameEvent_Setting_Enter, Hide);
}
///
<summary>
///
类销毁的时候
///
</summary>
public
void
Destory()
{
WindowsEvent
.RemoveListener(
EwindowEventType
.eGameEvent_Setting_Enter, Show);
WindowsEvent
.RemoveListener(
EwindowEventType
.eGameEvent_Setting_Enter, Hide);
}
void
Show()
{
window.SetActive(
true
);
}
void
Hide()
{
window.SetActive(
false
);
}
}
|
///
<summary>
///
窗口进入 退出的名称枚举
///
</summary>
public
enum
EwindowEventType
{
eGameEvent_Setting_Enter,
eGameEvent_Setting_Exit,
eGameEvent_Menu_Enter,
eGameEvent_Menu_Exit,
}
///
<summary>
///
窗口事件分发类,中介者 ,减少依赖,这个类通过枚举值来调用窗口的显示隐藏
///
</summary>
public
static
class
WindowsEvent
{
///
<summary>
///
窗口动作名称,窗口调用函数
///
</summary>
static
Dictionary
<
EwindowEventType
,
Action
> dict =
new
Dictionary
<
EwindowEventType
,
Action
>();
///
<summary>
///
添加
显示隐藏窗口消息
监听
///
</summary>
///
<param name="
type
"></param>
///
<param name="
action
"></param>
public
static
void
AddListener(
EwindowEventType
type,
Action
action)
{
if
(dict.ContainsKey(type) ==
false
)
{
dict.Add(type,
null
);
}
if
(action !=
null
)
{
dict[type] += action;
}
}
public
static
void
RemoveListener(
EwindowEventType
type,
Action
action)
{
if
(dict.ContainsKey(type) ==
false
)
//没有key
{
return
;
}
else
//有key
{
if
(action ==
null
)
{
return
;
}
else
{
dict[type] -= action;
if
(dict[type] ==
null
)
{
dict.Remove(type);
}
}
}
}
///
<summary>
///
发送显示隐藏窗口消息
///
</summary>
///
<param name="
type
"></param>
public
static
void
Broadcast(
EwindowEventType
type)
{
Action
hasAction;
if
(dict.TryGetValue(type,
out
hasAction))
{
if
(hasAction !=
null
)
{
hasAction();
}
else
{
Debug
.LogError(type +
"没有委托函数"
);
}
}
}
}
|
//其他类
public
class
Aidii
{
void
Start()
{
WindowsEvent
.Broadcast(
EwindowEventType
.eGameEvent_Setting_Enter);
//就显示了设置窗口
}
}
|