Unity 应用消息中心-MessageCenter

 Ps:主要解决耦合问题,把脚本之间的联系通过不同消息类型事件形式进行贯通

1.MessageCenter主脚本

2.DelegateEvent消息类型脚本

3.MC_Default_Data具体接收类脚本

Unity 应用消息中心-MessageCenter_第1张图片

Unity 应用消息中心-MessageCenter_第2张图片

 

 Unity 应用消息中心-MessageCenter_第3张图片

 

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// 
/// 事件管理
/// 
public class MessageCenter : Singleton
{
    // 消息委托
    public delegate void messageDelHandle(MessagDeta message);
    // 消息字典
    public Dictionary messageMap = new Dictionary();

    /// 
    /// 注册监听
    /// 
    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  ---  {messageType.ToString()}  ---  Has Registed Function  ---  {handle.Method.Name}  ---");
                    return;
                }
            }
        }

        messageMap[messageType] = (messageDelHandle)Delegate.Combine(myHandle, handle);
    }
    /// 
    /// 移除监听
    /// 
    public void RemoveListener(MessageCenterEventType messageType, messageDelHandle handle)
    {
        if (handle == null) return;
        messageMap[messageType] = (messageDelHandle)Delegate.Remove(messageMap[messageType], handle);
    }

    /// 
    /// 清空消息
    /// 
    public void Clear()
    {
        messageMap.Clear();
    }

    /// 
    /// 发送消息
    /// 
    /// 消息类型 
    ///  发送消息主体 
    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;
/// 
/// 消息类
/// 
public class MessagDeta
{
    public MessageCenterEventType Type  //发送的消息类型
    {
        get;
        private set;
    }
    public object Data  //消息主体
    {
        get;
        private set;
    }
    /// 
    /// 方法
    /// 
    public Action action;
    /// 
    /// 构造函数
    /// 
    /// 消息类型
    /// 消息体
    public MessagDeta(MessageCenterEventType type, object data,Action action_=null)
    {
        Type = type;
        Data = data;
        if (action_ != null)
        {
            action = action_;
        }
    }
}
/// 
/// 用户事件类型,必须按照类型,顺序添加,合理命名规范
/// 
public enum MessageCenterEventType
{
    /// 
    /// UI消息号开始
    /// 
    UI_Start = 100000,

    UI_Fence_ChangeCameraView = 100101, //围栏UI界面切换主相机视角
    /// 
    /// 实体消息号开始
    /// 
    Entity_Start = 200000,
    //可以自己添加类型,每个类型预留100000接口

    Ground_Wire=300000,//主接线图接地线
    Ground_Wire_ding_wei = 300001,//主接线图接地线定位

 
    Ti_shi_tong_yong = 400001,//通用提示
    /// 
    /// 其他杂类接口
    /// 
    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;
}
/// 
/// 接地线控制
/// 
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;
    }
}
/// 
/// 接地线定位
/// 
public class Fan_Ground_Wire_go
{
    public string id;// 
    public Fan_Ground_Wire_go(string ID)
    {
        id = ID;
    }
}
/// 
/// 提示通用
/// 
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_;
    }
}

 

你可能感兴趣的:(unity,脚本,c#,unity,游戏引擎)