您的位置:首页 > 产品设计 > UI/UE

UGUI 按钮监听事件

2016-09-07 18:50 260 查看
博主之前一直使用的是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物体到这个参数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: