纳金网

标题: NGUI EventDelegate 传递参数的方法 [打印本页]

作者: 王者再临    时间: 2014-11-29 04:52
标题: NGUI EventDelegate 传递参数的方法

在使用NGUI 3.5.8版本时发现 按钮点击事件可以方便的添加 EventDelegate.Add(gameObject.GetCompnent<UIButton>().OnClick,myMethod)
但是与UIEventListener.Get(go).OnClick 相比却不能传递当前的点击对象进去
于是找到    static public void Add (List<EventDelegate> list, EventDelegate ev) 此静态方法(还有一个是否只执行一次的 bool OnShot的重载函数)  

使用如下方法

       EventDelegate ev = new EventDelegate();
        ev.methodName = "mymethod";
        ev.parameters[0] = new EventDelegate.Parameter();
        ev.parameters[0].obj = gameObject;
        EventDelegate.Add(gameObject.GetCompnent<UIButton>().OnClick,ev);
结果发现   编译器报parameters  参数是只读的参数错误,查找发现原脚本发现如下

public Parameter[] parameters
{
  get
  {
#if UNITY_EDITOR
   if (!mCached || !Application.isPlaying) Cache();
#else
   if (!mCached) Cache();
#endif
   return mParameters;
  }
}
找遍整个脚本之后  木有发现 有地方能给 mParameters 赋值,但是在反射调用(Execute (List<EventDelegate> list))函数的时候有判断参数个数结果只能自己添加了
public Parameter[] parameters
{
  get
  {
#if UNITY_EDITOR
   if (!mCached || !Application.isPlaying) Cache();
#else
   if (!mCached) Cache();
#endif
   return mParameters;
  }
/* 在此处添加   set*/   
set
        {
            mParameters = value;
        }
}

经测试后发现 已经能通过此方法传递参数了。
稍微补充一下    : EventDelegate 使用反射的机制调用回调方法 并且自动获取 调用函数的参数类型和参数个数,因此不必担心能否调用方法的问题,倒是UIEventListener使用代理的方式 ,局限性在于只能传递GameObject参数。


作者: hyui    时间: 2014-11-29 05:38
Thakns for this !!!




欢迎光临 纳金网 (http://www.narkii.com/club/) Powered by Discuz! X2.5