Unity?UGUI?按鈕綁定事件的?4?種方式匯總
UGUI 可視化創(chuàng)建以及關(guān)聯(lián)事件很方便, 動態(tài)創(chuàng)建可以利用創(chuàng)建好的 Prefab 進(jìn)行實(shí)例化, 只是在關(guān)聯(lián)事件上有些復(fù)雜, 本文總結(jié)了幾種給按鈕綁定事件的關(guān)聯(lián)方式.
1. 可視化創(chuàng)建及事件綁定
Step 1 : 通過 Hierarchy 面板創(chuàng)建 UI > Button.

Step 2 : 創(chuàng)建一個腳本 TestClick.cs, 定義了一個 Click 的 public 方法.
Step 3 : 選中 Hierarchy 中的 Button, Add Component 腳本 TestClick.cs
Step 4 : 在 Button(Script) 關(guān)聯(lián) 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.");
}
}2. 通過直接綁定腳本來綁定事件
Step 1 : 通過 Hierarchy 面板創(chuàng)建 UI > Button.
Step 2 : 創(chuàng)建一個 ClickHandler.cs 腳本, 定義了一個私有方法 OnClick(), 并在 Start() 方法里為 Button 添加點(diǎn)擊事件的監(jiān)聽,作為參數(shù)傳入 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<Button> ();
btn.onClick.AddListener (OnClick);
}
private void OnClick(){
Debug.Log ("Button Clicked. ClickHandler.");
}3. 通過 EventTrigger 實(shí)現(xiàn)按鈕點(diǎn)擊事件
UGUI 系統(tǒng)中 Button 默認(rèn)只提供了 OnClick 的調(diào)用方法, 有時候我們還需要監(jiān)聽鼠標(biāo)進(jìn)入事件 (MouseIn) 和鼠標(biāo)滑出事件 (MouseOut). 就需要借助 UI 系統(tǒng)中的 EventTrigger 腳本來實(shí)現(xiàn).
Step 1 : 通過 Hierarchy 面板創(chuàng)建 UI > Button.
Step 2 : 創(chuàng)建一個 EventTriggerHandler.cs 腳本, 利用 UnityEngine.EventSystems.EventTrigger 添加監(jiān)聽事件.
Step 3 : 綁定 EventTriggerHandler.cs 腳本到 Button 上.

Step 4 : Done.
EventTriggerHandler.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
// 需要 EventTrigger 腳本的支援
[RequireComponent(typeof(UnityEngine.EventSystems.EventTrigger))]
public class EventTriggerHandler : MonoBehaviour {
// Use this for initialization
void Start () {
Button btn = this.GetComponent<Button> ();
EventTrigger trigger = btn.gameObject.GetComponent<EventTrigger> ();
EventTrigger.Entry entry = new EventTrigger.Entry ();
// 鼠標(biāo)點(diǎn)擊事件
entry.eventID = EventTriggerType.PointerClick;
// 鼠標(biāo)進(jìn)入事件 entry.eventID = EventTriggerType.PointerEnter;
// 鼠標(biāo)滑出事件 entry.eventID = EventTriggerType.PointerExit;
entry.callback = new EventTrigger.TriggerEvent ();
entry.callback.AddListener (OnClick);
// entry.callback.AddListener (OnMouseEnter);
trigger.triggers.Add (entry);
}
private void OnClick(BaseEventData pointData){
Debug.Log ("Button Clicked. EventTrigger..");
private void OnMouseEnter(BaseEventData pointData){
Debug.Log ("Button Enter. EventTrigger..");
}4. 通過 MonoBehaviour 實(shí)現(xiàn)事件類接口來實(shí)現(xiàn)事件的監(jiān)聽
Step 1 : 通過 Hierarchy 面板創(chuàng)建 UI > Button.
Step 2 : 創(chuàng)建一個 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 元素被點(diǎn)擊時是鼠標(biāo)的哪個按鍵, 上面的代碼中我們可以根據(jù) eventData.pointerId 來監(jiān)聽是鼠標(biāo)左鍵還是右鍵. 但是每個 UI 元素都創(chuàng)建一個 MonoBehaviour 來監(jiān)聽各個事件顯然不好, 下面是通過利用 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);
// 鼠標(biāo)點(diǎn)擊事件
public event UIEventProxy OnClick;
// 鼠標(biāo)進(jìn)入事件
public event UIEventProxy OnMouseEnter;
// 鼠標(biāo)滑出事件
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<Button> ();
UIEventListener btnListener = btn.gameObject.AddComponent<UIEventListener> ();
btnListener.OnClick += delegate(GameObject gb) {
Debug.Log(gb.name + " OnClick");
};
btnListener.OnMouseEnter += delegate(GameObject gb) {
Debug.Log(gb.name + " OnMouseEnter");
btnListener.OnMouseExit += delegate(GameObject gb) {
Debug.Log(gb.name + " OnMOuseExit");
}
}TestEvent 腳本綁定在 Button 上即可.

Project 結(jié)構(gòu)

代碼 : Here
到此這篇關(guān)于Unity UGUI 按鈕綁定事件的 4 種方式的文章就介紹到這了,更多相關(guān)Unity UGUI 按鈕綁定事件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#預(yù)處理指令之#line,#pragma warning 詳細(xì)解析
#line 指令可能由生成過程中的自動中間步驟使用。例如,如果行從原始的源代碼文件中移除,但是您仍希望編譯器基于文件中的原始行號生成輸出,則可以移除行,然后用 #line 模擬原始行號2014-01-01
C#WinForm實(shí)現(xiàn)多語言切換的示例
本文主要介紹了C#WinForm實(shí)現(xiàn)多語言切換的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
c#?Task.Wait()與awaiat?Task異常處理的區(qū)別說明
這篇文章主要介紹了c#?Task.Wait()與awaiat?Task異常處理的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06

