using System.Collections; using System.Collections.Generic; 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 = 4; public bool auto_refresh = true; public Image[] icons; public Sprite sprite_full; public Sprite sprite_empty; [Header("Value Display")] public Text value_text; // 显示当前法力值数字的文本组件 public string value_format = "{0}"; // 文本格式,{0}会被替换为当前值 void Awake() { } void Update() { if (auto_refresh) Refresh(); } public void Refresh() { if (icons == null) return; int index = 0; foreach (Image icon in icons) { if (icon == null) continue; icon.gameObject.SetActive(index < value || index < max_value); icon.sprite = (index < value) ? sprite_full : sprite_empty; index++; } // 更新法力值数字文本 UpdateValueText(); } private void UpdateValueText() { if (value_text != null) { value_text.text = string.Format(value_format, value); } } // 手动设置值并更新文本 public void SetValue(int new_value) { value = new_value; UpdateValueText(); } public void SetMat(Material mat) { if (icons == null) return; foreach (Image icon in icons) { if (icon != null) icon.material = mat; } } } }