UGUI 按钮监听事件


博主之前一直使用的是NGUI3.6.8版本,其中我们注册按钮事件,除了把物体拖到按钮的UIButton脚本的OnClick列表中并选择物体上挂载脚本的方法之外,还有动态的去添加方法。这样便能够在动态生成的按钮上添加监听事件。一般挂载脚本的是按钮的事件确定且有多个按钮。


UIEventListener.Get(button).onClick = OnClickAction;


这几天博主在学习UGUI,当然少不了UGUI的按钮了。首先我们需要了解一下UGUI中的事件。



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类型的方法。所以,当我们的监听事件需要传参的时候,可以使用匿名委托去实现。


暂时更新到这里,有什么发现博主会继续更新。


努力! 奋斗!





-----------------------------------------------------------------------------------

更新2016.9.13


补充一点,Unity本身允许事件函数可以带一个参数,参数类型为bool,float,int,string,Object 这5种。假若需要传递多个参数且这些参数都有关联的话,可以使用一个类来封装传递,例如GameObject。在Button组件里的OnClick列表里,也能拖拽GameObject物体到这个参数。






  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值