Unity UGUI 按鈕綁定事件的 4 種方式


UGUI 可視化創建以及關聯事件很方便, 動態創建可以利用創建好的 Prefab 進行實例化, 只是在關聯事件上有些復雜, 本文總結了幾種給按鈕綁定事件的關聯方式.

1. 可視化創建及事件綁定 #

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

2. 通過直接綁定腳本來綁定事件 #

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<Button> ();
		btn.onClick.AddListener (OnClick);
	}

	private void OnClick(){
		Debug.Log ("Button Clicked. ClickHandler.");
	}

}

3. 通過 EventTrigger 實現按鈕點擊事件 #

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 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 ();
		// 鼠標點擊事件
		entry.eventID = EventTriggerType.PointerClick;
		// 鼠標進入事件 entry.eventID = EventTriggerType.PointerEnter;
		// 鼠標滑出事件 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 實現事件類接口來實現事件的監聽 #

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<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 結構

代碼 : Here

End.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM