欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2022年01月27日 09:50:11   作者:Hu&Fei  
UGUI?可視化創(chuàng)建以及關(guān)聯(lián)事件很方便,?動(dòng)態(tài)創(chuàng)建可以利用創(chuàng)建好的?Prefab?進(jìn)行實(shí)例化,?只是在關(guān)聯(lián)事件上有些復(fù)雜,這篇文章主要介紹了Unity?UGUI?按鈕綁定事件的?4?種方式,需要的朋友可以參考下

UGUI 可視化創(chuàng)建以及關(guān)聯(lián)事件很方便, 動(dòng)態(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 : 通過(guò) Hierarchy 面板創(chuàng)建 UI > Button.

Step 2 : 創(chuàng)建一個(gè)腳本 TestClick.cs, 定義了一個(gè) 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. 通過(guò)直接綁定腳本來(lái)綁定事件

Step 1 : 通過(guò) Hierarchy 面板創(chuàng)建 UI > Button.

Step 2 : 創(chuàng)建一個(gè) ClickHandler.cs 腳本, 定義了一個(gè)私有方法 OnClick(), 并在 Start() 方法里為 Button 添加點(diǎn)擊事件的監(jiān)聽(tīng),作為參數(shù)傳入 OnClick 方法.

Step 3 : 將 ClickHandler 綁定在 Button 對(duì)象上.

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. 通過(guò) EventTrigger 實(shí)現(xiàn)按鈕點(diǎn)擊事件

UGUI 系統(tǒng)中 Button 默認(rèn)只提供了 OnClick 的調(diào)用方法, 有時(shí)候我們還需要監(jiān)聽(tīng)鼠標(biāo)進(jìn)入事件 (MouseIn) 和鼠標(biāo)滑出事件 (MouseOut). 就需要借助 UI 系統(tǒng)中的 EventTrigger 腳本來(lái)實(shí)現(xiàn).

Step 1 : 通過(guò) Hierarchy 面板創(chuàng)建 UI > Button.

Step 2 : 創(chuàng)建一個(gè) EventTriggerHandler.cs 腳本, 利用 UnityEngine.EventSystems.EventTrigger 添加監(jiān)聽(tīng)事件.

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. 通過(guò) MonoBehaviour 實(shí)現(xiàn)事件類接口來(lái)實(shí)現(xiàn)事件的監(jiān)聽(tīng)

Step 1 : 通過(guò) Hierarchy 面板創(chuàng)建 UI > Button.

Step 2 : 創(chuàng)建一個(gè) EventHandler.cs 腳本.

Step 3 : 將腳本綁定在 Button 對(duì)象上.

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)擊時(shí)是鼠標(biāo)的哪個(gè)按鍵, 上面的代碼中我們可以根據(jù) eventData.pointerId 來(lái)監(jiān)聽(tīng)是鼠標(biāo)左鍵還是右鍵. 但是每個(gè) UI 元素都創(chuàng)建一個(gè) MonoBehaviour 來(lái)監(jiān)聽(tīng)各個(gè)事件顯然不好, 下面是通過(guò)利用 Delegate 和 Event 來(lái)做一個(gè)通用類 UIEventListener 來(lái)處理事件 (觀察者模式).

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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論