UGUI研究院之控件以及按钮的监听事件系统

继续学习,我相信大家在做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差不多呢? 这里我给大家引一个思路,把下面代码放在你的项目里。

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 UnityEngine;
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类。

时间: 2024-10-07 11:01:52

UGUI研究院之控件以及按钮的监听事件系统的相关文章

UGUI 之 控件以及按钮的监听事件系统 存档

using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.EventSystems; using UnityEngine.Events; public class UIMain : MonoBehaviour {         Button        button;         Image image;         void Start ()         {     

(转载)VS2010/MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)

因为私人问题,鸡啄米暂停更新了几天,首先向关注鸡啄米动态的朋友说一声抱歉. 言归正传,鸡啄米上一节中讲了编辑框的用法,本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box)等.命令按钮就是我们前面多次提到的狭义的按钮控件,用来响应用户的鼠标单击操作,进行相应的处理,它可以显示文本也可以嵌入位图.单选按钮使用时,一般是多个组成一组,组中每个单选按钮的选中状态具有互斥关系,即同组的单选按钮只能有

背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

原文:背水一战 Windows 10 (31) - 控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton [源码下载] 作者:webabcd 介绍背水一战 Windows 10 之 控件(按钮类) ButtonBase Button HyperlinkButton RepeatButton ToggleButton AppBarButto

C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值

关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using System; using System.Windows.Forms; namespace Simon.WinForms.Examples.PropertyGrid { public class EditorControl : UserControl { public EditorControl() {

GTK常用控件之按钮( GtkButton )

按钮在GUI里应用最为广泛,我们几乎能在所有的界面中看到按钮. 空按钮的创建(按钮上没有任何内容): GtkWidget *gtk_button_new(void); 返回值:按钮指针 创建带文本内容的按钮: GtkWidget *gtk_button_new_with_label (const gchar *label); label:文本内容 返回值:按钮指针 设置按钮的文本内容: void gtk_button_set_label(GtkButton *button, const gcha

Silverlight项目笔记5:Oracle归档模式引起的异常&amp;&amp;表格控件绑定按钮

两个问题: (1)Oracle无法连接正常使用,原因是归档日志满了引起异常,最后选择删除归档日志恢复正常. (2)使用silverlight自带的表格绑定按钮竟然无法使用,通过变通绑定数据源集合,把按钮操作作为数据源集合一部分,重新绑定解决. 一.Oracle归档模式产生日志文件引起数据库异常 连接数据库失败,提示监听错误,各种检查监听配置文件,删除再添加监听,无果. sqlplus下重启数据库数据库依然无果,期间碰到多个错误提示: ORA-01034: ORACLE not available

控件(按钮类): ButtonBase, Button, HyperlinkButton, RepeatButton, ToggleButton, AppBarButton, AppBarToggleButton

介绍背水一战 Windows 10 之 控件(按钮类) ButtonBase Button HyperlinkButton RepeatButton ToggleButton AppBarButton AppBarToggleButton 示例1.ButtonBase(基类) 的示例Controls/ButtonControl/ButtonBaseDemo.xaml <Page x:Class="Windows10.Controls.ButtonControl.ButtonBaseDemo

VS2010-MFC(常用控件:按钮控件Button、Radio Button和Check Box)

转自:http://www.jizhuomi.com/software/182.html 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box)等.命令按钮就是我们前面多次提到的狭义的按钮控件,用来响应用户的鼠标单击操作,进行相应的处理,它可以显示文本也可以嵌入位图.单选按钮使用时,一般是多个组成一组,组中每个单选按钮的选中状态具有互斥关系,即同组的单选按钮只能有一个被选中. 命令按钮是我们最熟悉也是最常用的一种按钮控件,而单选按钮

Ionic控件之——按钮(Button)

Ionic提供丰富的按钮特性,足以满足大部分的按钮实现需求. HTML定义一个按钮: <button class="button"> 我是按钮 </button> 监听按钮的点击事件: 通常一个按钮被用户点击后,一定会触发一个功能,例如提交表单.确认选择.弹出提示等等,因此对按钮点击的监听,以及触发点击后要处理的事件逻辑是在Ionic开发中最常见的开发需求. 1.在html中为ng-click添加一个事件: <button class="butt