Step 1 : 通过 Hierarchy 面板创建 UI > Button
.
Step 2 : 创建一个脚本 TestClick.cs, 定义了一个 Click 的 public 方法.
Step 3 : 选中 Hierarchy 中的 Button, Add Component
脚本 TestClick.cs
Step 4 : 在 Button(Script)
关联 TestClick 脚本里的 Click 方法.
Step 5 : Done.
TestClick.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TestClick : MonoBehaviour {
public void Click(){
Debug.Log ("Button Clicked. TestClick.");
}
}
Step 1 : 通过 Hierarchy 面板创建 UI > Button
.
Step 2 : 创建一个 ClickHandler.cs 脚本, 定义了一个私有方法 OnClick(), 并在 Start() 方法里为 Button 添加点击事件的监听,作为参数传入 OnClick 方法.
Step 3 : 将 ClickHandler 绑定在 Button 对象上.
Step 4 : Done.
ClickHandler.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ClickHandler : MonoBehaviour {
void Start () {
Button btn = this.GetComponent
UGUI 系统中 Button 默认只提供了 OnClick 的调用方法, 有时候我们还需要监听鼠标进入事件 (MouseIn) 和鼠标滑出事件 (MouseOut). 就需要借助 UI 系统中的 EventTrigger 脚本来实现.
Step 1 : 通过 Hierarchy 面板创建 UI > Button
.
Step 2 : 创建一个 EventTriggerHandler.cs 脚本, 利用 UnityEngine.EventSystems.EventTrigger 添加监听事件.
Step 3 : 绑定 EventTriggerHandler.cs 脚本到 Button 上.
Step 4 : Done.
EventTriggerHandler.cs
using UnityEngine;
using UnityEngine.EventSystems;
public class EventTriggerHandler: MonoBehaviour {
EventTrigger trigger;
void Start()
{
trigger= GetComponent();
EventTrigger.Entry ENTER = new EventTrigger.Entry();
EventTrigger.Entry EXIT = new EventTrigger.Entry();
EventTrigger.Entry CLICK = new EventTrigger.Entry();
EventTrigger.Entry DOWN = new EventTrigger.Entry();
EventTrigger.Entry UP = new EventTrigger.Entry();
ENTER.eventID = EventTriggerType.PointerEnter; //鼠标进入事件
EXIT.eventID = EventTriggerType.PointerExit; //鼠标滑出事件
CLICK.eventID = EventTriggerType.PointerClick; //鼠标点击事件
DOWN.eventID = EventTriggerType.PointerDown; //鼠标按下事件
UP.eventID = EventTriggerType.PointerUp; //鼠标抬起事件
ENTER.callback.AddListener(OnPointerEnter);
EXIT.callback.AddListener(OnPointerExit);
CLICK.callback.AddListener(OnPointerClick);
DOWN.callback.AddListener(OnPointerDown);
UP.callback.AddListener(OnPointerUp);
trigger.triggers.Add(ENTER);
trigger.triggers.Add(EXIT);
trigger.triggers.Add(CLICK);
trigger.triggers.Add(DOWN);
trigger.triggers.Add(UP);
}
public void OnPointerEnter(BaseEventData pointData)
{
print("PointerEnter");
}
public void OnPointerExit(BaseEventData pointData)
{
print("PointerExit");
}
public void OnPointerClick(BaseEventData pointData)
{
print("PointerClick");
}
public void OnPointerDown(BaseEventData pointData)
{
print("PointerDown");
}
public void OnPointerUp(BaseEventData pointData)
{
print("PointerUp");
}
}
Step 1 : 通过 Hierarchy 面板创建 UI > Button
.
Step 2 : 创建一个 EventHandler.cs 脚本.
Step 3 : 将脚本绑定在 Button 对象上.
Step 4 : Done.
EventHandler.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
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.");
} else 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..");
}
}
UGUI 如何判断 UI 元素被点击时是鼠标的哪个按键, 上面的代码中我们可以根据 eventData.pointerId 来监听是鼠标左键还是右键. 但是每个 UI 元素都创建一个 MonoBehaviour 来监听各个事件显然不好, 下面是通过利用 Delegate 和 Event 来做一个通用类 UIEventListener 来处理事件 (观察者模式).
UIEventListener.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class UIEventListener : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler {
// 定义事件代理
public delegate void UIEventProxy(GameObject gb);
// 鼠标点击事件
public event UIEventProxy OnClick;
// 鼠标进入事件
public event UIEventProxy OnMouseEnter;
// 鼠标滑出事件
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);
}
}
TestEvent.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TestEvent : MonoBehaviour {
void Start () {
Button btn = this.GetComponent
TestEvent 脚本绑定在 Button 上即可.
Project 结构