Unity4.6 UI按钮绑定事件(四)

 

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);
};
}
}

关于Unity New UI中的按钮绑定事件的介绍就到此结束,欢迎您的围观,不足之处,敬请留言。