using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using UnityEngine.Localization.Settings; /* //Set properties in C# example codes. using RainbowArt.CleanFlatUI; public class ProgressBarGridCircularDemo : MonoBehaviour { //The ProgressBarGridCircular Component. public ProgressBarGridCircular m_ProgressBar; void Start() { //Set the current value. m_ProgressBar.CurrentValue = 5; //Set whether to show the text. m_ProgressBar.HasText = true; } } */ namespace RainbowArt.CleanFlatUI { public class ProgressBarGridCircular : MonoBehaviour { [SerializeField] int currentValue = 0; [SerializeField] int maxValue = 10; [SerializeField] bool hasText = true; [SerializeField] TextMeshProUGUI text; [SerializeField] RectTransform background; [SerializeField] RectTransform foreground; [SerializeField] RectTransform bgTemplate; [SerializeField] RectTransform fgTemplate; List bgList = new List(); List fgList = new List(); public int CurrentValue { get => currentValue; set { if (currentValue == value) { return; } currentValue = value; OnValueChanged(); UpdateGUI(); } } public bool HasText { get => hasText; set { if (hasText == value) { return; } hasText = value; UpdateText(); } } void OnValueChanged() { if (currentValue < 0) { currentValue = 0; } if (maxValue < 0) { maxValue = 10; } if (currentValue > maxValue) { currentValue = maxValue; } } void Start() { OnValueChanged(); CreateList(bgList, background, bgTemplate); CreateList(fgList, foreground, fgTemplate); UpdateGUI(); } void UpdateGUI() { UpdateForeground(); UpdateText(); } void CreateList(List list, RectTransform rectParent, RectTransform template) { template.gameObject.SetActive(false); float angle = 360f / (float)maxValue; for (int i = 0; i < maxValue; i++) { RectTransform item = CreateItem(rectParent, template, i); list.Add(item); item.localEulerAngles = new Vector3(0, 0, -angle * i); } } RectTransform CreateItem(RectTransform rectParent, RectTransform template, int index) { GameObject obj = GameObject.Instantiate(template.gameObject, rectParent); obj.gameObject.SetActive(true); obj.gameObject.name = "item" + (index + 1); RectTransform rectTrans = obj.GetComponent(); rectTrans.localScale = Vector3.one; rectTrans.localEulerAngles = Vector3.zero; rectTrans.anchoredPosition3D = Vector3.zero; return rectTrans; } void UpdateForeground() { for (int i = 0; i < fgList.Count; i++) { RectTransform rectTrans = fgList[i]; Image image = rectTrans.transform.GetChild(0).GetComponent(); if (i < currentValue) { rectTrans.gameObject.SetActive(true); } else { rectTrans.gameObject.SetActive(false); } if (i < 16) { image.color = Color.Lerp(Color.green, Color.yellow, i / 16f); } else { image.color = Color.Lerp(Color.yellow, Color.red, (i - 16) / 16f); } } } void UpdateText() { if (text != null && text.gameObject.activeSelf != hasText) { text.gameObject.SetActive(hasText); } if (hasText && text != null) { //float val = (float)currentValue / (float)maxValue; //text.text = Mathf.FloorToInt(val * 100) + "%"; if (LocalizationSettings.SelectedLocale.Identifier.Code == "en") { text.text = currentValue * 500 + " RPM"; } else { text.text = currentValue * 500 + " 转"; } } } } }