UI 이벤트 추가

This commit is contained in:
2026-01-25 23:45:33 +09:00
parent 8b013830a0
commit 698300d465
7 changed files with 61 additions and 11 deletions

View File

@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI; using UnityEngine.UI;
public class UI_Base : MonoBehaviour public class UI_Base : MonoBehaviour
@@ -59,4 +60,21 @@ public class UI_Base : MonoBehaviour
{ {
return Get<Image>(idx); return Get<Image>(idx);
} }
public static void AddUIEvent(GameObject go, Action<PointerEventData> action, Define.UIEvent type = Define.UIEvent.Click)
{
UI_EventHandler evt = Util.GetOrAddComponent<UI_EventHandler>(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;
}
}
} }

View File

@@ -39,19 +39,21 @@ public class UI_Button : UI_Base
// Get 호출 시에도 타입을 맞춰줍니다. // Get 호출 시에도 타입을 맞춰줍니다.
// Get<TextMeshProUGUI>((int)Texts.ScoreText).text = "Bind Test"; // Get<TextMeshProUGUI>((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; GameObject go = GetImage((int)Images.ItemIcon).gameObject;
UI_EventHandler evt = go.GetComponent<UI_EventHandler>(); AddUIEvent(go, (PointerEventData data) => { go.transform.position = data.position; }, Define.UIEvent.Drag);
evt.OnDragHandler += ((PointerEventData data) => { evt.gameObject.transform.position = data.position; });
} }
private int _score = 0; private int _score = 0;
public void OnButtonClicked() public void OnButtonClicked(PointerEventData data)
{ {
// GetComponentInChildren<TextMeshProUGUI>().text = "텍스트 변경 완료!"; // GetComponentInChildren<TextMeshProUGUI>().text = "텍스트 변경 완료!";
_score += 1; _score += 1;
GetText((int)Texts.ScoreText).text = $"점수 : {_score}점";
} }
} }

View File

@@ -3,16 +3,16 @@ using UnityEngine;
using UnityEngine.EventSystems; using UnityEngine.EventSystems;
public class UI_EventHandler : MonoBehaviour, IBeginDragHandler, IDragHandler public class UI_EventHandler : MonoBehaviour, IPointerClickHandler, IDragHandler
{ {
public Action<PointerEventData> OnBeginDragHandler = null; public Action<PointerEventData> OnClickHandler = null;
public Action<PointerEventData> OnDragHandler = null; public Action<PointerEventData> OnDragHandler = null;
public void OnBeginDrag(PointerEventData eventData) public void OnPointerClick(PointerEventData eventData)
{ {
if (OnBeginDragHandler != null) if (OnClickHandler != null)
{ {
OnBeginDragHandler.Invoke(eventData); OnClickHandler.Invoke(eventData);
} }
} }

View File

@@ -2,6 +2,11 @@ using UnityEngine;
public class Define public class Define
{ {
public enum UIEvent
{
Click,
Drag,
}
public enum MouseEvent public enum MouseEvent
{ {
Press, Press,

View File

@@ -0,0 +1,12 @@
using System;
using UnityEngine;
using UnityEngine.EventSystems;
public static class Extension
{
public static void AddUIEvent(this GameObject go, Action<PointerEventData> action,
Define.UIEvent type = Define.UIEvent.Click)
{
UI_Base.AddUIEvent(go, action, type);
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 442b91d9fdae87b43b7b0c1c47026c65

View File

@@ -2,6 +2,17 @@ using UnityEngine;
public class Util public class Util
{ {
public static T GetOrAddComponent<T>(GameObject go) where T : UnityEngine.Component
{
T component = go.GetComponent<T>();
if (component == null)
{
component = go.AddComponent<T>();
}
return component;
}
public static GameObject FindChild(GameObject go, string name = null, bool recursive = false) public static GameObject FindChild(GameObject go, string name = null, bool recursive = false)
{ {
Transform transform = FindChild<Transform>(go, name, recursive); Transform transform = FindChild<Transform>(go, name, recursive);