UI窗口类设计框架

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); //就显示了设置窗口
    }
}
 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值