unity游戏框架学习-消息机制

概述:http://www.javashuo.com/article/p-nggymcxb-bw.htmlhtml

本篇咱们实现一种消息机制。为何须要消息机制,很简单,解耦合。c#

举个例子,游戏里面当资源数量更新时(例如粮食+200),全部显示该资源数量的界面都须要更新该资源的数量文本(例如训练士兵、升级建筑、治疗、研发等等),这可能会涉及十几种界面,而只有打开的界面须要更新。函数

那么当客户端收到服务端的数量更新消息时,在逻辑类里一个个的判断界面是否打开,若是界面打开则调用界面的更新方法显然是很低效、耦合的。那么消息机制的实现方式是怎么样的呢?lua

界面在打开时监听一条事件RESOURCE_DATA_UPDATE,在关闭时移除该事件RESOURCE_DATA_UPDATE,逻辑类收到资源更新消息时会触发这个事件,这样每一个监听该事件的界面均可以收到资源更新的通知。spa

具体的实现是经过事件类型EventType 和c#委托Delegate实现的,EventType是本身定义的枚举。关键代码以下所示指针

private static Dictionary<EventType, Delegate> m_EventTable = new Dictionary<EventType, Delegate>();
public delegate void CallBack();
//添加一个事件(例如界面打开时)
public static void AddListener(EventType eventType, CallBack callBack) { m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack; }
//移除一个事件(例如界面关闭时)
public static void RemoveListener(EventType eventType, CallBack callBack) { m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack; OnListenerRemoved(eventType); }
//触发事件,逻辑类调用
public static void Broadcast(EventType eventType) { Delegate d; if (m_EventTable.TryGetValue(eventType, out d)) { CallBack callBack = d as CallBack; if (callBack != null) { callBack(); } else { throw new Exception(string.Format("广播事件错误:事件{0}对应委托具备不一样的类型", eventType)); } } }

详细代码以下:code

 

1、c#端orm

1.回调定义:本来打算使用params object[] param做为回调参数,这样只须要定义一个回调。可是这种作法会致使频繁的装箱、拆箱操做,并且业务的代码也很差写。htm

装箱:值类型转引用类型,例如int装object。对值类型进行装箱会在堆中分配一个对象实例,并将该值复制到新的对象中。 最后会返回指向该内存的指针。咱们应该尽可能避免装箱操做。对象

public delegate void CallBack();
public delegate void CallBack<T>(T arg);
public delegate void CallBack<T, X>(T arg1, X arg2);
public delegate void CallBack<T, X, Y>(T arg1, X arg2, Y arg3);
public delegate void CallBack<T, X, Y, Z>(T arg1, X arg2, Y arg3, Z arg4);
public delegate void CallBack<T, X, Y, Z, W>(T arg1, X arg2, Y arg3, Z arg4, W arg5);
//public delegate void CallBack(params object[] param);

2.事件定义:

public enum EventType
{
    ShowText,
}

3.事件添加、移除、分发:最多支持五个参数的回调

public class EventCenter
{
    private static Dictionary<EventType, Delegate> m_EventTable = new Dictionary<EventType, Delegate>();

    private static void OnListenerAdding(EventType eventType, Delegate callBack)
    {
        if (!m_EventTable.ContainsKey(eventType))
        {
            m_EventTable.Add(eventType, null);
        }
        Delegate d = m_EventTable[eventType];
        if (d != null && d.GetType() != callBack.GetType())
        {
            throw new Exception(string.Format("尝试为事件{0}添加不一样类型的委托,当前事件所对应的委托是{1},要添加的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
        }
    }
    private static void OnListenerRemoving(EventType eventType, Delegate callBack)
    {
        if (m_EventTable.ContainsKey(eventType))
        {
            Delegate d = m_EventTable[eventType];
            if (d == null)
            {
                throw new Exception(string.Format("移除监听错误:事件{0}没有对应的委托", eventType));
            }
            else if (d.GetType() != callBack.GetType())
            {
                throw new Exception(string.Format("移除监听错误:尝试为事件{0}移除不一样类型的委托,当前委托类型为{1},要移除的委托类型为{2}", eventType, d.GetType(), callBack.GetType()));
            }
        }
        else
        {
            throw new Exception(string.Format("移除监听错误:没有事件码{0}", eventType));
        }
    }
    private static void OnListenerRemoved(EventType eventType)
    {
        if (m_EventTable[eventType] == null)
        {
            m_EventTable.Remove(eventType);
        }
    }
    //no parameters
    public static void AddListener(EventType eventType, CallBack callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] + callBack;
    }
    //Single parameters
    public static void AddListener<T>(EventType eventType, CallBack<T> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] + callBack;
    }
    //two parameters
    public static void AddListener<T, X>(EventType eventType, CallBack<T, X> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] + callBack;
    }
    //three parameters
    public static void AddListener<T, X, Y>(EventType eventType, CallBack<T, X, Y> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] + callBack;
    }
    //four parameters
    public static void AddListener<T, X, Y, Z>(EventType eventType, CallBack<T, X, Y, Z> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] + callBack;
    }
    //five parameters
    public static void AddListener<T, X, Y, Z, W>(EventType eventType, CallBack<T, X, Y, Z, W> callBack)
    {
        OnListenerAdding(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] + callBack;
    }

    //no parameters
    public static void RemoveListener(EventType eventType, CallBack callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    //single parameters
    public static void RemoveListener<T>(EventType eventType, CallBack<T> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    //two parameters
    public static void RemoveListener<T, X>(EventType eventType, CallBack<T, X> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    //three parameters
    public static void RemoveListener<T, X, Y>(EventType eventType, CallBack<T, X, Y> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    //four parameters
    public static void RemoveListener<T, X, Y, Z>(EventType eventType, CallBack<T, X, Y, Z> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }
    //five parameters
    public static void RemoveListener<T, X, Y, Z, W>(EventType eventType, CallBack<T, X, Y, Z, W> callBack)
    {
        OnListenerRemoving(eventType, callBack);
        m_EventTable[eventType] = (CallBack<T, X, Y, Z, W>)m_EventTable[eventType] - callBack;
        OnListenerRemoved(eventType);
    }


    //no parameters
    public static void Broadcast(EventType eventType)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack callBack = d as CallBack;
            if (callBack != null)
            {
                callBack();
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具备不一样的类型", eventType));
            }
        }
    }
    //single parameters
    public static void Broadcast<T>(EventType eventType, T arg)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T> callBack = d as CallBack<T>;
            if (callBack != null)
            {
                callBack(arg);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具备不一样的类型", eventType));
            }
        }
    }
    //two parameters
    public static void Broadcast<T, X>(EventType eventType, T arg1, X arg2)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, X> callBack = d as CallBack<T, X>;
            if (callBack != null)
            {
                callBack(arg1, arg2);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具备不一样的类型", eventType));
            }
        }
    }
    //three parameters
    public static void Broadcast<T, X, Y>(EventType eventType, T arg1, X arg2, Y arg3)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, X, Y> callBack = d as CallBack<T, X, Y>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具备不一样的类型", eventType));
            }
        }
    }
    //four parameters
    public static void Broadcast<T, X, Y, Z>(EventType eventType, T arg1, X arg2, Y arg3, Z arg4)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, X, Y, Z> callBack = d as CallBack<T, X, Y, Z>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3, arg4);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具备不一样的类型", eventType));
            }
        }
    }
    //five parameters
    public static void Broadcast<T, X, Y, Z, W>(EventType eventType, T arg1, X arg2, Y arg3, Z arg4, W arg5)
    {
        Delegate d;
        if (m_EventTable.TryGetValue(eventType, out d))
        {
            CallBack<T, X, Y, Z, W> callBack = d as CallBack<T, X, Y, Z, W>;
            if (callBack != null)
            {
                callBack(arg1, arg2, arg3, arg4, arg5);
            }
            else
            {
                throw new Exception(string.Format("广播事件错误:事件{0}对应委托具备不一样的类型", eventType));
            }
        }
    }
}

 

2、lua端,lua对调并不须要定义。

1.事件定义:

local MESSAGE = 0
local function GetID()
    MESSAGE = MESSAGE + 1
    return MESSAGE
end

--消息table
GMsgDef                = {
    SOCKET_TCP_DATA                                        = GetID(),
}

2.事件处理:

local GameMsg={}

local Messagelisteners = {}
local GMsgDef = GMsgDef
local _msgPool = ObjectPool.new(function() return {} end, "GameMsgPool", false)

local function GetMsg(msgName)
    local msg = GMsgDef[msgName]
    if msg == nil then
        printErrorF("未定义MSG_{%s}", msgName)
    end

    return msg
end

-- msg 为GMsgDef.XXX
-- target 由于调用类的成员须要target,若是backFunc不是类成员 则target传nil;target也用来在实例Dispos的时候统一移除监听,视状况传
-- backFunc 回调函数
function GameMsg.AddMessage(msgName, target, backFunc)
    if not msgName then
        return
    end
    local msg = GetMsg(msgName)

    --操做
    local lstListeners = Messagelisteners[msg]
    if lstListeners == nil then
        lstListeners = {}
        Messagelisteners[msg] = lstListeners
    end
    if GameMsg.HasMessageAlready(msg, target, backFunc) then
        printWarning("GameMsg.AddMessage duplicate, ", msgName)
        return
    end
    local msgObj = _msgPool:Pop()
    msgObj.target = target
    msgObj.func = backFunc
    table.insert(lstListeners, msgObj)
end

-- 传入监听消息列表
function GameMsg.AddMessageList(msglist, target, backFunc)
    if not msglist then
        return
    end
    for _,v in ipairs(msglist) do
        GameMsg.AddMessage(v, target, backFunc)
    end
end

function GameMsg.SendMessage(msgName, ...)
    -- target
    local msg = GetMsg(msgName)
    local msgs = Messagelisteners[msg]
    if msgs == nil then
        return
    end
    for i = #msgs, 1, - 1 do
        local l = msgs[i]
        if l then
            local func = l["func"]
            local target = l["target"]
            if func then
                if target then
                    func(target, ...)
                else
                    func(...)
                end
            end
        end
    end
end

-- 对象和方法都有相应映射的话就返回
function GameMsg.HasMessageAlready(msg, target, func)
    local msgs = Messagelisteners[msg]
    if not msgs then
        return false
    end
    for k, v in ipairs(msgs) do
        if v["func"] == func and v.target==target then
            return true
        end
    end
    return false
end

-- 移除某个消息监听
function GameMsg.RemoveMessage(msgName, target, func)
    local msg = GetMsg(msgName)
    local msgs = Messagelisteners[msg]
    if not msgs then
        return
    end
    for i=#msgs,1,-1 do
        local item = msgs[i]
        if item["func"] == func and item.target==target then
            _msgPool:Push(msgs[i])
            table.remove(msgs, i)
        end
    end
    return false
end


function GameMsg.RemoveMessageByTarget(target)
    for k,v in pairs(Messagelisteners) do
        for i=#v,1,-1 do
            if v[i]["target"] == target then
                _msgPool:Push(v[i])
                table.remove(v, i)
            end
        end
    end
end

function GameMsg.RemoveMessageFromTargetByName(target, msgName)
    --操做
    local msg = GetMsg(msgName)
    local msgs = Messagelisteners[msg]
    if msgs then
        for i=#msgs,1,-1 do
            if msgs[i]["target"] == target then
                _msgPool:Push(msgs[i])
                table.remove(msgs, i)
            end
        end
    end
end

function GameMsg.RemoveAllMessage()
    Messagelisteners = {}
end

return GameMsg