58 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class MStringStepper : MonoBehaviour
{
public event Action<string> OnValueChanged;
[SerializeField] private TextMeshProUGUI _valueLabel;
[SerializeField] private CanvasGroup _leftArrow;
[SerializeField] private CanvasGroup _rightArrow;
private string _value;
private List<string> _values;
private int _index;
private bool _switched = false;
public void Initialize(string value, List<string> values)
{
_value = value;
_values = values;
_index = values.IndexOf(value);
UpdateView();
}
public void SetDirection(int direction)
{
if(_switched && direction == 0)
_switched = false;
if(_switched) return;
int newIndex = _index + direction;
newIndex = Mathf.Clamp(newIndex, 0, _values.Count - 1);
bool valueChanged = newIndex != _index;
if (valueChanged)
{
_index = newIndex;
_value = _values[_index];
OnValueChanged?.Invoke(_value);
UpdateView();
_switched = true;
}
}
private void UpdateView()
{
_valueLabel.text = _value.ToString();
_leftArrow.alpha = _index == 0 ? 0 : 1;
_rightArrow.alpha = _index == _values.Count - 1 ? 0 : 1;
}
}