UGUI 按鈕監聽事件


博主以前一直使用的是NGUI3.6.8版本,其中咱們註冊按鈕事件,除了把物體拖到按鈕的UIButton腳本的OnClick列表中並選擇物體上掛載腳本的方法以外,還有動態的去添加方法。這樣便可以在動態生成的按鈕上添加監聽事件。通常掛載腳本的是按鈕的事件肯定且有多個按鈕。
app


UIEventListener.Get(button).onClick = OnClickAction;


這幾天博主在學習UGUI,固然少不了UGUI的按鈕了。首先咱們須要瞭解一下UGUI中的事件。ide



Supported Events

The Eventsystem supports a number of events, and they can be customised further in user custom user written InputModules.

The events that are supported by the StandaloneInputModule and TouchInputModule are provided by interface and can be implemented on a MonoBehaviour by implementing the interface. If you have a valid EventSystem configured the events will be called at the correct time.

IPointerEnterHandler – OnPointerEnter – Called when a pointer enters the object
IPointerExitHandler – OnPointerExit – Called when a pointer exits the object
IPointerDownHandler – OnPointerDown – Called when a pointer is pressed on the object
IPointerUpHandler – OnPointerUp – Called when a pointer is released (called on the original the pressed object)
IPointerClickHandler – OnPointerClick – Called when a pointer is pressed and released on the same object
IBeginDragHandler – OnBeginDrag – Called on the drag object when dragging is about to begin
IDragHandler – OnDrag – Called on the drag object when a drag is happening
IEndDragHandler – OnEndDrag – Called on the drag object when a drag finishes
IDropHandler – OnDrop – Called on the object where a drag finishes
IScrollHandler – OnScroll – Called when a mouse wheel scrolls
IUpdateSelectedHandler – OnUpdateSelected – Called on the selected object each tick
ISelectHandler – OnSelect – Called when the object becomes the selected object
IDeselectHandler – OnDeselect – Called on the selected object becomes deselected
IMoveHandler – OnMove – Called when a move event occurs (left, right, up, down, ect)
ISubmitHandler – OnSubmit – Called when the submit button is pressed
ICancelHandler – OnCancel – Called when the cancel button is pressed
函數


使用接口時須要引用命名空間using UnityEngine.EventSystems; 

而後這是博主測試的一個小腳本,須要掛載在按鈕上,輸出的是按下按鈕的名稱。學習


using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class ItemClickAction : MonoBehaviour, IPointerClickHandler
{

    public void OnPointerClick(PointerEventData eventData)
    {
            Debug.Log(eventData.pointerPress.name);
    }
}


若是咱們要動態添加事件呢?測試


using UnityEngine;
using UnityEngine.UI;

public class Test: MonoBehaviour {

	public GameObject button;

	void Start () {
            Button btn = button.GetComponent<Button>();
            btn.onClick.AddListener(delegate()
            {
                OnClickAction(gameObject);
            });
	}

	void OnClickAction(GameObject obj){

	}
}



爲何要使用匿名委託? 因爲AddListener的參數是UnityAction,這個委託的聲明以下:public delegate void UnityAction(); 因此咱們之間傳參的時候只能傳void類型的方法。因此,當咱們的監聽事件須要傳參的時候,可使用匿名委託去實現。spa


暫時更新到這裏,有什麼發現博主會繼續更新。code


努力! 奮鬥!接口





-----------------------------------------------------------------------------------事件

更新2016.9.13string


補充一點,Unity自己容許事件函數能夠帶一個參數,參數類型爲bool,float,int,string,Object 這5種。倘若須要傳遞多個參數且這些參數都有關聯的話,可使用一個類來封裝傳遞,例如GameObject。在Button組件裏的OnClick列表裏,也能拖拽GameObject物體到這個參數。