UIButton vs UIEventListener

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   os   io   檔案   

在使用NGUI的事件處理時,發現UIButton和UIEventListener之間的共同點越來越多。

當然處理事件,也有一些其它的函數,比如:UIEventTrigger,ButtonMessage 等等,但我用的最多的就是UIButton和UIEventListener

我們知道,只要一個GameObject上添加了Collider(BoxCollider)並且它放在UICamera所渲染的Layer下,

那麼當你在指令碼中寫OnClick()函數時,當我們單擊這個Collider時,就會觸發這個OnClick()函數。

using UnityEngine;using System.Collections;public class ClickTest : MonoBehaviour{    void OnClick()    {        CBase.Log("click");    }}

(圖) 呼叫堆疊

 

因此,我們要使用UIEventListener 來響應UI事件,我們需要做兩件事

1)給UI控制項添加BoxCollider,同時添加UIEventListener指令碼

2) 調用委託的方法

方法一:UIEventListener.Get(gameObject).onClick += MyClickFunction;

方法二:  StartBtn.GetComponent<UIEventListener>().onClick = OnButtonClick;

 

UIEventlistener

UIEventlistener的源檔案放在Scripts/Internal/下,可以這個指令碼的重要性

開啟這個指令碼,可以看到裡面寫了很多的委託。

 

用法一:UIEventListener.Get(gameObject).onClick += MyClickFunction;

其實就是在執行OnClick() 方法,而OnClick()是VoidDelegate(Gameobject go)

用法二:  StartBtn.GetComponent<UIEventListener>().onClick = OnButtonClick;  void OnButtonClick(GameObject obj)

UIButton

基本上來說如果只是處理事件,可以用UIEventListener代替UIButton

UIButton中的這個功能 和以前2.X版本中的ImageButton功能非常相似

 

另附這篇文章寫的很不錯:使用UIEventListener響應NGUI事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.