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 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<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<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;
UI_EventHandler evt = go.GetComponent<UI_EventHandler>();
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<TextMeshProUGUI>().text = "텍스트 변경 완료!";
_score += 1;
GetText((int)Texts.ScoreText).text = $"점수 : {_score}점";
}
}

View File

@@ -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<PointerEventData> OnBeginDragHandler = null;
public Action<PointerEventData> OnClickHandler = 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);
}
}