57 lines
1.5 KiB
C#
57 lines
1.5 KiB
C#
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.UI;
|
|
|
|
/// <summary>
|
|
/// 원하는 스크립트에서 메서드 만들고 {버튼}.Bind(메서드) 하면 클릭시 메서드 실행
|
|
/// </summary>
|
|
public class Button_Menu: MonoBehaviour
|
|
{
|
|
private Button _button;
|
|
private Image _image;
|
|
private TextMeshPro _text;
|
|
|
|
private void Awake()
|
|
{
|
|
_button = GetComponent<Button>();
|
|
_image = GetComponentInChildren<Image>();
|
|
_text = GetComponentInChildren<TextMeshPro>();
|
|
}
|
|
|
|
public void SetButton(Sprite image, string text, bool onImage = true, bool onText = true)
|
|
{
|
|
if (onImage)
|
|
{
|
|
_image.enabled = true;
|
|
_image.sprite = image;
|
|
}
|
|
else _image.enabled = false;
|
|
|
|
if (onText)
|
|
{
|
|
_text.enabled = true;
|
|
_text.text = text;
|
|
}
|
|
else _text.enabled = false;
|
|
}
|
|
|
|
// 기존에 바인딩된 액션을 모두 제거하고 새로 바인딩
|
|
public void Bind(UnityAction action)
|
|
{
|
|
_button.onClick.RemoveAllListeners();
|
|
_button.onClick.AddListener(action);
|
|
}
|
|
|
|
// 기존에 바인딩된 액션을 유지하며 바인딩추가
|
|
public void Add(UnityAction action)
|
|
{
|
|
_button.onClick.AddListener(action);
|
|
}
|
|
|
|
// 버튼 클릭 강제 실행
|
|
// 버튼의 onClick 이벤트에 등록된 모든 리스너를 호출
|
|
public void Click() => _button.onClick?.Invoke();
|
|
}
|