Unity 事件系统

单例:

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

public class Single<T> : MonoBehaviour where T  : MonoBehaviour
{
    private static T _singleton;

    public static T SingleTon
    {
        get
        {
            if (_singleton == null)
            {
                _singleton = FindObjectOfType<T>();
                if (_singleton == null)
                {
                    Debug.LogError("场景中未找到类的对象,类名为:" + typeof(T).Name + "该类可能没有挂载到场景中");
                }
            }
            return _singleton;
        }
    }

    private void Awake()
    {
        if (_singleton == null)
        {
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
}

事件管理器:

using MoreMountains.Tools;
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class EventManger : Single<EventManger>
{
    public delegate void Event();
    public delegate void Event<T>(ref T t);
    public delegate void Event<T, K>(ref T t, ref K k);
    public delegate void Event<T, K, N>(ref T t, ref K k, ref N n);
    public delegate void Event<T, K, N, S>(ref T t, ref K k, ref N n, ref S s);


    private Dictionary<string, Delegate> Actions = new Dictionary<string, Delegate>();



    //事件管理器
    /*private Dictionary<string, Action> zeroAction=new Dictionary<string, Action>();
    private Dictionary<string, Action<object>> oneAction = new Dictionary<string, Action<object>>();
    private Dictionary<string,Action<object,object,object>> threeAction=new Dictionary<string, Action<object, object, object>>();
    private Dictionary<string, Func<object, object, object, object>> threeFunc = new Dictionary<string, Func<object, object, object, object>>();*/
    //添加事件
    public void AddEvent(string name)
    {
        Event action = null;
        if (Actions.ContainsKey(name))
        {
            Debug.LogError("该事件已存在");
            return;
        }
        Actions.Add(name, action);
    }
    public void AddEvent<T>(string name)
    {
        Event<T> action = null;
        if (Actions.ContainsKey(name))
        {
            Debug.LogError("该事件已存在");
            return;
        }
        Actions.Add(name, action);
    }

    public void AddEvent<T,S>(string name)
    {
        Event<T,S> action = null;
        if (Actions.ContainsKey(name))
        {
            Debug.LogError("该事件已存在");
            return;
        }
        Actions.Add(name, action);
    }
    public void AddEvent<T, K, S>(string name)
    {
        Event<T, K, S> action = null;
        if (Actions.ContainsKey(name))
        {
            Debug.LogError("该事件已存在");
            return;
        }
        Actions.Add(name, action);
    }
    public void AddEvent<T, K, S, N>(string name)
    {
        Event<T, K, S, N> action = null;
        if (Actions.ContainsKey(name))
        {
            Debug.LogError("该事件已存在");
            return;
        }
        Actions.Add(name, action);
    }
    //添加函数
    public Event AddListener(string name, Event action)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Combine(action, Actions[name]);
                    Event ev = (Event)Actions[name];
                    ev += action;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else
            {
                Actions[name] = action;
            }
            return action;
        }
        Debug.LogError("该事件没有创建或者添加");
        return null;
    }
    public Event<T> AddListener<T>(string name, Event<T> action)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Combine(action, Actions[name]);
                    Event<T> ev = (Event<T>)Actions[name];
                    ev += action;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else
            {
                Actions[name] = action;
            }
            return action;
        }
        Debug.LogError("该事件没有创建或者添加");
        return null;
    }
    public Event<T, K> AddListener<T, K>(string name, Event<T, K> action)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Combine(action, Actions[name]);
                    Event<T, K> ev = (Event<T, K>)Actions[name];
                    ev += action;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else
            {
                Actions[name] = action;
            }
            return action;
        }
        Debug.LogError("该事件没有创建或者添加");
        return null;
    }
    public Event<T, K, S> AddListener<T, K, S>(string name, Event<T, K, S> action)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Combine(action, Actions[name]);
                    Event<T, K, S> ev = (Event<T, K, S>)Actions[name];
                    ev += action;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else
            {
                Actions[name] = action;
            }
            return action;
        }
        Debug.LogError("该事件没有创建或者添加");
        return null;
    }
    public Event<T, K, N, S> AddListener<T, K, N, S>(string name, Event<T, K, N, S> action)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Combine(action, Actions[name]);
                    Event<T, K, N, S> ev = (Event<T, K, N, S>)Actions[name];
                    ev += action;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else
            {
                Actions[name] = action;
            }
            return action;
        }
        Debug.LogError("该事件没有创建或者添加");
        return null;
    }


    public void RemoveListener(string name, Event action)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Remove(Actions[name], action);
                    Event ev = (Event)Actions[name];
                    ev -= action;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else Debug.LogError("该事件为空");
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }
    public void RemoveListener<T>(string name, Event<T> action)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Remove(Actions[name], action);
                    Event<T> ev = (Event<T>)Actions[name];
                    ev -= action;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else Debug.LogError("该事件为空");
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }
    public void RemoveListener<T, K>(string name, Event<T, K> action)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Remove(Actions[name], action);
                    Event<T, K> ev = (Event<T, K>)Actions[name];
                    ev -= action;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else Debug.LogError("该事件为空");
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }
    public void RemoveListener<T, K, S>(string name, Event<T, K, S> action)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Remove(Actions[name], action);
                    Event<T, K, S> ev = (Event<T, K, S>)Actions[name];
                    ev -= action;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else Debug.LogError("该事件为空");
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }

    public void RemoveListener<T, K, S, N>(string name, Event<T, K, S, N> action)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {
                try
                {
                    //Actions[name] = Delegate.Remove(Actions[name], action);
                    Event<T, K, S, N> ev = (Event<T, K, S, N>)Actions[name];
                    ev -= action;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }

            }
            else Debug.LogError("该事件为空");
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }

    //触发
    public void Trigger(string name)
    {
        if (Actions.ContainsKey(name))
        {
            ((Event)Actions[name])?.Invoke();
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }

    public void Trigger<T>(string name,ref T a)
    {
        if (Actions.ContainsKey(name))
        {
            ((Event<T>)Actions[name])?.Invoke(ref a);
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }

    public void Trigger<T, S>(string name, ref T t, ref S s)
    {
        if (Actions.ContainsKey(name))
        {
            ((Event<T, S>)Actions[name])?.Invoke(ref t, ref s);
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }


    public void Trigger<T, S, K>(string name, ref T t, ref S s, ref K k)
    {
        if (Actions.ContainsKey(name))
        {
            ((Event<T, S, K>)Actions[name])?.Invoke(ref t, ref s, ref k);
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }

    public void Trigger<T, S, K, N>(string name, ref T t, ref S s, ref K k, ref N n)
    {
        if (Actions.ContainsKey(name))
        {
            ((Event<T, S, K, N>)Actions[name])?.Invoke(ref t, ref s, ref k, ref n);
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }

    public void RemoveAllListener(string name)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Remove(Actions[name], action);
                    Event ev = (Event)Actions[name];
                    ev = null;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else Debug.LogError("该事件为空");
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }
    public void RemoveAllListener<T>(string name)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Remove(Actions[name], action);
                    Event<T> ev = (Event<T>)Actions[name];
                    ev = null;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else Debug.LogError("该事件为空");
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }
    public void RemoveAllListener<T, K>(string name)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Remove(Actions[name], action);
                    Event<T, K> ev = (Event<T, K>)Actions[name];
                    ev = null;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else Debug.LogError("该事件为空");
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }
    public void RemoveAllListener<T, K, S>(string name)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Remove(Actions[name], action);
                    Event<T, K, S> ev = (Event<T, K, S>)Actions[name];
                    ev = null;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else Debug.LogError("该事件为空");
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }
    public void RemoveAllListener<T, K, S, N>(string name)
    {
        if (Actions.ContainsKey(name))
        {
            if (Actions[name] != null)
            {

                try
                {
                    //Actions[name] = Delegate.Remove(Actions[name], action);
                    Event<T, K, S, N> ev = (Event<T, K, S, N>)Actions[name];
                    ev = null;
                    Actions[name] = ev;
                }
                catch (Exception e)
                {
                    Debug.LogError("key:" + name + "对应委托与传入委托参数类型可能不一致" + e.ToString());
                }
            }
            else Debug.LogError("该事件为空");
            return;
        }
        Debug.LogError("该事件没有创建或者添加");
    }

    void Start()
    {


    }
    // Update is called once per frame
    void Update()
    {

    }

    private void OnDestroy()
    {
        Actions.Clear();
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值