首页 > Unity3D频道 > 【UGUI研究院】 > UGUI研究院之控件以及按钮的监听事件系统(五)
2014
10-27

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

http://docs.unity3d.com/460/Documentation/Manual/SupportedEvents.html

OK 怎么样才能让UGUI监听的方式和NGUI差不多呢? 这里我给大家引一个思路,把下面代码放在你的项目里。

然后在你的界面里面写入监听按钮的代码。

虽然还有一些别的监听方式,但是我觉得这种方式是最科学的,大家可根据自己项目的需求继续拓展EventTriggerListener类。

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

UGUI研究院之控件以及按钮的监听事件系统(五)》有 84 条评论

  1. Brand 说:

    这个在lua中怎么用啊。我在lua中.onClick出不来报onClick是nil

  2. 毛毛 说:

    UGUI里使用点击的接口事件只能在PC端使用么 ? 例如:继承了IPointerClickHandler这个接口 实现双击打印log日志,PC端可以打印 到了Android端就不行了

  3. 说:

    interactable感觉无效了,有解没

  4. ____欧阳氏 说:

    大大 我想问ugui在限制区域怎么响应触摸事件或者鼠标点击事件 我用滑动scroll的时候也会触发3d模型物体的移动事件(3d模型是根据鼠标的移动旋转) 怎么解决 求个思路

    • ____欧阳氏 说:

      momo大大 我在做角色展示界面的时候 最底层是3d背景 其次3d模型(透视相机) 然后是uiRoot层(里面有个ui摄像机正交的),最外层是按钮层(也是在uiRoot里面的iamge背景)我也在想点击角色周围使角色旋转 我是应该把事件组件放在最外层 还是做穿透放在3d背景层 (穿透又应该大概怎么做)

  5. 陆宇杨 说:

    btn.onClick.AddListener(delegate() {this.OnClick(btnObj);});}

  6. 怀念...o^蔠 说:

    momo你好,我想问问,可不可以用射线检测来触发ugui button里面的高亮动画呀?是在on select()里面写吗?

  7. 东大鹏 说:

    MOMO你好,uGui 有没有类似于 nGui Allow Multi Touch 的设置,现在我可以同时点多个按钮,然后就乱了,谢谢。

  8. 雨松大大这个能使GameObject物体具有点击功能吗?我试的只有UGUI的组件才可以呢?

  9. FG911 说:

    我想问一下如何让一个image不遮挡事件,rawimage虽然可以但是不支持slice

  10. 雨松MOMO 说:

    我觉得最好不要用 = 。。 如果 =多了 万一忘-= 可能就出问题了。

  11. C#里面给代理赋值不是应该用 =么?

  12. C#里面给代理赋值不是应该用+=么?

  13. TXT 说:

    继承EventTrigger和一些UGUI的控件有冲突 为什么不继承接口呢

  14. 记忆 说:

    eventtrigger 是多点触控啊,怎么实现单点触控的,求教大神

  15. 雪焰 说:

    貌似有更简单的办法:把这个代码添加给需要响应事件的UI就行using UnityEngine;using System.Collections;using UnityEngine.EventSystems;public class EventOrder : MonoBehaviour,IPointerClickHandler {#region IPointerClickHandler implementationpublic void OnPointerClick (PointerEventData eventData){//throw new System.NotImplementedException ();Debug.Log(name);Debug.Log(eventData.pointerCurrentRaycast.gameObject);}#endregion// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}}

  16. 雪焰 说:

    貌似有更简单的办法:把这个代码添加给需要响应事件的UI就行using UnityEngine;using System.Collections;using UnityEngine.EventSystems;public class EventOrder : MonoBehaviour,IPointerClickHandler { #region IPointerClickHandler implementation public void OnPointerClick (PointerEventData eventData) { //throw new System.NotImplementedException (); Debug.Log(name); Debug.Log(eventData.pointerCurrentRaycast.gameObject); } #endregion // Use this for initialization void Start () { } // Update is called once per frame void Update () { }}

留下一个回复

你的email不会被公开。