UGUI中鼠标点击进入等事件的监听
Posted 萌新小L
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UGUI中鼠标点击进入等事件的监听相关的知识,希望对你有一定的参考价值。
继承于EventTrigger,然后在类内定义了一个委托,这个委托表示点击哪个物体。
在调用Get()方法时,就已经把EventTriggerListener脚本挂在对应找到的物体上了。所以注册进去的事件都注册到对应功能上了
这里可以把对应的点击事件进行分离。
来源:http://www.xuanyusong.com/archives/3325
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");
}
}
}
以上是关于UGUI中鼠标点击进入等事件的监听的主要内容,如果未能解决你的问题,请参考以下文章
js常用对象:点击双击onload事件鼠标相关onblur事件和onfocus事件等;通过循环给多个元素添加事件通过addEventListener() 方法 监听事件函数