using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; /* //Set properties in C# example codes. using RainbowArt.CleanFlatUI; public class ProgressBarDemo : MonoBehaviour { //The ProgressBar Component. public ProgressBar m_ProgressBar; void Start() { //Set the current value. m_ProgressBar.CurrentValue = 50; //Set the maximum value. m_ProgressBar.MaxValue = 100; //Set whether to show the text. m_ProgressBar.HasText = true; } } */ namespace RainbowArt.CleanFlatUI { [ExecuteAlways] public class ProgressBar : MonoBehaviour { [SerializeField] float currentValue = 0f; [SerializeField] float maxValue = 100.0f; [SerializeField] bool hasText = true; [SerializeField] TextMeshProUGUI text; [SerializeField] Image foreground; bool bDelayedUpdate = false; public float CurrentValue { get => currentValue; set { if(currentValue == value) { return; } currentValue = value; OnValueChanged(); } } public float MaxValue { get => maxValue; set { if(maxValue == value) { return; } maxValue = value; OnValueChanged(); } } public bool HasText { get => hasText; set { if (hasText == value) { return; } hasText = value; UpdateText(); } } void OnValueChanged() { if(maxValue < 0) { maxValue = 100.0f; } if(currentValue < 0) { currentValue = 0f; } currentValue = Mathf.Clamp(currentValue, 0, maxValue); UpdateGUI(); } void Start() { UpdateGUI(); } void Update() { if(bDelayedUpdate) { bDelayedUpdate = false; OnValueChanged(); } } void UpdateGUI() { UpdateForeground(); UpdateText(); } void UpdateForeground() { foreground.fillAmount = currentValue / maxValue; } void UpdateText() { if (text != null && text.gameObject.activeSelf != hasText) { text.gameObject.SetActive(hasText); } if (hasText && (text != null)) { text.text = (int)((currentValue/maxValue)*100) + "%"; } } #if UNITY_EDITOR protected void OnValidate() { bDelayedUpdate = true; } #endif } }