147 lines
4.3 KiB
C#
147 lines
4.3 KiB
C#
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace Studio.UVC.UI
|
|
{
|
|
[RequireComponent(typeof(RectTransform))]
|
|
public class UVCTabButton : MonoBehaviour, ITabButton, IPointerEnterHandler, IPointerExitHandler
|
|
{
|
|
private static GameObject prefab;
|
|
|
|
public static UVCTabButton Create(string text, Transform parent = null)
|
|
{
|
|
if (prefab == null) prefab = Resources.Load("Prefabs/Common/UVCTabButton", typeof(GameObject)) as GameObject;
|
|
var instance = Instantiate(prefab, parent);
|
|
var btn = instance.GetComponent<UVCTabButton>();
|
|
if (text != null && text.Length > 0) btn.GetComponentInChildren<TextMeshProUGUI>().text = text;
|
|
return btn;
|
|
}
|
|
|
|
|
|
public UnityEvent<int, bool> onClick = new UnityEvent<int, bool>();
|
|
|
|
[SerializeField]
|
|
private bool selected = false;
|
|
public bool Selected { get => selected; }
|
|
|
|
public int Index { get; set; }
|
|
|
|
private TextMeshProUGUI txt;
|
|
private UnityEngine.UI.Button 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 void Init()
|
|
{
|
|
txt = GetComponentInChildren<TextMeshProUGUI>();
|
|
button = GetComponent<UnityEngine.UI.Button>();
|
|
rect = GetComponent<RectTransform>();
|
|
image = GetComponent<Image>();
|
|
|
|
if (button != null)
|
|
{
|
|
button.onClick.AddListener(() =>
|
|
{
|
|
if (selected) return;
|
|
selected = true;
|
|
onClick.Invoke(Index, selected);
|
|
});
|
|
}
|
|
|
|
if (selected) Select();
|
|
else Deselect();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 텍스트 길이에 따른 사이즈 변경
|
|
/// </summary>
|
|
/// <param name="text">Title Name</param>
|
|
/// <param name="isSize">autosizeing</param>
|
|
public void SetText(string text, bool isAutoSize)
|
|
{
|
|
txt.text = text;
|
|
if (isAutoSize)
|
|
rect.sizeDelta = new Vector2(txt.preferredWidth + Space, rect.sizeDelta.y);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 선택된 탭
|
|
/// </summary>
|
|
public void Select()
|
|
{
|
|
if (image == null) image = GetComponent<Image>();
|
|
if (txt == null) txt = GetComponentInChildren<TextMeshProUGUI>();
|
|
|
|
image.sprite = selectSprite;
|
|
image.color = selectColor;
|
|
|
|
selected = true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 탭이 변경 될 경우
|
|
/// </summary>
|
|
public void Deselect()
|
|
{
|
|
if (image == null) image = GetComponent<Image>();
|
|
if (txt == null) txt = GetComponentInChildren<TextMeshProUGUI>();
|
|
|
|
image.sprite = normalSprite;
|
|
image.color = normalColor;
|
|
|
|
selected = false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 버튼 상태에 따른 색상 설정
|
|
/// </summary>
|
|
/// <param name="_normalColor"></param>
|
|
/// <param name="_highLightColor"></param>
|
|
/// <param name="_selectColor"></param>
|
|
public void SetButtonColor(Color _normalColor, Color _highLightColor, Color _selectColor)
|
|
{
|
|
normalColor = _normalColor;
|
|
highLightColor = _highLightColor;
|
|
selectColor = _selectColor;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (button != null) button.onClick.RemoveAllListeners();
|
|
onClick.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;
|
|
}
|
|
}
|
|
}
|