usingUnityEngine;usingMyEventManager;publicclassMyEventTest:MonoBehaviour{publicconststring ON_CLICK ="ON_CLICK";publicconststring ON_CLICK2 ="ON_CLICK2";voidStart(){if(!MyEventManager<int>.HasListener(ON_CLICK)) MyEventManager<int>.AddListener(ON_CLICK, OnClick);if(!MyEventManager<string>.HasListener(ON_CLICK2)) MyEventManager<string>.AddListener(ON_CLICK2, OnClick2);}privatevoidOnClick(paramsint[] i){for(int f =0; f < i.Length; f++){
Debug.Log("This is Number is:"+ i[f]);}}privatevoidOnClick2(paramsstring[] s){for(int f =0; f < s.Length; f++){
Debug.Log("This is String is:"+ s[f]);}}privatevoidUpdate(){if(Input.GetMouseButtonDown(0)){
MyEventManager<int>.DispatchEvent(ON_CLICK,1);
MyEventManager<string>.DispatchEvent(ON_CLICK2,"what");}}}
Unity泛型自定义封装事件系统(望大佬指点)一:定义——MyEventManagerusing System.Collections.Generic;namespace MyEventManager{ public class MyEventManager<T> { public static Dictionary<string, MyEventListener<T>> EventDic = new Dictionary<