推薦使用方法4 比較簡單 性能好一點
事件監聽處理
1. 點擊事件:【UIEventListener.Get(GameObject).onClick = OnClick;】
2. 其它事件基本與【1】中一致,支持的具體事件可以看UIEventListener中定義。
3. UIEventListener.Get()方法中會在對應的GameObject上創建一個UIEventListener腳本。
UIEventListener.Get(roadCreatButton.gameObject).onClick =
delegate(GameObject go)
{
Debug.Log("這裏寫處理點擊按鈕事件的邏輯");
};
方法2
在NGUI下UIRoot下的物體,如果當前物體有Box Collider,就可以當前物體上掛一個腳本,點擊事件可以用
void OnClick(){} 這個方法監聽鼠標單擊
void OnPress(boo isPress)//這個方法來監聽鼠標按下擡起 ,OnPress拖拽時也會觸發,click按下擡起才觸發
{
Debug.Log("這裏寫處理點擊按鈕事件的邏輯");
}
方法3
UIButton headBtn = transform.Find("HeadButton").GetComponent<UIButton>();
headBtn.onClick.Add(new EventDelegate(this,"OnHeadButtonClick"));//爲按鈕註冊點擊事件,
void OnHeadButtonClick()
{
Debug.Log("這裏寫處理點擊按鈕事件的邏輯");
}
方法4
UIButton close_button = transform.GetComponent<UIButton>();
close_button.onClick.Add(new EventDelegate(() =>
{
Debug.Log("這裏寫處理點擊按鈕事件的邏輯");
}));