继续学习,我相信大家在做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差不多呢? 这里我给大家引一个思路,把下面代码放在你的项目里。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
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); } } |
然后在你的界面里面写入监听按钮的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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类。
- 本文固定链接: https://www.xuanyusong.com/archives/3325
- 转载请注明: 雨松MOMO 于 雨松MOMO程序研究院 发表
这个在lua中怎么用啊。我在lua中.onClick出不来报onClick是nil
UGUI里使用点击的接口事件只能在PC端使用么 ? 例如:继承了IPointerClickHandler这个接口 实现双击打印log日志,PC端可以打印 到了Android端就不行了
这个接口 pc和移动端都可以支持
interactable感觉无效了,有解没
大大 我想问ugui在限制区域怎么响应触摸事件或者鼠标点击事件 我用滑动scroll的时候也会触发3d模型物体的移动事件(3d模型是根据鼠标的移动旋转) 怎么解决 求个思路
momo大大 我在做角色展示界面的时候 最底层是3d背景 其次3d模型(透视相机) 然后是uiRoot层(里面有个ui摄像机正交的),最外层是按钮层(也是在uiRoot里面的iamge背景)我也在想点击角色周围使角色旋转 我是应该把事件组件放在最外层 还是做穿透放在3d背景层 (穿透又应该大概怎么做)
btn.onClick.AddListener(delegate() {this.OnClick(btnObj);});}
momo你好,我想问问,可不可以用射线检测来触发ugui button里面的高亮动画呀?是在on select()里面写吗?
自己封装一个button类吧。。 方便拓展
MOMO你好,uGui 有没有类似于 nGui Allow Multi Touch 的设置,现在我可以同时点多个按钮,然后就乱了,谢谢。
雨松大大这个能使GameObject物体具有点击功能吗?我试的只有UGUI的组件才可以呢?
我想问一下如何让一个image不遮挡事件,rawimage虽然可以但是不支持slice
添加一个 canvas group组件。 然后把 blocksRaycasts 关了
谢谢!
我觉得最好不要用 = 。。 如果 =多了 万一忘-= 可能就出问题了。
C#里面给代理赋值不是应该用 =么?
C#里面给代理赋值不是应该用+=么?
我觉得最好不要用+= 。。 如果+=多了 万一忘-= 可能就出问题了。
好在这里的语法要求并不严格。在VS中这么写是过不了语法检测的错误。
继承EventTrigger和一些UGUI的控件有冲突 为什么不继承接口呢
啊。我这里没发现冲突啊。。
eventtrigger 是多点触控啊,怎么实现单点触控的,求教大神
貌似有更简单的办法:把这个代码添加给需要响应事件的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 () {}}
貌似有更简单的办法:把这个代码添加给需要响应事件的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 () { }}
IPointerClickHandler 就是继承EventTrigger的。。 这样可以,但是不好用,因为界面要单独自己写 IPointerClickHandler接口。。我觉得同意方法比较好。。NGUI之前的思路。。
EventTrigger继承IPointClickHander的吧。。。