using System; using System.Collections; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; namespace TcgEngine.UI { /// /// Bar that contain multiple icons to represent a value /// Such as the mana bar during the game /// public class IconBar : MonoBehaviour { public int value = 0; public int max_value = 10; public bool auto_refresh = true; public List icons; public Sprite mana_icon_true; public Sprite mana_icon_false; public GameObject mana_gem; public Transform mana_magic_slot; [Header("Value Display")] public Text value_text; // 显示当前法力值数字的文本组件 public string value_format = "{0}"; // 文本格式,{0}会被替换为当前值 void Awake() { } private void Start() { InitManaData(); } private void InitManaData() { icons.Clear(); for (int i = 0; i < max_value; i++) { GameObject item = Instantiate(mana_gem, mana_magic_slot); item.name = $"mana--{i}"; var img = item.GetComponent(); icons.Add(img); } } void Update() { if (auto_refresh) Refresh(); } public void Refresh() { if (icons == null) return; for (int i = 0; i < icons.Count; i++) { if (icons[i] == null) continue; // 前 value 个点亮,后面的变灰 icons[i].sprite = (i < value) ? mana_icon_true : mana_icon_false; } // 更新法力值数字文本 UpdateValueText(); } private void UpdateValueText() { if (value_text != null) { value_text.text = string.Format(value_format, value + "/10"); } } } }