Image를 마우스로 드래그할 수 있는 이벤트를 구현하기 위해 UI EventHandler 추가

This commit is contained in:
2026-01-25 22:30:18 +09:00
parent ce83f21c93
commit 8b013830a0
5 changed files with 686 additions and 1 deletions

View File

@@ -17,7 +17,8 @@ public class PlayerController : MonoBehaviour
Managers.Input.MouseAction -= OnMouseClicked;
Managers.Input.MouseAction += OnMouseClicked;
Managers.Resource.Instantiate("UI/UI_Button");
// UI 생성
// Managers.Resource.Instantiate("UI/UI_Button");
}
public enum PlayerState

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_Button : UI_Base
@@ -22,6 +23,11 @@ public class UI_Button : UI_Base
TestObject
}
enum Images
{
ItemIcon,
}
private void Start()
{
// Reflection
@@ -29,10 +35,15 @@ public class UI_Button : UI_Base
// Text 대신 TextMeshProUGUI로 변경
Bind<TextMeshProUGUI>(typeof(Texts));
Bind<GameObject>(typeof(GameObjects));
Bind<Image>(typeof(Images));
// Get 호출 시에도 타입을 맞춰줍니다.
// Get<TextMeshProUGUI>((int)Texts.ScoreText).text = "Bind Test";
GetText((int)Texts.ScoreText).text = "Bind Test";
GameObject go = GetImage((int)Images.ItemIcon).gameObject;
UI_EventHandler evt = go.GetComponent<UI_EventHandler>();
evt.OnDragHandler += ((PointerEventData data) => { evt.gameObject.transform.position = data.position; });
}
private int _score = 0;

View File

@@ -0,0 +1,26 @@
using System;
using UnityEngine;
using UnityEngine.EventSystems;
public class UI_EventHandler : MonoBehaviour, IBeginDragHandler, IDragHandler
{
public Action<PointerEventData> OnBeginDragHandler = null;
public Action<PointerEventData> OnDragHandler = null;
public void OnBeginDrag(PointerEventData eventData)
{
if (OnBeginDragHandler != null)
{
OnBeginDragHandler.Invoke(eventData);
}
}
public void OnDrag(PointerEventData eventData)
{
if (OnDragHandler != null)
{
OnDragHandler.Invoke(eventData);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 668ca21f9fefb404388f30bd6902a7c3