游戏事件管理

作者:追风剑情 发布于:2020-11-9 10:42 分类:Unity3d

11111.png

事件类型枚举

/// <summary>
/// 自定义游戏事件类型
/// 命名规则:
/// 监听(Listen): 所属模块_事件类型
/// 调用(Invoke): INVOKE_类名_方法名(或事件类型)
/// </summary>
public enum GameEventType
{

}


事件管理器

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
/// <summary>
/// 事件管理器
/// </summary>
public class GameEventManager
{
    private static Dictionary<GameEventType, Action<GameEventType, object>> eventDic = new Dictionary<GameEventType, Action<GameEventType, object>>();

    /// <summary>
    /// 监听事件,必须与RemoveListener()配对使用
    /// </summary>
    public static void AddListener(GameEventType type, Action<GameEventType, object> func)
    {
        if (!eventDic.ContainsKey(type))
        {
            Action<GameEventType, object> action = func;
            eventDic[type] = action;
        }
        else
        {
            eventDic[type] += func;
        }
    }

    public static void RemoveListener(GameEventType type, Action<GameEventType, object> func=null)
    {
        if (!eventDic.ContainsKey(type))
            return;
        if (func != null)
            eventDic[type] -= func;
        else
        {
            eventDic[type] = null;
            eventDic.Remove(type);
        }

        //debug
        //string func_name = string.Empty;
        //if (func != null)
        //    func_name = func.GetType().FullName;
        //Debug.LogFormat("[GameEventManager] RemoveListener({0}, {1})", type, func_name);
    }

    public static void FireEvent(GameEventType type, object data = null)
    {
        if (!eventDic.ContainsKey(type) || eventDic[type] == null)
            return;
        eventDic[type](type, data);
    }

    public static void Clear()
    {
        Debug.Log("[GameEventManager] Clear()");
        foreach(var key in  eventDic.Keys)
        {
            eventDic[key] = null;
            eventDic.Remove(key);
        }
        eventDic.Clear();
    }
}


事件监听器

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public class GameEventListener
{
    private Dictionary<GameEventType, List<Action<GameEventType, object>>> eventDic = new Dictionary<GameEventType, List<Action<GameEventType, object>>>();

    // 注册事件监听器
    public void AddListener(GameEventType type, Action<GameEventType, object> func)
    {
        if (!eventDic.ContainsKey(type))
            eventDic[type] = new List<Action<GameEventType, object>>();
        var list = eventDic[type];
        list.Add(func);
        GameEventManager.AddListener(type, func);
    }

   // 移除事件监听器
    public void RemoveListener()
    {
        foreach (var type in eventDic.Keys)
        {
            var list = eventDic[type];
            if (list == null)
                continue;
            for (int i=0; i < list.Count; i++)
            {
                var fun = list[i];
                if (fun == null)
                    continue;
                GameEventManager.RemoveListener(type, fun);
            }
            if (list != null)
                list.Clear();
        }
        eventDic.Clear();
    }

    public void RemoveListener(GameEventType type)
    {
        if (!eventDic.ContainsKey(type))
            return;
        var list = eventDic[type];
        if (list != null)
        {
            for (int i = 0; i < list.Count; i++)
            {
                var fun = list[i];
                if (fun == null)
                    continue;
                GameEventManager.RemoveListener(type, fun);
            }
            list.Clear();
        }
        eventDic.Remove(type);
    }
}


事件行为基类

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 事件行为
/// </summary>
public class GameEventBehaviour : MonoBehaviour
{
    private GameEventListener m_GameEventListener;

    private void Awake()
    {
        m_GameEventListener = new GameEventListener();
        OnAwake();
    }

    private void Start()
    {
        OnStart();
    }

    private void OnEnable()
    {
        OnLateEnable();
    }

    private void OnDisable()
    {
        OnLateDisable();
    }

    private void Update()
    {
        OnUpdate();
    }

    private void LateUpdate()
    {
        OnLateUpdate();
    }

    private void FixedUpdate()
    {
        OnFixedUpdate();
    }

    private void OnDestroy()
    {
        m_GameEventListener.RemoveListener();
        OnLateDestroy();
    }

    protected virtual void OnAwake() { }
    protected virtual void OnStart() { }
    protected virtual void OnUpdate() { }
    protected virtual void OnLateUpdate() { }
    protected virtual void OnFixedUpdate() { }
    protected virtual void OnLateEnable() { }
    protected virtual void OnLateDisable() { }
    protected virtual void OnLateDestroy() { }

    // 注册事件监听器
    protected void AddListener(GameEventType type, Action<GameEventType, object> func)
    {
        m_GameEventListener.AddListener(type, func);
    }

    // 发送游戏事件
    protected void FireEvent(GameEventType type, object data = null)
    {
        GameEventManager.FireEvent(type, data);
    }

    // 移除事件监听器
    protected void RemoveListener(GameEventType type)
    {
        m_GameEventListener.RemoveListener(type);
    }

    // 移除事件监听器
    protected void RemoveListener()
    {
        m_GameEventListener.RemoveListener();
    }

    //是否已被销毁
    public bool IsDestroyed()
    {
        return this == null;
    }
}


标签: Unity3d

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号