Unity_Learn/Assets/Scripts/UI/Popup/UI_Button.cs
2025-09-05 16:04:55 +09:00

78 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using Object = UnityEngine.Object;
public class UI_Button : UI_Popup
{
enum Buttons
{
PointButton
}
enum Texts
{
PointText,
ScoreText
}
enum GameObjects
{
TestObject,
}
enum Images
{
ItemIcon
}
private void Start()
{
Init();
}
public override void Init()
{
// 내 부모의 Init() 호출
base.Init();
Bind<Button>(typeof(Buttons));
Bind<TextMeshProUGUI>(typeof(Texts));
Bind<GameObject>(typeof(GameObjects));
Bind<Image>(typeof(Images));
// GetText((int)Texts.ScoreText).text = "Bind Text";
/*
// 당장 이미지를 받아 올게 아니라 이미지 오브젝트에 스크립트 컴포넌트를 추가하거나
// 이미 있다면 이벤트 연동하기 위해서 게임오브젝트로 받아온다.
GameObject go = GetImage((int)Images.ItemIcon).gameObject;
UI_EventHandler evt = go.GetComponent<UI_EventHandler>();
// 이 아래에서 만약 람다 내부에 오브젝트를 이렇게 transform.position 받아오면 이건 이미지의 transform이 아니라,
// UI_Button 이 가지고 있는 transform이 된다. 그래서 evt.gameObject.transform.position 으로 받아와야 한다.
evt.OnDragHandler += ((PointerEventData data) => { evt.gameObject.transform.position = data.position; });
*/
// 위의 부분을 조금 더 간략하게 만들기 위해 공통단으로 보내고 이것저것 한게 아래다.
GameObject go = GetImage((int)Images.ItemIcon).gameObject;
AddUIEvent(go,(PointerEventData data) => { go.transform.position = data.position; }, Define.UIEvent.Drag);
// 이게 위에같이 2줄로 나눠서 해도 되는데 익스텐션 메서드를 만들어서 하는 방법이 있는데 Extension.cs 참고
GetButton((int)Buttons.PointButton).gameObject.AddUIEvent(OnButtonClicked);
}
int _score = 0;
public void OnButtonClicked(PointerEventData eventData)
{
_score++;
Debug.Log("Button Clicked");
GetText((int)Texts.ScoreText).text = $"점수: {_score}점";
}
}