76 lines
1.8 KiB
C#
76 lines
1.8 KiB
C#
using DG.Tweening;
|
|
using System;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class MSignalButton : MonoBehaviour
|
|
{
|
|
public event Action OnClick;
|
|
|
|
[SerializeField] protected Image _image;
|
|
[SerializeField] protected TextMeshProUGUI _textMeshPro;
|
|
[SerializeField] protected Color _normalColor;
|
|
[SerializeField] protected Color _selectedColor;
|
|
[SerializeField] protected Color _pressedColor;
|
|
[SerializeField] protected AudioClip _clip;
|
|
|
|
protected bool _lastPressed = false;
|
|
protected Sequence _animation;
|
|
|
|
public void TakeInput(bool pressed)
|
|
{
|
|
if (_lastPressed != pressed)
|
|
{
|
|
if (pressed)
|
|
{
|
|
ClickAnimation();
|
|
}
|
|
_lastPressed = pressed;
|
|
}
|
|
}
|
|
|
|
public virtual void ClickAnimation()
|
|
{
|
|
StopAnimation();
|
|
|
|
_animation = DOTween.Sequence();
|
|
|
|
_animation.Append(_image.DOColor(_pressedColor, 0.15f));
|
|
_animation.Append(_image.DOColor(_selectedColor, 0.15f));
|
|
_animation.AppendCallback(() =>
|
|
{
|
|
OnClick?.Invoke();
|
|
SoundManager.instance.playClip(_clip);
|
|
});
|
|
|
|
_animation.PlayForward();
|
|
}
|
|
|
|
public void SelectAnimation()
|
|
{
|
|
StopAnimation();
|
|
|
|
_animation = DOTween.Sequence();
|
|
_animation.Append(_image.DOColor(_selectedColor, 0.3f));
|
|
_animation.Join(_textMeshPro.DOColor(Color.black, 0.3f));
|
|
_animation.PlayForward();
|
|
}
|
|
|
|
public void DeselectAnimation()
|
|
{
|
|
StopAnimation();
|
|
|
|
_image.color = _normalColor;
|
|
_textMeshPro.color = Color.white;
|
|
}
|
|
|
|
protected void StopAnimation()
|
|
{
|
|
if (_animation != null)
|
|
{
|
|
_animation.Kill();
|
|
_animation = null;
|
|
}
|
|
}
|
|
} |