简易U3D消息中心

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class EventManger

{

/// <summary>

/// 存储所有的事件

/// </summary>

public static Dictionary<TypeEvent, DeleEvent> events = new Dictionary<TypeEvent, DeleEvent>();

/// <summary>

/// 添加事件

/// </summary>

/// <param name="type"></param>

/// <param name="handler"></param>

public static void Add(TypeEvent type, DeleEvent.EventHandler handler)

{

    DeleEvent delEvent;

    if (events.ContainsKey(type))

    {

        delEvent = events[type];

    }

    else

    {

        delEvent = new DeleEvent();

        events[type] = delEvent;//events.add(type,delEvent)

    }

    delEvent.Add(handler);//添加事件

}

/// <summary>

/// 移除事件

/// </summary>

/// <param name="type">事件类型</param>

/// <param name="handler">监听函数</param>

public static void Remove(TypeEvent type, DeleEvent.EventHandler handler)

{

    if (handler == null)

    {

        return;

    }

    if (!events.ContainsKey(type))

    {

        return;

    }

    DeleEvent del = events[type];

    del.Remove(handler);

}

/// <summary>

/// 触发事件  传递数据

/// </summary>

/// <param name="type">事件类型</param>

/// <param name="data">数据</param>

public static void SendEvent(TypeEvent type, object data)

{

    if (!events.ContainsKey(type))

    {

        return;

    }

    //创建事件数据包

    EventMessage mes = new EventMessage();

    mes.type = type;

    mes.data = data;

    DeleEvent del = events[type];

    del.Handler(mes);

}

}

///

/// 事件类

///

public class DeleEvent

{

/// <summary>

/// 定义委托

/// </summary>

/// <param name="data"></param>

public delegate void EventHandler(EventMessage data);

/// <summary>

/// 事件

/// </summary>

public event EventHandler handler;

/// <summary>

/// 触发监听事件,也是具体处理和分发事件的地方

/// </summary>

/// <param name="data"></param>

public void Handler(EventMessage data)

{

    if (handler != null)

    {

        handler(data);

    }

}

/// <summary>

/// 添加事件

/// </summary>

/// <param name="removeHandler"></param>

public void Add(EventHandler removeHandler)

{

    handler += removeHandler;

}

/// <summary>

/// 删除事件

/// </summary>

/// <param name="removeHandler"></param>

public void Remove(EventHandler removeHandler)

{

    if (handler != null)

    {

        handler -= removeHandler;

    }

}

}

///

/// 事件类型(根据实际需求)

///

public enum TypeEvent

{

登录, 注册,升级

}

///

/// 事件数据

///

public class EventMessage

{

/// <summary>

/// 事件类型

/// </summary>

public TypeEvent type;

/// <summary>

/// 数据

/// </summary>

public object data;

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值