JamesMakesGames

Untitled

Mar 29th, 2022
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.21 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using TMPro;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7.  
  8. public class Selector : Selectable
  9. {
  10.     [SerializeField] private TextMeshProUGUI _labelText;
  11.     [SerializeField] private List<string> _options;
  12.     [SerializeField] private UnityEvent<int> _onValueChanged = new UnityEvent<int>();
  13.     private int _value;
  14.  
  15.     public int Value
  16.     {
  17.         get
  18.         {
  19.             return _value;
  20.         }
  21.         set
  22.         {
  23.             _value = value;
  24.             RefreshLabelText();
  25.             _onValueChanged?.Invoke(_value);
  26.         }
  27.     }
  28.  
  29.     public List<string> Options
  30.     {
  31.         get
  32.         {
  33.             return _options;
  34.         }
  35.         set
  36.         {
  37.             _options = value;
  38.             Value = 0;
  39.         }
  40.     }
  41.  
  42.     public UnityEvent<int> OnValueChanged
  43.     {
  44.         get
  45.         {
  46.             return _onValueChanged;
  47.         }
  48.     }
  49.  
  50.     protected override void Start()
  51.     {
  52.         base.Start();
  53.  
  54.         RefreshLabelText();
  55.     }
  56.  
  57.     public override void OnMove(AxisEventData eventData)
  58.     {
  59.         if (eventData.moveDir == MoveDirection.Left)
  60.         {
  61.             SelectPreviousItem();
  62.         }
  63.         else if (eventData.moveDir == MoveDirection.Right)
  64.         {
  65.             SelectNextItem();
  66.         }
  67.         else
  68.         {
  69.             base.OnMove(eventData);
  70.         }
  71.     }
  72.  
  73.     public void RefreshLabelText()
  74.     {
  75.         if (_options.Count == 0)
  76.         {
  77.             _labelText.SetText("");
  78.         }
  79.         else
  80.         {
  81.             _labelText.SetText(_options[_value].ToString());
  82.         }
  83.     }
  84.  
  85.     public void SelectPreviousItem()
  86.     {
  87.         int newValue = _value - 1;
  88.  
  89.         if (newValue < 0)
  90.         {
  91.             newValue = _options.Count - 1;
  92.         }
  93.  
  94.         Value = newValue;
  95.     }
  96.  
  97.     public void SelectNextItem()
  98.     {
  99.         int newValue = _value + 1;
  100.  
  101.         if (newValue > _options.Count - 1)
  102.         {
  103.             newValue = 0;
  104.         }
  105.  
  106.         Value = newValue;
  107.     }
  108.  
  109.     public void SetValueSilent(int value)
  110.     {
  111.         _value = value;
  112.  
  113.         RefreshLabelText();
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment