/// <summary> /// 添加某个Unity函数回调的指定方法 /// </summary> /// <param name="FunName">要添加的Unity函数 枚举</param> /// <param name="obj">给对应物体添加回调</param> /// <param name="func">lua层回调的方法</param> public void AddCallBack(CallBackEnum FunName, GameObject obj, LuaFunction func) { try { if (!PeriodDic.FTryGetValue(FunName)) { Dictionary <string, LuaFunction> dic = new Dictionary <string, LuaFunction>(); dic.Add(obj.GetInstanceID().ToString(), func); PeriodDic.Add(FunName, dic); } else { if (!PeriodDic[FunName].FTryGetValue(obj.GetInstanceID().ToString())) { PeriodDic[FunName].Add(obj.GetInstanceID().ToString(), func); } else { PeriodDic[FunName][obj.GetInstanceID().ToString()] = func; } } } catch (Exception e) { Debugger.LogError(e); } }
public LuaFunction GetObjCallBack(CallBackEnum FunName, GameObject obj) { if (PeriodDic.Count == 0 || !PeriodDic.FTryGetValue(FunName) || !PeriodDic[FunName].FTryGetValue(obj.GetInstanceID().ToString())) { return(null); //字典为空或者没有对应函数的注册直接返回 } return(PeriodDic[FunName][obj.GetInstanceID().ToString()]); }
/// <summary> /// 执行回调事件 /// </summary> /// <param name="FunName">要添加的Unity函数 枚举</param> /// <param name="obj">给对应物体添加回调</param> /// <param name="arg">传递的回调参数</param> public void CallBack(CallBackEnum FunName, GameObject obj, params object[] arg) { if (PeriodDic.Count == 0) { return; //字典为空直接返回 } if (PeriodDic.FTryGetValue(FunName)) { if (PeriodDic[FunName].FTryGetValue(obj.GetInstanceID().ToString())) { PeriodDic[FunName][obj.GetInstanceID().ToString()].Call(arg); } } }
/// <summary> /// 清除某个Unity函数回调的指定方法 /// </summary> /// <param name="FunName">要清除的Unity函数 枚举</param> /// <param name="obj">添加回调的对应物体</param> public void RemoveCallBack(CallBackEnum FunName, GameObject obj) { try { if (PeriodDic.Count == 0 || !PeriodDic.FTryGetValue(FunName)) { return; //字典为空或者没有对应函数的注册直接返回 } if (PeriodDic[FunName].FTryGetValue(obj.GetInstanceID().ToString())) { PeriodDic[FunName].Remove(obj.GetInstanceID().ToString()); } } catch (Exception e) { Debugger.LogError(e); } }