游戏事件管理

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

1111.png

事件类型枚举

/// <summary>
/// 自定义游戏事件类型
/// 命名规则: 所属模块_事件类型
/// </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))
            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 || list.Count <= 0)
                continue;
            foreach (var act in list)
            {
                GameEventManager.RemoveListener(type, act);
            }
            list.Clear();
        }
        eventDic.Clear();
    }

    public void RemoveListener(GameEventType type)
    {
        if (!eventDic.ContainsKey(type))
            return;
        var list = eventDic[type];
        foreach(var act in list)
        {
            GameEventManager.RemoveListener(type, act);
        }
        list.Clear();
        eventDic.Remove(type);
    }
}


事件行为基类

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

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

    private void Start()
    {
        OnStart();
    }

    private void OnEnable()
    {
        OnIEnable();
    }

    private void OnDisable()
    {
        OnIDisable();
    }

    private void Update()
    {
        OnUpdate();
    }

    private void LateUpdate()
    {
        OnLastUpdate();
    }

    private void FixedUpdate()
    {
        OnFixedUpdate();
    }

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

    protected virtual void OnAwake() { }
    protected virtual void OnStart() { }
    protected virtual void OnUpdate() { }
    protected virtual void OnLastUpdate() { }
    protected virtual void OnFixedUpdate() { }
    protected virtual void OnIEnable() { }
    protected virtual void OnIDisable() { }
    protected virtual void OnIDestroy() { }

    // 注册事件监听器
    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();
    }
}


标签: Unity3d

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号