using System; using UnityEngine; using System.Collections.Generic; namespace SK.Framework { /// /// 消息中心 /// public class Messenger : MonoBehaviour { private static Messenger instance; private Dictionary> intSubjects; private Dictionary> stringSubjects; private Dictionary> intMessages; private Dictionary> stringMessages; public static Messenger Instance { get { if (instance == null) { instance = new GameObject("[SKFramework.Messenger]").AddComponent(); instance.intSubjects = new Dictionary>(); instance.stringSubjects = new Dictionary>(); instance.intMessages = new Dictionary>(); instance.stringMessages = new Dictionary>(); DontDestroyOnLoad(instance); } return instance; } } private void Subscribe(int subject, Delegate callback) { if (!intSubjects.ContainsKey(subject)) { intSubjects.Add(subject, new List()); } intSubjects[subject].Add(callback); } private void Subscribe(string subject, Delegate callback) { if (!stringSubjects.ContainsKey(subject)) { stringSubjects.Add(subject, new List()); } stringSubjects[subject].Add(callback); } private bool Unsubscribe(int subject, Delegate callback) { if (intSubjects.TryGetValue(subject, out List target)) { target.Remove(callback); if (target.Count == 0) { intSubjects.Remove(subject); } return true; } return false; } private bool Unsubscribe(string subject, Delegate callback) { if (stringSubjects.TryGetValue(subject, out List target)) { target.Remove(callback); if (target.Count == 0) { stringSubjects.Remove(subject); } return true; } return false; } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(int subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(int subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(int subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(int subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(int subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(int subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(string subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(string subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(string subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(string subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(string subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 订阅消息 /// /// 消息主题 /// 订阅事件 public static void Subscribe(string subject, Action callback) { Instance.Subscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(int subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(int subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(int subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(int subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(int subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(int subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(string subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(string subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(string subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(string subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(string subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 取消订阅 /// /// 消息主题 /// 订阅事件 /// 成功取消订阅返回true 否则返回false public static bool Unsubscribe(string subject, Action callback) { return Instance.Unsubscribe(subject, callback); } /// /// 发布消息 /// /// 消息主题 public static void Publish(int subject) { var dic = Instance.intSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(); } } } } /// /// 发布消息 /// /// 消息主题 public static void Publish(int subject, T t) { var dic = Instance.intSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(t); } } } } /// /// 发布消息 /// /// 消息主题 public static void Publish(int subject, T1 t1, T2 t2) { var dic = Instance.intSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(t1, t2); } } } } /// /// 发布消息 /// /// 消息主题 public static void Publish(int subject, T1 t1, T2 t2, T3 t3) { var dic = Instance.intSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(t1, t2, t3); } } } } /// /// 发布消息 /// /// 消息主题 public static void Publish(int subject, T1 t1, T2 t2, T3 t3, T4 t4) { var dic = Instance.intSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(t1, t2, t3, t4); } } } } /// /// 发布消息 /// /// 消息主题 public static void Publish(int subject, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) { var dic = Instance.intSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(t1, t2, t3, t4, t5); } } } } /// /// 发布消息 /// /// 消息主题 public static void Publish(string subject) { var dic = Instance.stringSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(); } } } } /// /// 发布消息 /// /// 消息主题 public static void Publish(string subject, T t) { var dic = Instance.stringSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(t); } } } } /// /// 发布消息 /// /// 消息主题 public static void Publish(string subject, T1 t1, T2 t2) { var dic = Instance.stringSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(t1, t2); } } } } /// /// 发布消息 /// /// 消息主题 public static void Publish(string subject, T1 t1, T2 t2, T3 t3) { var dic = Instance.stringSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(t1, t2, t3); } } } } /// /// 发布消息 /// /// 消息主题 public static void Publish(string subject, T1 t1, T2 t2, T3 t3, T4 t4) { var dic = Instance.stringSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(t1, t2, t3, t4); } } } } /// /// 发布消息 /// /// 消息主题 public static void Publish(string subject, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) { var dic = Instance.stringSubjects; if (dic.TryGetValue(subject, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Action) { Action callback = target[i] as Action; callback.Invoke(t1, t2, t3, t4, t5); } } } } /// /// 打包消息 /// /// 标识码 public static void Pack(int identifier, T t) { var dic = Instance.intMessages; if (!dic.ContainsKey(identifier)) { dic.Add(identifier, new List()); } Message message = Activator.CreateInstance>(); message.content = t; dic[identifier].Add(message); } /// /// 打包消息 /// /// 标识码 public static void Pack(int identifier, T1 t1, T2 t2) { var dic = Instance.intMessages; if (!dic.ContainsKey(identifier)) { dic.Add(identifier, new List()); } Message message = Activator.CreateInstance>(); message.content1 = t1; message.content2 = t2; dic[identifier].Add(message); } /// /// 打包消息 /// /// 标识码 public static void Pack(int identifier, T1 t1, T2 t2, T3 t3) { var dic = Instance.intMessages; if (!dic.ContainsKey(identifier)) { dic.Add(identifier, new List()); } Message message = Activator.CreateInstance>(); message.content1 = t1; message.content2 = t2; message.content3 = t3; dic[identifier].Add(message); } /// /// 打包消息 /// /// 标识码 public static void Pack(int identifier, T1 t1, T2 t2, T3 t3, T4 t4) { var dic = Instance.intMessages; if (!dic.ContainsKey(identifier)) { dic.Add(identifier, new List()); } Message message = Activator.CreateInstance>(); message.content1 = t1; message.content2 = t2; message.content3 = t3; message.content4 = t4; dic[identifier].Add(message); } /// /// 打包消息 /// /// 标识码 public static void Pack(int identifier, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) { var dic = Instance.intMessages; if (!dic.ContainsKey(identifier)) { dic.Add(identifier, new List()); } Message message = Activator.CreateInstance>(); message.content1 = t1; message.content2 = t2; message.content3 = t3; message.content4 = t4; message.content5 = t5; dic[identifier].Add(message); } /// /// 打包消息 /// /// 标识码 public static void Pack(string identifier, T t) { var dic = Instance.stringMessages; if (!dic.ContainsKey(identifier)) { dic.Add(identifier, new List()); } Message message = Activator.CreateInstance>(); message.content = t; dic[identifier].Add(message); } /// /// 打包消息 /// /// 标识码 public static void Pack(string identifier, T1 t1, T2 t2) { var dic = Instance.stringMessages; if (!dic.ContainsKey(identifier)) { dic.Add(identifier, new List()); } Message message = Activator.CreateInstance>(); message.content1 = t1; message.content2 = t2; dic[identifier].Add(message); } /// /// 打包消息 /// /// 标识码 public static void Pack(string identifier, T1 t1, T2 t2, T3 t3) { var dic = Instance.stringMessages; if (!dic.ContainsKey(identifier)) { dic.Add(identifier, new List()); } Message message = Activator.CreateInstance>(); message.content1 = t1; message.content2 = t2; message.content3 = t3; dic[identifier].Add(message); } /// /// 打包消息 /// /// 标识码 public static void Pack(string identifier, T1 t1, T2 t2, T3 t3, T4 t4) { var dic = Instance.stringMessages; if (!dic.ContainsKey(identifier)) { dic.Add(identifier, new List()); } Message message = Activator.CreateInstance>(); message.content1 = t1; message.content2 = t2; message.content3 = t3; message.content4 = t4; dic[identifier].Add(message); } /// /// 打包消息 /// /// 标识码 public static void Pack(string identifier, T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) { var dic = Instance.stringMessages; if (!dic.ContainsKey(identifier)) { dic.Add(identifier, new List()); } Message message = Activator.CreateInstance>(); message.content1 = t1; message.content2 = t2; message.content3 = t3; message.content4 = t4; message.content5 = t5; dic[identifier].Add(message); } /// /// 拆包消息 /// /// 标识码 /// 回调函数 public static void Unpack(int identifier, Action callback) { var dic = Instance.intMessages; if (dic.TryGetValue(identifier, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Message) { Message message = target[i] as Message; callback.Invoke(message.content); target.RemoveAt(i); i--; } } if (target.Count == 0) { dic.Remove(identifier); } } } /// /// 拆包消息 /// /// 标识码 /// 回调函数 public static void Unpack(int identifier, Action callback) { var dic = Instance.intMessages; if (dic.TryGetValue(identifier, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Message) { Message message = target[i] as Message; callback.Invoke(message.content1, message.content2); target.RemoveAt(i); i--; } } if (target.Count == 0) { dic.Remove(identifier); } } } /// /// 拆包消息 /// /// 标识码 /// 回调函数 public static void Unpack(int identifier, Action callback) { var dic = Instance.intMessages; if (dic.TryGetValue(identifier, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Message) { Message message = target[i] as Message; callback.Invoke(message.content1, message.content2, message.content3); target.RemoveAt(i); i--; } } if (target.Count == 0) { dic.Remove(identifier); } } } /// /// 拆包消息 /// /// 标识码 /// 回调函数 public static void Unpack(int identifier, Action callback) { var dic = Instance.intMessages; if (dic.TryGetValue(identifier, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Message) { Message message = target[i] as Message; callback.Invoke(message.content1, message.content2, message.content3, message.content4); target.RemoveAt(i); i--; } } if (target.Count == 0) { dic.Remove(identifier); } } } /// /// 拆包消息 /// /// 标识码 /// 回调函数 public static void Unpack(int identifier, Action callback) { var dic = Instance.intMessages; if (dic.TryGetValue(identifier, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Message) { Message message = target[i] as Message; callback.Invoke(message.content1, message.content2, message.content3, message.content4, message.content5); target.RemoveAt(i); i--; } } if (target.Count == 0) { dic.Remove(identifier); } } } /// /// 拆包消息 /// /// 标识码 /// 回调函数 public static void Unpack(string identifier, Action callback) { var dic = Instance.stringMessages; if (dic.TryGetValue(identifier, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Message) { Message message = target[i] as Message; callback.Invoke(message.content); target.RemoveAt(i); i--; } } if (target.Count == 0) { dic.Remove(identifier); } } } /// /// 拆包消息 /// /// 标识码 /// 回调函数 public static void Unpack(string identifier, Action callback) { var dic = Instance.stringMessages; if (dic.TryGetValue(identifier, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Message) { Message message = target[i] as Message; callback.Invoke(message.content1, message.content2); target.RemoveAt(i); i--; } } if (target.Count == 0) { dic.Remove(identifier); } } } /// /// 拆包消息 /// /// 标识码 /// 回调函数 public static void Unpack(string identifier, Action callback) { var dic = Instance.stringMessages; if (dic.TryGetValue(identifier, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Message) { Message message = target[i] as Message; callback.Invoke(message.content1, message.content2, message.content3); target.RemoveAt(i); i--; } } if (target.Count == 0) { dic.Remove(identifier); } } } /// /// 拆包消息 /// /// 标识码 /// 回调函数 public static void Unpack(string identifier, Action callback) { var dic = Instance.stringMessages; if (dic.TryGetValue(identifier, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Message) { Message message = target[i] as Message; callback.Invoke(message.content1, message.content2, message.content3, message.content4); target.RemoveAt(i); i--; } } if (target.Count == 0) { dic.Remove(identifier); } } } /// /// 拆包消息 /// /// 标识码 /// 回调函数 public static void Unpack(string identifier, Action callback) { var dic = Instance.stringMessages; if (dic.TryGetValue(identifier, out List target)) { for (int i = 0; i < target.Count; i++) { if (target[i] is Message) { Message message = target[i] as Message; callback.Invoke(message.content1, message.content2, message.content3, message.content4, message.content5); target.RemoveAt(i); i--; } } if (target.Count == 0) { dic.Remove(identifier); } } } } }