游戲事件管理器

事件管理器的由來

在游戲系統(tǒng)中,當(dāng)一個(gè)對(duì)象需要去訪問另一個(gè)對(duì)象的時(shí)候,一般有幾種情況:
1、A對(duì)象是B對(duì)象的成員變量,B可以直接訪問A對(duì)象的公有函數(shù)。
2、A對(duì)象是一個(gè)單例,B對(duì)象可以通過A對(duì)象的單例進(jìn)行訪問。
3、A對(duì)象注冊(cè)了回調(diào)到事件管理器中,B對(duì)象觸發(fā)事件管理器的事件。
這3種方式都能實(shí)現(xiàn)B對(duì)象調(diào)用A對(duì)象的函數(shù),區(qū)別在于第1,2種方法A,B是直接耦合的,第三種方法A,B是沒有耦合關(guān)系的。事件管理器還能起到廣播的作用,例如當(dāng)游戲發(fā)生掉線時(shí),我們可以觸發(fā)掉線的事件,這樣所有注冊(cè)過掉線事件的對(duì)象都能收到通知,也就可以去處理掉線的邏輯,而不用每一個(gè)地方都去監(jiān)測(cè)是否有掉線的情況。

事件的泛型注冊(cè)

當(dāng)我們?nèi)プ?cè)一個(gè)事件的時(shí)候,我們可能需要1個(gè)參數(shù),2個(gè)參數(shù)或者多個(gè)參數(shù),參數(shù)類型也不一樣,你可以通過傳入object的方式,在具體的回調(diào)里再去進(jìn)行拆箱操作,還原成具體的參數(shù),但是這樣就涉及到一個(gè)裝箱和拆箱的操作,不但效率不高還有GC的問題。泛型的參數(shù)設(shè)定可以一定程度上的去解決這個(gè)問題,但是參數(shù)的個(gè)數(shù)還是需要做適配的,例如你有1個(gè),2個(gè),3個(gè),4個(gè)參數(shù)的回調(diào)函數(shù),你就需要去分別取實(shí)現(xiàn)這幾個(gè)參數(shù)的Regsister,UnRegister和TriggerAction函數(shù)。如果函數(shù)參數(shù)太多,那么這個(gè)函數(shù)設(shè)計(jì)的可能有點(diǎn)問題,一般來說,4個(gè)參數(shù)足夠使用了。

代碼實(shí)現(xiàn)

    public enum EventName
    {
        test1,
        test2,
    }
    
    #region 0 param event
    public void RegisterAction(EventName name,Action callback)
    {
        int intName = (int)name;
        Delegate action = null;
        _events.TryGetValue(intName,out action);
        if(action != null)
        {
            Action func = action as Action;
            if(func != null)
            {
                func += callback;
                _events[intName] = func;
            }
            else
            {
                ThrowEventTranslationException(name);
            }
        }
        else
        {
            _events[intName] = callback;
        }

    }

    public void UnRegisterAction(EventName name,Action callback)
    {
        int intName = (int)name;
        Delegate action = null;
        _events.TryGetValue(intName,out action);
        if(action != null)
        {
            Action func = action as Action;
            if(func != null)
            {
                func -= callback;
                _events[intName] = func;
            }
            else
            {
                ThrowEventTranslationException(name);
            }
        }
        else
        {
            ThrowEventAlreadyUnRegsisteredException(name);
        }

    }

    public void TriggerAction(EventName name)
    {
        int intName = (int)name;
        Delegate callback = null;
        _events.TryGetValue(intName,out callback);
        if(callback != null)
        {
            Action func = callback as Action;
            if(func != null)
            {
                func();
            }
            else
            {
                ThrowEventTranslationException(name);
            }
        }
        else
        {
            ThrowEventNotFindException(name);
        }
    }
    #endregion

    #region 1 param event
    public void RegisterAction<T>(EventName name,Action<T> callback)
    {
        int intName = (int)name;
        Delegate action = null;
        _events.TryGetValue(intName,out action);
        if(action != null)
        {
            Action<T> func = action as Action<T> ;
            if(func != null)
            {
                func += callback;
                _events[intName] = func;
            }
            else
            {
                ThrowEventTranslationException(name);
            }
        }
        else
        {
            _events[intName] = callback;
        }
    }

    public void UnRegisterAction<T>(EventName name,Action<T> callback)
    {
        int intName = (int)name;
        Delegate action = null;
        _events.TryGetValue(intName,out action);
        if(action != null)
        {
            Action<T> func = action as Action<T>;
            if(func != null)
            {
                func -= callback;
                _events[intName] = func;
            }
            else
            {
                ThrowEventTranslationException(name);
            }
        }
        else
        {
            ThrowEventAlreadyUnRegsisteredException(name);
        }

    }

    public void TriggerAction<T>(EventName name,T arg1)
    {
        int intName = (int)name;
        Delegate callback = null;
        _events.TryGetValue(intName,out callback);
        if(callback != null)
        {
            Action<T> func = callback as Action<T>;
            if(func != null)
            {
                func(arg1);
            }
            else
            {
                ThrowEventTranslationException(name);
            }
        }
        else
        {
            ThrowEventNotFindException(name);
        }
    }
    #endregion

    #region 2 params event
    public void RegisterAction<T,U>(EventName name,Action<T,U> callback)
    {
        int intName = (int)name;
        Delegate action = null;
        _events.TryGetValue(intName,out action);
        if(action != null)
        {
            Action<T,U> func = action as Action<T,U> ;
            if(func != null)
            {
                func += callback;
                _events[intName] = func;
            }
            else
            {
                ThrowEventTranslationException(name);
            }
        }
        else
        {
            _events[intName] = callback;
        }
    }

    public void UnRegisterAction<T,U>(EventName name,Action<T,U> callback)
    {
        int intName = (int)name;
        Delegate action = null;
        _events.TryGetValue(intName,out action);
        if(action != null)
        {
            Action<T,U> func = action as Action<T,U>;
            if(func != null)
            {
                func -= callback;
                _events[intName] = func;
            }
            else
            {
                ThrowEventTranslationException(name);
            }
        }
        else
        {
            ThrowEventAlreadyUnRegsisteredException(name);
        }

    }

    public void TriggerAction<T,U>(EventName name,T arg1,U arg2)
    {
        int intName = (int)name;
        Delegate callback = null;
        _events.TryGetValue(intName,out callback);
        if(callback != null)
        {
            Action<T,U> func = callback as Action<T,U>;
            if(func != null)
            {
                func(arg1,arg2);
            }
            else
            {
                ThrowEventTranslationException(name);
            }
        }
        else
        {
            ThrowEventNotFindException(name);
        }
    }
    #endregion

寫在最后

為什么字典要用int作為健值呢,因?yàn)槭褂妹杜e作為字典的健值時(shí),枚舉類型沒有實(shí)現(xiàn)沒有實(shí)現(xiàn)IEquatable接口。因此,當(dāng)我們使用Enum類型作為key值時(shí),Dictionary的內(nèi)部操作就需要將Enum類型轉(zhuǎn)換為System.Object,這就導(dǎo)致了Boxing的產(chǎn)生,會(huì)產(chǎn)生GC和效率問題,因此我們將字典的鍵值存為int,可以有效的避免這個(gè)問題。
工程地址:https://github.com/yrsc/EventSystem.git

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容