【unity基础框架】事件系统

废话少说,先上代码:

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

/// <summary>
/// 通用接口,有没有泛型都能装,适当的时候进行转化
/// </summary>
public interface MyEventInfo
{

}
/// <summary>
/// 如果有返回值,则基于MyEventInfo接口再创建EventInfo
/// </summary>
public class EventInfo<T> : MyEventInfo
{
    public UnityAction<T> myAction;

}
public class EventInfo : MyEventInfo
{
    public UnityAction myAction;

}

//多参数
public class EventInfo<T, K> : MyEventInfo
{
    public UnityAction<T, K> myAction;


}



public class EventManager : SingletonMono<EventManager>
{
    public Dictionary<string, MyEventInfo> actionDic = new Dictionary<string, MyEventInfo>();
    //添加事件无泛型和有泛型

    public void AddEventListener(string name, UnityAction action)
    {
        if (actionDic.ContainsKey(name))
        {
            (actionDic[name] as EventInfo).myAction += action;
            // Debug.Log("添加了事件" + name);
        }
        else
        {
            //Debug.Log("添加了事件" + name);

            actionDic.Add(name, new EventInfo() { myAction = action });
            Debug.Log("添加了事件" + name + actionDic.ContainsKey(name));
            ;
        }

    }


    public void AddEventListener<T>(string name, UnityAction<T> action)
    {
        if (actionDic.ContainsKey(name))
        {
            (actionDic[name] as EventInfo<T>).myAction += action;
        }
        else
        {
            actionDic.Add(name, new EventInfo<T>() { myAction = action });

        }
    }
    public void AddEventListener<T, K>(string name, UnityAction<T, K> action)
    {
        if (actionDic.ContainsKey(name))
        {
            (actionDic[name] as EventInfo<T, K>).myAction += action;
        }
        else
        {
            actionDic.Add(name, new EventInfo<T, K>() { myAction = action });

        }
    }
    //移除事件
    public void RemoveEventLisetener(string name, UnityAction action)
    {
        if (actionDic.ContainsKey(name))
        {
            (actionDic[name] as EventInfo).myAction -= action;
        }
    }
    public void RemoveEventLisetener<T>(string name, UnityAction<T> action)
    {
        if (actionDic.ContainsKey(name))
        {
            (actionDic[name] as EventInfo<T>).myAction -= action;
        }
    }
    public void RemoveEventLisetener<T, K>(string name, UnityAction<T, K> action)
    {
        if (actionDic.ContainsKey(name))
        {
            (actionDic[name] as EventInfo<T, K>).myAction -= action;
        }
    }

    //触发事件

    public void TriggerEventLisetener(string name)
    {

        (actionDic[name] as EventInfo).myAction?.Invoke();
    }
    public void TriggerEventLisetener<T>(string name, T value)
    {
        //泛型不支持多个参数,只能重写
        Debug.Log("触发了事件" + actionDic.ContainsKey(name));
        (actionDic[name] as EventInfo<T>).myAction?.Invoke(value);
        Debug.Log("触发事件2");
    }
    public void TriggerEventLisetener<T, K>(string name, T value, K value2)
    {
        //重写多参
        (actionDic[name] as EventInfo<T, K>).myAction?.Invoke(value, value2);
    }
    //清空事件
    public void Clean()
    {
        actionDic.Clear();
    }
}

 核心:c#的Dictionary的使用。

将key为事件名,value为事件对象。

AddEventListener传入键值对,若没有key事件,则创建,有则添加
RemoveEventLisetener传入键值对,若没有key什么都不做,有则删除事件
TriggerEventLisetener传入键,若存在就触发所有键的事件
Clean清空

 

测试类:

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

public class EventTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int a=0;
        EventManager.GetInstance().AddEventListener("mouseClick", Test);
        EventManager.GetInstance().AddEventListener<int>("mouseClick2", Test2);
    }
    private void Test()
    {
        Debug.Log("事件中心测试成功");
    }
    private void  Test2(int a)
    {
        Debug.Log("事件中心测试成功2");
    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            EventManager.GetInstance().TriggerEventLisetener("mouseClick");
            EventManager.GetInstance().TriggerEventLisetener<int>("mouseClick2",2);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>