using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using Sirenix.OdinInspector; using KairoEngine.Core; namespace KairoEngine.UI { public class SliderText : MonoBehaviour { public enum TextType { Percentage, CurrentMax } public TextType textType = TextType.Percentage; public TextMeshProUGUI text; [ShowIf("@textType == TextType.Percentage")] public string suffix = "%"; [ShowIf("@textType == TextType.Percentage")] public float multiplier = 100; [ShowIf("@textType == TextType.CurrentMax")] public string separator = "/"; public bool hideEmptyFill = false; public Slider slider; public void OnEnable() { slider.onValueChanged.AddListener(delegate {UpdateText(); }); } void OnDisable() { slider.onValueChanged.RemoveListener(delegate {UpdateText(); }); } void Start() { UpdateText(); } [OnInspectorInit] public void UpdateText() { if(textType == TextType.Percentage) { float value = slider.value * multiplier; value = value < 0.6 ? 0 : Mathf.CeilToInt(value); text.text = $"{(int)value}{suffix}"; } else if(textType == TextType.CurrentMax) { float value = slider.value; float max = slider.maxValue; text.text = $"{(int)value}{separator}{(int)max}"; } if(hideEmptyFill) { if((int)slider.value == 0 && slider.fillRect.gameObject.activeSelf) Timer.ExecuteRealTime(20, () => slider.fillRect.gameObject.SetActive(false)); else if(!slider.fillRect.gameObject.activeSelf) Timer.ExecuteRealTime(20, () => slider.fillRect.gameObject.SetActive(true)); } } } }