Unity Button綁定事件的方法


一、可視化創建及事件綁定

先寫一段 Button 的響應代碼:

 1 using UnityEngine;
 2 using UnityEngine.UI;
 3 
 4 public class ButtonTest : MonoBehaviour
 5 {
 6     public Text m_Text;
 7 
 8     public void OnButtonClickEvent()
 9     {
10         m_Text.text = "按鈕點擊";
11     }
12 }

點擊 Button 組件上的 OnClick 的+號,將綁定腳本的對象賦值到這個 Button 組件上

選擇並綁定 Button 的點擊事件

Button 的點擊效果:

二、通過監聽按鈕綁定事件

使用 Button 組件自帶的 onClick.AddListener 方法:

 1 using UnityEngine;
 2 using UnityEngine.UI;
 3 
 4 public class ButtonTest : MonoBehaviour
 5 {
 6     public Button m_Button;
 7     public Text m_Text;
 8 
 9     void Start()
10     {
11         m_Button.onClick.AddListener(OnButtonClickEvent);
12     }
13 
14     public void OnButtonClickEvent()
15     {
16         m_Text.text = "按鈕點擊";
17     }
18 }

Button 的點擊效果:

三、通過射線監聽點擊的物體來綁定事件

 1 using System.Collections.Generic;
 2 using UnityEngine;
 3 using UnityEngine.EventSystems;
 4 using UnityEngine.UI;
 5 
 6 public class ButtonTest : MonoBehaviour
 7 {
 8     public Text m_Text;
 9 
10     void Update()
11     {
12         if (Input.GetMouseButtonDown(0))
13         {
14             if (OnePointColliderObject() != null)
15             {
16                 if (OnePointColliderObject().name == "Button" || OnePointColliderObject().name == "Text")
17                 {
18                     ButtonOnClickEvent();
19                 }
20             }
21         }
22     }
23 
24     // 點擊對象獲取到對象的名字
25     public GameObject OnePointColliderObject()
26     {
27         // 存有鼠標或者觸摸數據的對象
28         PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
29         // 當前指針位置
30         eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
31         // 射線命中之后的反饋數據
32         List<RaycastResult> results = new List<RaycastResult>();
33         // 投射一條光線並返回所有碰撞
34         EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
35         // 返回點擊到的物體
36         if (results.Count > 0)
37             return results[0].gameObject;
38         else
39             return null;
40     }
41 
42     public void ButtonOnClickEvent()
43     {
44         m_Text.text = "按鈕點擊";
45     }
46 }

Button 的點擊效果:

四、通過EventTrigger實現點擊事件

在 Button 上添加 EventTrigger 組件

編輯代碼:

 1 using UnityEngine;
 2 using UnityEngine.EventSystems;
 3 using UnityEngine.UI;
 4 
 5 [RequireComponent(typeof(EventTrigger))]
 6 public class ButtonTest : MonoBehaviour
 7 {
 8     public Text m_Text;
 9 
10     void Start()
11     {
12         Button btn = transform.GetComponent<Button>();
13         EventTrigger trigger = btn.gameObject.GetComponent<EventTrigger>();
14         EventTrigger.Entry entry = new EventTrigger.Entry
15         {
16             // 鼠標點擊事件
17             eventID = EventTriggerType.PointerClick,
18 
19             callback = new EventTrigger.TriggerEvent()
20         };
21         entry.callback.AddListener(ButtonOnClickEvent);
22         trigger.triggers.Add(entry);
23     }
24 
25     public void ButtonOnClickEvent(BaseEventData pointData)
26     {
27         m_Text.text = "按鈕點擊";
28     }
29 }

Button 的點擊效果:

五、通過通用類UIEventListener處理響應事件

通用類 UIEventListener 腳本:

 1 using UnityEngine;
 2 using UnityEngine.EventSystems;
 3 
 4 public class UIEventListener : MonoBehaviour, IPointerClickHandler
 5 {
 6     // 定義事件代理
 7     public delegate void UIEventProxy();
 8     // 鼠標點擊事件
 9     public event UIEventProxy OnClick;
10 
11     public void OnPointerClick(PointerEventData eventData)
12     {
13         if (OnClick != null)
14             OnClick();
15     }
16 }

Button 腳本:

 1 using UnityEngine;
 2 using UnityEngine.EventSystems;
 3 using UnityEngine.UI;
 4 
 5 [RequireComponent(typeof(EventTrigger))]
 6 public class ButtonTest : MonoBehaviour
 7 {
 8     public Text m_Text;
 9 
10     void Start()
11     {
12         Button btn = this.GetComponent<Button>();
13         UIEventListener btnListener = btn.gameObject.AddComponent<UIEventListener>();
14 
15         btnListener.OnClick += delegate () {
16             ButtonOnClickEvent();
17         };
18     }
19 
20     public void ButtonOnClickEvent()
21     {
22         m_Text.text = "按鈕點擊";
23     }
24 }

Button 的點擊效果:

 

 

 

 

 

*** |  以上內容僅為學習參考、學習筆記使用  | ***


免責聲明!

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



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