Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- public class Selector : Selectable
- {
- [SerializeField] private TextMeshProUGUI _labelText;
- [SerializeField] private List<string> _options;
- [SerializeField] private UnityEvent<int> _onValueChanged = new UnityEvent<int>();
- private int _value;
- public int Value
- {
- get
- {
- return _value;
- }
- set
- {
- _value = value;
- RefreshLabelText();
- _onValueChanged?.Invoke(_value);
- }
- }
- public List<string> Options
- {
- get
- {
- return _options;
- }
- set
- {
- _options = value;
- Value = 0;
- }
- }
- public UnityEvent<int> OnValueChanged
- {
- get
- {
- return _onValueChanged;
- }
- }
- protected override void Start()
- {
- base.Start();
- RefreshLabelText();
- }
- public override void OnMove(AxisEventData eventData)
- {
- if (eventData.moveDir == MoveDirection.Left)
- {
- SelectPreviousItem();
- }
- else if (eventData.moveDir == MoveDirection.Right)
- {
- SelectNextItem();
- }
- else
- {
- base.OnMove(eventData);
- }
- }
- public void RefreshLabelText()
- {
- if (_options.Count == 0)
- {
- _labelText.SetText("");
- }
- else
- {
- _labelText.SetText(_options[_value].ToString());
- }
- }
- public void SelectPreviousItem()
- {
- int newValue = _value - 1;
- if (newValue < 0)
- {
- newValue = _options.Count - 1;
- }
- Value = newValue;
- }
- public void SelectNextItem()
- {
- int newValue = _value + 1;
- if (newValue > _options.Count - 1)
- {
- newValue = 0;
- }
- Value = newValue;
- }
- public void SetValueSilent(int value)
- {
- _value = value;
- RefreshLabelText();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment