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

Babybus-u3d技术交流-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 objectIPointerExitHandler – OnPointerExit – Called when a pointer exits the objectIPointerDownHandler – OnPointerDown – Called when a pointer is pressed on the objectIPointerUpHandler – 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 objectIBeginDragHandler – OnBeginDrag – Called on the drag object when dragging is about to beginIDragHandler – OnDrag – Called on the drag object when a drag is happeningIEndDragHandler – OnEndDrag – Called on the drag object when a drag finishesIDropHandler – OnDrop – Called on the object where a drag finishesIScrollHandler – OnScroll – Called when a mouse wheel scrollsIUpdateSelectedHandler – OnUpdateSelected – Called on the selected object each tickISelectHandler – OnSelect – Called when the object becomes the selected objectIDeselectHandler – OnDeselect – Called on the selected object becomes deselectedIMoveHandler – OnMove – Called when a move event occurs (left, right, up, down, ect)ISubmitHandler – OnSubmit – Called when the submit button is pressedICancelHandler – OnCancel – Called when the cancel button is pressed
[http://docs.unity3d.com/460/Documentation/Manual/SupportedEvents.html

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

using UnityEngine;
using System.Collections;

using UnityEngine.EventSystems;
public class EventTriggerListener:
 UnityEngine.EventSystems.EventTrigger
{
public delegate void VoidDelegate(GameObjectgo);

 public Void DelegateonClick;
 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;
usingUnityEngine.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类。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,449评论 0 23
  • 一、EventSystem对象的说明 当我们在场景中创建任一UI对象后,Hierarchy面板中都可以看到系统自动...
    游戏开发小Y阅读 4,620评论 0 1
  • 昨日趁着休息,天气又晴好,便约了几位久未见面了的好友一起去老同学家掏番薯,老同学家住江南镇坞聪自然村,座落...
    五月的荷阅读 3,483评论 12 15
  • 一 Blockblock是一种数据类型,用来存储(封装)代码块,跟函数很相似,官方非常推荐使用Block,效率高...
    037e3257fa3b阅读 825评论 0 0
  • 文/不二姐姐 最近,我的闺蜜小Z突然之间就失恋了,“突然之间”那是因为她与男友低调的几个月恋情就这样没有任何征兆的...
    不二姐姐阅读 4,005评论 0 2