using System; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.EventSystems; using UnityEngine.UI; namespace Studio.UVC.UI { [RequireComponent(typeof(RectTransform))] public class UVCButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler { private static GameObject prefab; public static UVCButton Create(string text, Transform parent = null) { if (prefab == null) prefab = Resources.Load("Prefabs/Common/UVCButton", typeof(GameObject)) as GameObject; var instance = Instantiate(prefab, parent); var btn = instance.GetComponent(); if (text != null && text.Length > 0) btn.GetComponentInChildren().text = text; return btn; } [SerializeField] private bool selected = false; public bool Selected { get => selected; } public int Index { get; set; } private TextMeshProUGUI txt; private UnityEngine.UI.Button button; public Button UVCClickButton { get { return button; } } private RectTransform rect; public RectTransform Rect { get { return rect; } } private Image image; public Image Image { get { return image; } } public float Space = 8f; [Header("Color")] public Color normalColor; public Color HighLightColor; public Color selectColor; [Header("Sprite")] public Sprite normalSprite; public Sprite selectSprite; public UnityEvent onClickButton; public void Init() { txt = GetComponentInChildren(); button = GetComponent(); rect = GetComponent(); image = GetComponent(); button.navigation = new Navigation() { mode = Navigation.Mode.None }; if (button != null) { button.onClick.AddListener(() => { if (selected) return; selected = true; onClickButton.Invoke(); }); } } /// /// 텍스트 길이에 따른 사이즈 변경 /// /// Title Name public void SetText(string text) { txt.text = text; rect.sizeDelta = new Vector2(txt.preferredWidth + Space, rect.sizeDelta.y); } /// /// 선택된 탭 /// public void Select() { if (image == null) image = GetComponent(); if (txt == null) txt = GetComponentInChildren(); image.sprite = selectSprite; image.color = selectColor; selected = true; } /// /// 탭이 변경 될 경우 /// public void Deselect() { if (image == null) image = GetComponent(); if (txt == null) txt = GetComponentInChildren(); image.sprite = normalSprite; image.color = normalColor; selected = false; } private void OnDestroy() { if (button != null) button.onClick.RemoveAllListeners(); onClickButton.RemoveAllListeners(); } public void OnPointerEnter(PointerEventData eventData) { image.sprite = selectSprite; image.color = HighLightColor; } public void OnPointerExit(PointerEventData eventData) { if (selected) image.color = selectColor; else image.sprite = normalSprite; image.color = normalColor; } } }