網(wǎng)站首頁(yè) 編程語言 正文
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 : 通過 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. 通過直接綁定腳本來綁定事件
Step 1 : 通過 Hierarchy 面板創(chuàng)建 UI > Button
.
Step 2 : 創(chuàng)建一個(gè) ClickHandler.cs 腳本, 定義了一個(gè)私有方法 OnClick(), 并在 Start() 方法里為 Button 添加點(diǎn)擊事件的監(jiān)聽,作為參數(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. 通過 EventTrigger 實(shí)現(xiàn)按鈕點(diǎn)擊事件
UGUI 系統(tǒng)中 Button 默認(rèn)只提供了 OnClick 的調(diào)用方法, 有時(shí)候我們還需要監(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)建一個(gè) 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)建一個(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 來監(jiān)聽是鼠標(biāo)左鍵還是右鍵. 但是每個(gè) UI 元素都創(chuàng)建一個(gè) MonoBehaviour 來監(jiān)聽各個(gè)事件顯然不好, 下面是通過利用 Delegate 和 Event 來做一個(gè)通用類 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
原文鏈接:https://www.cnblogs.com/isayes/p/6370168.html#5008315
相關(guān)推薦
- 2022-03-19 CentOS7下安裝MongoDB數(shù)據(jù)庫(kù)過程_MongoDB
- 2022-07-18 python中數(shù)組array和列表list的基本用法及區(qū)別解析_python
- 2024-03-22 【IDEA】maven項(xiàng)目刷新依賴的兩種方式
- 2022-04-06 C++的友元和內(nèi)部類你了解嗎_C 語言
- 2022-10-15 Qt鍵盤事件實(shí)現(xiàn)圖片在窗口上下左右移動(dòng)_C 語言
- 2022-03-14 文件上傳錯(cuò)誤the request doesn't contain a multipart/form
- 2022-05-08 react實(shí)現(xiàn)簡(jiǎn)單的拖拽功能_React
- 2022-11-09 SQL語句過濾條件放在on與where子句中的區(qū)別和聯(lián)系淺析_MsSql
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支