【unity基础框架】事件系统

废话少说,先上代码:

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

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

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

}
public class EventInfo : MyEventInfo
{
    public UnityAction myAction;

}

//多参数
public class EventInfo : MyEventInfo
{
    public UnityAction myAction;


}



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

    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(string name, UnityAction action)
    {
        if (actionDic.ContainsKey(name))
        {
            (actionDic[name] as EventInfo).myAction += action;
        }
        else
        {
            actionDic.Add(name, new EventInfo() { myAction = action });

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

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

    //触发事件

    public void TriggerEventLisetener(string name)
    {

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

你可能感兴趣的:(unity,c#,游戏引擎)