From 698300d465a311c939e17f8b40fa1db3ab7603eb Mon Sep 17 00:00:00 2001 From: cooney Date: Sun, 25 Jan 2026 23:45:33 +0900 Subject: [PATCH] =?UTF-8?q?UI=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/UI/UI_Base.cs | 18 ++++++++++++++++++ Assets/Scripts/UI/UI_Button.cs | 12 +++++++----- Assets/Scripts/UI/UI_EventHandler.cs | 12 ++++++------ Assets/Scripts/Utils/Define.cs | 5 +++++ Assets/Scripts/Utils/Extension.cs | 12 ++++++++++++ Assets/Scripts/Utils/Extension.cs.meta | 2 ++ Assets/Scripts/Utils/Util.cs | 11 +++++++++++ 7 files changed, 61 insertions(+), 11 deletions(-) create mode 100644 Assets/Scripts/Utils/Extension.cs create mode 100644 Assets/Scripts/Utils/Extension.cs.meta diff --git a/Assets/Scripts/UI/UI_Base.cs b/Assets/Scripts/UI/UI_Base.cs index ed51d90..6efa8e7 100644 --- a/Assets/Scripts/UI/UI_Base.cs +++ b/Assets/Scripts/UI/UI_Base.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using TMPro; using UnityEngine; +using UnityEngine.EventSystems; using UnityEngine.UI; public class UI_Base : MonoBehaviour @@ -59,4 +60,21 @@ public class UI_Base : MonoBehaviour { return Get(idx); } + + public static void AddUIEvent(GameObject go, Action action, Define.UIEvent type = Define.UIEvent.Click) + { + UI_EventHandler evt = Util.GetOrAddComponent(go); + + switch (type) + { + case Define.UIEvent.Click: + evt.OnClickHandler -= action; + evt.OnClickHandler += action; + break; + case Define.UIEvent.Drag: + evt.OnDragHandler -= action; + evt.OnDragHandler += action; + break; + } + } } diff --git a/Assets/Scripts/UI/UI_Button.cs b/Assets/Scripts/UI/UI_Button.cs index b181ca0..5df7670 100644 --- a/Assets/Scripts/UI/UI_Button.cs +++ b/Assets/Scripts/UI/UI_Button.cs @@ -39,19 +39,21 @@ public class UI_Button : UI_Base // Get 호출 시에도 타입을 맞춰줍니다. // Get((int)Texts.ScoreText).text = "Bind Test"; - GetText((int)Texts.ScoreText).text = "Bind Test"; - + + GetButton((int)Buttons.PointButton).gameObject.AddUIEvent(OnButtonClicked); + GameObject go = GetImage((int)Images.ItemIcon).gameObject; - UI_EventHandler evt = go.GetComponent(); - evt.OnDragHandler += ((PointerEventData data) => { evt.gameObject.transform.position = data.position; }); + AddUIEvent(go, (PointerEventData data) => { go.transform.position = data.position; }, Define.UIEvent.Drag); } private int _score = 0; - public void OnButtonClicked() + public void OnButtonClicked(PointerEventData data) { // GetComponentInChildren().text = "텍스트 변경 완료!"; _score += 1; + + GetText((int)Texts.ScoreText).text = $"점수 : {_score}점"; } } diff --git a/Assets/Scripts/UI/UI_EventHandler.cs b/Assets/Scripts/UI/UI_EventHandler.cs index e206093..2ca99a6 100644 --- a/Assets/Scripts/UI/UI_EventHandler.cs +++ b/Assets/Scripts/UI/UI_EventHandler.cs @@ -3,16 +3,16 @@ using UnityEngine; using UnityEngine.EventSystems; -public class UI_EventHandler : MonoBehaviour, IBeginDragHandler, IDragHandler +public class UI_EventHandler : MonoBehaviour, IPointerClickHandler, IDragHandler { - public Action OnBeginDragHandler = null; + public Action OnClickHandler = null; public Action OnDragHandler = null; - - public void OnBeginDrag(PointerEventData eventData) + + public void OnPointerClick(PointerEventData eventData) { - if (OnBeginDragHandler != null) + if (OnClickHandler != null) { - OnBeginDragHandler.Invoke(eventData); + OnClickHandler.Invoke(eventData); } } diff --git a/Assets/Scripts/Utils/Define.cs b/Assets/Scripts/Utils/Define.cs index f9db2aa..49798cc 100644 --- a/Assets/Scripts/Utils/Define.cs +++ b/Assets/Scripts/Utils/Define.cs @@ -2,6 +2,11 @@ using UnityEngine; public class Define { + public enum UIEvent + { + Click, + Drag, + } public enum MouseEvent { Press, diff --git a/Assets/Scripts/Utils/Extension.cs b/Assets/Scripts/Utils/Extension.cs new file mode 100644 index 0000000..836151a --- /dev/null +++ b/Assets/Scripts/Utils/Extension.cs @@ -0,0 +1,12 @@ +using System; +using UnityEngine; +using UnityEngine.EventSystems; + +public static class Extension +{ + public static void AddUIEvent(this GameObject go, Action action, + Define.UIEvent type = Define.UIEvent.Click) + { + UI_Base.AddUIEvent(go, action, type); + } +} diff --git a/Assets/Scripts/Utils/Extension.cs.meta b/Assets/Scripts/Utils/Extension.cs.meta new file mode 100644 index 0000000..d7a27fa --- /dev/null +++ b/Assets/Scripts/Utils/Extension.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 442b91d9fdae87b43b7b0c1c47026c65 \ No newline at end of file diff --git a/Assets/Scripts/Utils/Util.cs b/Assets/Scripts/Utils/Util.cs index 312b487..93e75fb 100644 --- a/Assets/Scripts/Utils/Util.cs +++ b/Assets/Scripts/Utils/Util.cs @@ -2,6 +2,17 @@ using UnityEngine; public class Util { + public static T GetOrAddComponent(GameObject go) where T : UnityEngine.Component + { + T component = go.GetComponent(); + + if (component == null) + { + component = go.AddComponent(); + } + return component; + } + public static GameObject FindChild(GameObject go, string name = null, bool recursive = false) { Transform transform = FindChild(go, name, recursive);