unity4.6 ui

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

More

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

Unity4.6新的UI的又称UGUI,个人感觉跟NGUI和DFGUI(Daikon Forge GUI)有很多相似之处,可视化创建以及关联事件确实很方便,动态创建可以利用创建好的prefab进行实例化,只是在关联事件上有些复杂,现在我们来学习如何来给按钮绑定事件,下面列举三种事件关联方式。

一、可视化创建及事件绑定

第一步:通过Hierarchy面板创建button,如图

ui-demo-1

More