Unity-based event manager

Event manager


Personal use event managers, event distribution for the project, in order to increase the flexibility of the code.

event

Because I used to, the principle of "multi-less attitude '' of using the program comes with a library eventEventHandler

using System.Runtime.InteropServices;

namespace System
{
    [ComVisible(true)]
    public delegate void EventHandler(object sender, EventArgs e);
}

Among them, the parametersEventArgsClass, but also the library comes with:

using System.Runtime.InteropServices;

namespace System
{
    [ComVisible(true)]
    public class EventArgs
    {
        public static readonly EventArgs Empty;

        public EventArgs();
    }
}

Supervisor

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

/// <summary>
/// 事件管理者
/// </summary>
public class EventManager : BaseManager<EventManager>
{
    Dictionary<GameObject, Dictionary<Type, EventHandler>> m_dic = new Dictionary<GameObject, Dictionary<Type, EventHandler>>();

    /// <summary>
    /// 增加事件
    /// </summary>
    /// <typeparam name="T">事件的参数类</typeparam>
    /// <param name="owner">拥有对象</param>
    /// <param name="eh">事件</param>
    public void AddEvent<T>(GameObject owner, EventHandler eh) where T : EventArgs
    {
        if (!m_dic.ContainsKey(owner))
        {
            m_dic[owner] = new Dictionary<Type, EventHandler>();
        }
        Type type = typeof(T);
        if (m_dic[owner].ContainsKey(type))
        {
            m_dic[owner][type] += eh;
        }
        else
        {
            m_dic[owner][type] = eh;
        }
    }

    /// <summary>
    /// 触发事件
    /// </summary>
    /// <typeparam name="T">事件的参数类</typeparam>
    /// <param name="owner">拥有对象</param>
    /// <param name="sender">事件的参数</param>
    /// <param name="t">事件的参数</param>
    public void TriggerEvent<T>(GameObject owner, object sender, T t) where T : EventArgs
    {
        if (m_dic.ContainsKey(owner))
        {
            Type type = typeof(T);
            if (m_dic[owner].ContainsKey(type))
            {
                m_dic[owner][type].Invoke(sender, t);
            }
        }
    }

    /// <summary>
    /// 移除事件
    /// </summary>
    /// <typeparam name="T">事件的参数类</typeparam>
    /// <param name="owner">拥有对象</param>
    /// <param name="eh">事件</param>
    public void RemoveEvent<T>(GameObject owner, EventHandler eh) where T : EventArgs
    {
        if (m_dic.ContainsKey(owner))
        {
            Type type = typeof(T);
            if (m_dic[owner].ContainsKey(type))
            {
                m_dic[owner][type] -= eh;
            }
        }
    }
}

This class is a singleton, inheritance is a singleton parent I have written, there is a special article before recording Base: click on to view the article .
In the code, there is a double dictionary, we used to accurately determine the use to the event. The first weightGameObjectType, an object in the scene; derived from a second weightEventArgsType type class.

use

use thisEvent managerWhen the first to write a eventEventHandlerStructure as a function of:

public void TestEvent(object sender, EventArgs e)
{

}

Then create a derived fromEventArgsTypes of classes:

public class TestEventArgs : EventArgs
{

}

Then the real function of the parameters used to create the class in the form of fields. Then only need to function, theEventArgs class parameters into corresponding type (e.g., Example: TestEventArgs)

public void TestEvent(object sender, EventArgs e)
{
	TestEventArgs tea=(TestEventArgs)e;
}

You can get to the real parameters passed in the need. After that you can write execution logic.
It first before using the event added to the event manager:

EventManager.GetInstance().AddEvent<TestEventArgs>(gameObject, TestEvent);

When a trigger event:

EventManager.GetInstance().TriggerEvent(gameObject, 0new TestEventArgs());

one of themDigital 0, It is a first object type of function parameters, if necessary, can make changes.
The last event when it is not required, it can be deleted in the manager:

EventManager.GetInstance().RemoveEvent<TestEventArgs>(gameObject, TestEvent);
Published 20 original articles · won praise 1 · views 937

Guess you like

Origin blog.csdn.net/f_957995490/article/details/103654376