纳金网

标题: NGUI 3.0后事件的新写法 [打印本页]

作者: 狂风大尉    时间: 2014-7-29 21:36
标题: NGUI 3.0后事件的新写法

以前NGUI中事件驱动的写法是
  1. {
  2.      UIButton btn = gameObject.GetCompenent<UIButton>();
  3.      btn.onClick = onClick;
  4. }

  5. void onClick(GameObject sender)
  6. {
  7.     Debug.Log(sender.name);
  8. }
复制代码
而新版本NGUI的事件方法都是添加到事件list里的,这样写
  1. {
  2.         UIButton btn = gameObject.GetComponent<UIButton>();
  3.         EventDelegate.Add(btn.onClick, onSelectRole);
  4.         }

  5.     public void onSelectRole()
  6.     {
  7.         Debug.Log(UIButton.current.name);
  8.     }
复制代码
或是这样
  1. {
  2.         UIButton btn = gameObject.GetComponent<UIButton>();
  3.         EventDelegate.Add(btn.onClick, delegate() {
  4.             Debug.Log(UIButton.current.name);
  5.         });
  6.         }
复制代码

作者: hyui    时间: 2014-7-29 22:04
I always love learn from other scripts !!!
作者: 我不再年轻    时间: 2014-7-29 22:25
好像没有以前的简洁
作者: HIDEOKOJIMA    时间: 2014-7-29 22:42
Thanks for sharing this !
作者: libufan    时间: 2014-7-29 23:13
封装的越好,就越来越想Java或者AS3的语法了
作者: Kadina    时间: 2014-7-30 07:06
Very interesting )
作者: iam0623    时间: 2014-8-11 11:47
Very Nice . Thanks




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