Ps:主要解决耦合问题,把脚本之间的联系通过不同消息类型事件形式进行贯通
1.MessageCenter主脚本
2.DelegateEvent消息类型脚本
3.MC_Default_Data具体接收类脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;/// <summary>
/// 事件管理
/// </summary>
public class MessageCenter : Singleton<MessageCenter>
{// 消息委托public delegate void messageDelHandle(MessagDeta message);// 消息字典public Dictionary<MessageCenterEventType, messageDelHandle> messageMap = new Dictionary<MessageCenterEventType, messageDelHandle>();/// <summary>/// 注册监听/// </summary>public void RegisterListener(MessageCenterEventType messageType, messageDelHandle handle){if (handle == null) return;// 把事件添加到对应的委托中messageDelHandle myHandle = null;messageMap.TryGetValue(messageType, out myHandle);if (myHandle != null){Delegate[] HandleList = myHandle.GetInvocationList();foreach (messageDelHandle item in HandleList){if (item == handle){Debug.LogError($"MessageCenter RegisterListener Type --- <color=yellow>{messageType.ToString()}</color> --- Has Registed Function --- <color=yellow>{handle.Method.Name}</color> ---");return;}}}messageMap[messageType] = (messageDelHandle)Delegate.Combine(myHandle, handle);}/// <summary>/// 移除监听/// </summary>public void RemoveListener(MessageCenterEventType messageType, messageDelHandle handle){if (handle == null) return;messageMap[messageType] = (messageDelHandle)Delegate.Remove(messageMap[messageType], handle);}/// <summary>/// 清空消息/// </summary>public void Clear(){messageMap.Clear();}/// <summary>/// 发送消息/// </summary>/// <param name="messageName">消息类型 </param>/// <param name="body"> 发送消息主体 </param>public void SendMessage(MessageCenterEventType messageType, object body = null){messageDelHandle handle;if (messageMap.TryGetValue(messageType, out handle)){MessagDeta evt = new MessagDeta(messageType, body);try{if (handle != null){handle(evt);}}catch (System.Exception e){Debug.Log($"SendMessage:::{evt.Type.ToString()}:::{e.Message}:::{e.StackTrace}:::{e}");}}}#region 枚举类型接口#region MessageType//public void RegisterListener(MessageCenterEventType messageType, messageDelHandle handle)//{// RegisterListener(messageType, handle);//}//public void RemoveListener(MessageCenterEventType messageType, messageDelHandle handle)//{// RemoveListener(messageType, handle);//}//public void SendMessage(MessageCenterEventType messageType, object body = null)//{// SendMessage(messageType, body);//}#endregion#region BattleEvent//public void RegisterListener(BattleEvent messageType, messageDelHandle handle)//{// RegisterListener((int)messageType, handle);//}//public void RemoveListener(BattleEvent messageType, messageDelHandle handle)//{// RemoveListener((int)messageType, handle);//}//public void SendMessage(BattleEvent messageType, object body = null)//{// SendMessage((int)messageType, body);//}#endregion#region ProtocolEvent//public void RegisterListener(ProtocolEvent messageType, messageDelHandle handle)//{// RegisterListener((int)messageType, handle);//}//public void RemoveListener(ProtocolEvent messageType, messageDelHandle handle)//{// RemoveListener((int)messageType, handle);//}//public void SendMessage(ProtocolEvent messageType, object body = null)//{// SendMessage((int)messageType, body);//}#endregion#endregion}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
/// <summary>
/// 消息类
/// </summary>
public class MessagDeta
{public MessageCenterEventType Type //发送的消息类型{get;private set;}public object Data //消息主体{get;private set;}/// <summary>/// 方法/// </summary>public Action action;/// <summary>/// 构造函数/// </summary>/// <param name="type">消息类型</param>/// <param name="body">消息体</param>public MessagDeta(MessageCenterEventType type, object data,Action action_=null){Type = type;Data = data;if (action_ != null){action = action_;}}
}
/// <summary>
/// 用户事件类型,必须按照类型,顺序添加,合理命名规范
/// </summary>
public enum MessageCenterEventType
{/// <summary>/// UI消息号开始/// </summary>UI_Start = 100000,UI_Fence_ChangeCameraView = 100101, //围栏UI界面切换主相机视角/// <summary>/// 实体消息号开始/// </summary>Entity_Start = 200000,//可以自己添加类型,每个类型预留100000接口Ground_Wire=300000,//主接线图接地线Ground_Wire_ding_wei = 300001,//主接线图接地线定位Ti_shi_tong_yong = 400001,//通用提示/// <summary>/// 其他杂类接口/// </summary>Other_Start = 900000
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class MC_Int_Data
{public int _num;
}public class MC_Uint_Data
{public uint _num;
}public class MC_Long_Data
{public long _num;
}public class MC_Ulong_Data
{public ulong _num;
}public class MC_Shotr_Data
{public short _num;
}public class MC_Ushotr_Data
{public ushort _num;
}public class MC_Float_Data
{public float _num;
}public class MC_Double_Data
{public double _num;
}public class MC_String_Data
{public string _Str;
}public class MC_Char_Data
{public char _char;
}public class MC_Bool_Data
{public bool _flag;
}public class MC_Object_Data
{public object _obj;
}
/// <summary>
/// 接地线控制
/// </summary>
public class Fan_Ground_Wire_Data
{public string id;//public bool active;//是否激活public Fan_Ground_Wire_Data(){}public Fan_Ground_Wire_Data(string ID,bool Active){id = ID;active = Active;}
}
/// <summary>
/// 接地线定位
/// </summary>
public class Fan_Ground_Wire_go
{public string id;// public Fan_Ground_Wire_go(string ID){id = ID;}
}
/// <summary>
/// 提示通用
/// </summary>
public class Ti_shi_tong_yong_c
{public string content;// public string text_icon;// public Action action;public Ti_shi_tong_yong_c(string content_,string text_icon_, Action action_){content = content_;text_icon = text_icon_;action = action_;}
}