namespace UIWidgets.Examples { using System.Collections.Generic; using UIWidgets; using UnityEngine; using UnityEngine.UI; /// /// Spinner with string values. /// Display strings instead numeric value. /// public class SpinnerString : MonoBehaviour, IUpgradeable { /// /// Spinner. /// [SerializeField] protected Spinner Spinner; /// /// Text component to display selected option. /// [SerializeField] [HideInInspector] [System.Obsolete("Replaced with TextAdapter.")] protected Text Text; /// /// Text component to display selected option. /// [SerializeField] protected TextAdapter TextAdapter; /// /// Options list. /// [SerializeField] protected List Options = new List(); /// /// Start this instance. /// protected virtual void Start() { Init(); } /// /// Init this instance. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "HAA0603:Delegate allocation from a method group", Justification = "Required")] protected virtual void Init() { Spinner.Min = -1; Spinner.Max = Options.Count; Spinner.Step = 1; // add callback Spinner.onValueChangeInt.AddListener(Changed); // display initial option Changed(Spinner.Value); } /// /// Handle change event. /// /// Value. protected virtual void Changed(int value) { if (value == -1) { Spinner.Value = Options.Count - 1; } else if (value == Options.Count) { Spinner.Value = 0; } else { // display option TextAdapter.text = Options[Spinner.Value]; } } /// /// Upgrade this instance. /// public virtual void Upgrade() { #pragma warning disable 0612, 0618 Utilities.GetOrAddComponent(Text, ref TextAdapter); #pragma warning restore 0612, 0618 } #if UNITY_EDITOR /// /// Validate this instance. /// protected virtual void OnValidate() { Compatibility.Upgrade(this); } #endif } }