按钮的监听事件系统
2015-05-04 17:58
399 查看
继续学习,我相信大家在做NGUI开发的时候处理事件都会用到UIEventListener,那么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
点击打开链接
OK 怎么样才能让UGUI监听的方式和NGUI差不多呢? 这里我给大家引一个思路,把下面代码放在你的项目里。
然后在你的界面里面写入监听按钮的代码。
虽然还有一些别的监听方式,但是我觉得这种方式是最科学的,大家可根据自己项目的需求继续拓展EventTriggerListener类。
转载自:雨松MOMO 2014年10月27日 于 雨松MOMO程序研究院 发表
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
点击打开链接
OK 怎么样才能让UGUI监听的方式和NGUI差不多呢? 这里我给大家引一个思路,把下面代码放在你的项目里。
using UnityEngine; using System.Collections; using UnityEngine.EventSystems; public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger{ public delegate void VoidDelegate (GameObject go); public VoidDelegate onClick; public VoidDelegate onDown; public VoidDelegate onEnter; public VoidDelegate onExit; public VoidDelegate onUp; public VoidDelegate onSelect; public VoidDelegate onUpdateSelect; static public EventTriggerListener Get (GameObject go) { EventTriggerListener listener = go.GetComponent<EventTriggerListener>(); if (listener == null) listener = go.AddComponent<EventTriggerListener>(); return listener; } public override void OnPointerClick(PointerEventData eventData) { if(onClick != null) onClick(gameObject); } public override void OnPointerDown (PointerEventData eventData){ if(onDown != null) onDown(gameObject); } public override void OnPointerEnter (PointerEventData eventData){ if(onEnter != null) onEnter(gameObject); } public override void OnPointerExit (PointerEventData eventData){ if(onExit != null) onExit(gameObject); } public override void OnPointerUp (PointerEventData eventData){ if(onUp != null) onUp(gameObject); } public override void OnSelect (BaseEventData eventData){ if(onSelect != null) onSelect(gameObject); } public override void OnUpdateSelected (BaseEventData eventData){ if(onUpdateSelect != null) onUpdateSelect(gameObject); } }
然后在你的界面里面写入监听按钮的代码。
using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems; using UnityEngine.Events; public class UIMain : MonoBehaviour { Button button; Image image; void Start () { button = transform.Find("Button").GetComponent<Button>(); image = transform.Find("Image").GetComponent<Image>(); EventTriggerListener.Get(button.gameObject).onClick =OnButtonClick; EventTriggerListener.Get(image.gameObject).onClick =OnButtonClick; } private void OnButtonClick(GameObject go){ //在这里监听按钮的点击事件 if(go == button.gameObject){ Debug.Log ("DoSomeThings"); } } }
虽然还有一些别的监听方式,但是我觉得这种方式是最科学的,大家可根据自己项目的需求继续拓展EventTriggerListener类。
转载自:雨松MOMO 2014年10月27日 于 雨松MOMO程序研究院 发表
相关文章推荐
- UGUI控件之按钮的监听事件系统
- UGUI的按钮监听事件系统
- UGUI研究院之控件以及按钮的监听事件系统
- UGUI 按钮的监听事件系统
- flashCs3 按钮事件没了?原来As3已经改为监听模式了!
- <Android>监听软键盘打开收起事件(软键盘自带收起按钮)
- android 监听按钮的点击事件 匿名内部类监听按钮点击事件
- 监听页面返回事件或者移动端返回按钮事件
- android Butt 按钮单击事件的监听
- android为按钮事件进行监听过程
- 在页面中监听用户控件的按钮点击事件
- Ext FormPanel监听键盘按钮事件
- android监听返回按钮事件
- iOS重写系统中的UINavigationController返回按钮的事件
- UGUI基于EventTrigger的UI交互事件监听系统&动态加载
- 定义按钮监听事件的方法汇总
- 给Android的音量按钮添加监听事件
- 按钮的监听事件及连接数据库的登录注册
- 很多人都不知道的监听微信、支付宝等移动app及浏览器的返回、后退、上一页按钮的事件方法