UGUI事件

来源:互联网 发布:蜂窝数据打开自动关闭 编辑:程序博客网 时间:2024/05/22 07:47
在使用UGUI过程中,有时我们想长按钮按,执行某个功能,但UGUI只有简单的单击功能,下面我们来看下UGUI长按事件的实现,贴上代码
using UnityEngine;
using System.Collections;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[AddComponentMenu(“UI/LongPressButton”)] public class UILongPressButton : Selectable, IPointerDownHandler,IPointerExitHandler,IPointerUpHandler
{
[SerializeField] UnityEvent m_onLongPress = new UnityEvent();


float interval = 0.1f;
float longPressDelay = 0.5f;


private bool isTouchDown = false;
private bool isLongpress = false;
private float touchBegin = 0;
private float lastInvokeTime = 0;


// Update is called once per frame
void Update ()
{
if (isTouchDown && IsPressed() && interactable)
{
if (isLongpress)
{
if (Time.time – lastInvokeTime > interval)
{
m_onLongPress.Invoke();
lastInvokeTime = Time.time;
}
}
else
{
isLongpress = Time.time – touchBegin > longPressDelay;
}
}
}


public void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
touchBegin = Time.time;
isTouchDown = true;
}


public void OnPointerExit(PointerEventData eventData)
{
base.OnPointerExit(eventData);
isTouchDown = false;
isLongpress = false;
}


public void OnPointerUp(PointerEventData eventData)
{
base.OnPointerUp(eventData);
isTouchDown = false;
isLongpress = false;
}

}



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.


阅读这段Unity3d的官方文档我们会发现Unity4.6 UI,有一种更简单的方式来监听Button按钮的点击,MouseIn鼠标滑入,MouseOut鼠标滑出等事件,那就是我们我们可以通过实现各个事件的接口类来自定义事件。


三、通过MonoBehaviour 来实现事件类接口来实现事件的监听
第一步:通过Hierarchy面板创建button(详细参考Unity4.6 UI按钮绑定事件(一))


第二步:创建一个名为EventHandler的脚本,代码如下

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class EventHandler : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IDragHandler
{
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.pointerId == -1)
Debug.Log("Left Mouse Clicked");
if (eventData.pointerId == -2)
Debug.Log("Right Mouse Clicked");
}
 
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Pointer Enter");
}
 
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("Pointer Exit");
}
 
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Pointer Down");
}
 
public void OnDrag(PointerEventData eventData)
{
Debug.Log("Dragged");
}
}


第三步:将脚本绑定到Button对象上(详细参考Unity4.6 UI按钮绑定事件(三)中图片介绍)


然后运行,我们就能看到各个事件被实现了


Unity4.6 UI(UGUI)如何判断UI元素被点击时是鼠标哪个按键,上面代码中我们可以根据eventData.pointerId来监听是我们按下的是鼠标左键还是右键。


通过前面几部分学习我们已经实现对Unity4.6 UI新的UI系统如何绑定事件做了大概讲解,但是弊端明显,就是每个UI元素都创建一个MonoBehavior来进行监听各个事件,显然这样做不行,下面我们来学习下利用Delegate和Event来做一个通用类UIEventListener来处理事件(不了解Delegate和Event的童鞋请自行谷歌搜索观察者模式),好了不废话了,下面贴上代码

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class UIEventListener : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
 
/// <summary>
/// 定义事件代理
/// </summary>
/// <param name="gb"></param>
public delegate void UIEventProxy(GameObject gb);
 
/// <summary>
/// 鼠标点击事件
/// </summary>
public event UIEventProxy OnClick;
 
/// <summary>
/// 鼠标进入事件
/// </summary>
public event UIEventProxy OnMouseEnter;
 
/// <summary>
/// 鼠标滑出事件
/// </summary>
public event UIEventProxy OnMouseExit;
 
public void OnPointerClick(PointerEventData eventData)
{
if (OnClick != null)
OnClick(this.gameObject);
}
 
public void OnPointerEnter(PointerEventData eventData)
{
if (OnMouseEnter != null)
OnMouseEnter(this.gameObject);
}
 
public void OnPointerExit(PointerEventData eventData)
{
if (OnMouseExit != null)
OnMouseExit(this.gameObject);
}
}
 


下面我们来看下调用的代码:


using UnityEngine;
using System.Collections;
using UnityEngine.UI;
 
public class Test : MonoBehaviour
{
 
// Use this for initialization
void Start()
{
Button btn = this.GetComponent<Button>();
UIEventListener btnListener = btn.gameObject.AddComponent<UIEventListener>();
btnListener.OnClick += delegate(GameObject gb)
{
Debug.Log(gb.name);
};
btnListener.OnMouseEnter += delegate(GameObject gb)
{
Debug.Log(gb.name);
};
btnListener.OnMouseExit += delegate(GameObject gb)
{
Debug.Log(gb.name);
};
}
}

0 0