Unity_Utils/Canvases/Components/ButtonUIComponent.cs

62 lines
1.5 KiB
C#

using System;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.UI;
namespace Canvases.Components
{
public class ButtonUIComponent : UISelectableComponentBase
{
public event Action OnClick;
[Header("Association"), SerializeField, Required]
private Button button;
[Header("Configuration"), SerializeField]
private ButtonNavigationType buttonNavigationType;
private enum ButtonNavigationType
{
Forward,
Backward
}
public Color Color
{
set => button.image.color = value;
}
public bool Enabled
{
set => button.interactable = value;
}
public void Select() => button.Select();
protected override void SetSelectableGameObject() => Selectable = button;
private void Start()
{
Debug.Assert(button, $"A {nameof(button)} must be assigned to a {nameof(ButtonUIComponent)}");
button.onClick.AddListener(OnButtonClicked);
}
protected override void OnDestroy()
{
button.onClick.RemoveListener(OnButtonClicked);
base.OnDestroy();
}
private void OnButtonClicked()
{
// PlayClickedSound();
OnClick?.Invoke();
}
private void OnValidate()
{
if (!button)
button = GetComponent<Button>();
}
}
}