UGUI 按钮监听事件
2016-09-07 18:50
260 查看
博主之前一直使用的是NGUI3.6.8版本,其中我们注册按钮事件,除了把物体拖到按钮的UIButton脚本的OnClick列表中并选择物体上挂载脚本的方法之外,还有动态的去添加方法。这样便能够在动态生成的按钮上添加监听事件。一般挂载脚本的是按钮的事件确定且有多个按钮。
这几天博主在学习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;
然后这是博主测试的一个小脚本,需要挂载在按钮上,输出的是按下按钮的名称。
如果我们要动态添加事件呢?
为什么要使用匿名委托? 由于AddListener的参数是UnityAction,这个委托的声明如下:public delegate void UnityAction(); 所以我们之间传参的时候只能传void类型的方法。所以,当我们的监听事件需要传参的时候,可以使用匿名委托去实现。
暂时更新到这里,有什么发现博主会继续更新。
努力! 奋斗!
-----------------------------------------------------------------------------------
更新2016.9.13
补充一点,Unity本身允许事件函数可以带一个参数,参数类型为bool,float,int,string,Object 这5种。假若需要传递多个参数且这些参数都有关联的话,可以使用一个类来封装传递,例如GameObject。在Button组件里的OnClick列表里,也能拖拽GameObject物体到这个参数。
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物体到这个参数。
相关文章推荐
- UGUI 按钮的监听事件系统
- UGUI的按钮监听事件系统
- UGUI研究院之控件以及按钮的监听事件系统
- UGUI控件之按钮的监听事件系统
- android之选项控件(单选按钮)事件监听
- 按钮监听事件实现(三种方式实现)
- 学习Android按钮以及监听事件
- android开发中Button按钮监听事件(匿名内部类监听或者不匿名)
- android:动态创建多个按钮 并给每个按键添加监听事件
- 安卓(Android)动态创建多个按钮并添加监听事件
- j2se使用ActionListener实现界面上按钮点击的事件监听操作
- Unity中UGUI脚本添加Button按钮事件
- HTML5 浏览器返回按钮/手机返回按钮事件监听
- UGUI事件监听总结
- UGUI-仿照NGUI的事件监听机制
- Android开发20——单个监听器监听多个按钮点击事件
- android动态创建多个按钮并添加监听事件
- 安卓上按钮绑定监听事件的两种写法
- 监听tab点击事件,获取点击tab的第几个按钮
- Android攻城狮—监听(按钮)点击事件