/// <summary> /// 加入消息队列,每帧执行固定数量的消息,post后下一帧执行 /// </summary> /// <typeparam name="T">调用时不用填写,会根据参数类型识别</typeparam> /// <param name="msg">发送的消息</param> public static void Post <T>(T msg) { MessageFilter <T> msgFilter = MessageFilter <T> .getInstance(); msgFilter.msgList.Add(msg); filterList.Add(msgFilter); }
/// <summary> /// 注册监听器 /// 同一个对象的生命周期中如果调用了该函数,需要在销毁时调用UnRegister函数以注销监听器,防止重复的调用 /// </summary> /// <typeparam name="T">filter 类型</typeparam> /// <param name="hanlder">注册的函数</param> public static void Register <T>(MessageFilter <T> .MessageBusHanlder hanlder = null) { MessageFilter <T> msgFilter = MessageFilter <T> .getInstance(); if (hanlder == null) //说明来自preinit,不是真正注册消息 { return; } if (msgFilter.hanlderList.Contains(hanlder)) { return; } msgFilter.hanlderList.Add(hanlder); }
/// <summary> /// 执行消息,会立刻调用对应filter的函数 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="msg"></param> public static void Send <T>(T msg) { if (isLocked /* GameSystem.instance.connection.IsLockEvent()*/) { MessageBus.Post <T>(msg); } else { MessageFilter <T> msgFilter = MessageFilter <T> .getInstance(); if (msgFilter != null && msgFilter.hanlderList.Count != 0) { for (int i = 0; i < msgFilter.hanlderList.Count; i++) { (msgFilter.hanlderList[i])(msg); } } } }
/// <summary> /// 反注册,取消监听时调用 /// </summary> /// <typeparam name="T">filter类型</typeparam> /// <param name="hanlder">取消注册的hanlder</param> public static void UnRegister <T>(MessageFilter <T> .MessageBusHanlder hanlder) { MessageFilter <T> msgFilter = MessageFilter <T> .getInstance(); if (msgFilter != null && msgFilter.hanlderList.Count > 0) { for (int i = 0; i < msgFilter.hanlderList.Count; ++i) { if (msgFilter.hanlderList.Contains(hanlder)) { msgFilter.hanlderList.Remove(hanlder); } else { return; } } } }
/// <summary> /// 解除消息锁 /// </summary> /// <typeparam name="T">需要解锁的消息类型</typeparam> public static void UnLockFilterEvent <T>() { MessageFilter <T> msgFilter = MessageFilter <T> .getInstance(); msgFilter.SetFilterLock(false); }