mirror of
http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite.git
synced 2025-11-19 23:37:13 +08:00
Co-authored-by: m0_75251201 <m0_75251201@noreply.gitcode.com> Reviewed-on: http://47.107.252.169:3000/Roguelite-Game-Developing-Team/Gen_Hack-and-Slash-Roguelite/pulls/60
67 lines
1.8 KiB
C#
67 lines
1.8 KiB
C#
using System.Collections.Generic;
|
|
using Entity;
|
|
using Prefab;
|
|
using UnityEngine;
|
|
|
|
namespace UI
|
|
{
|
|
public class BuffIconListUI : MonoBehaviour
|
|
{
|
|
public BuffIconUI prefab;
|
|
public Transform container;
|
|
private readonly List<BuffIconUI> icons = new(); // 用于存储所有的图标
|
|
|
|
public LivingEntity CurrentEntity { get; private set; }
|
|
|
|
private void Start()
|
|
{
|
|
foreach (Transform child in container) Destroy(child.gameObject);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
Program.Instance.OnFocusedEntityChanged += FocusedEntityChanged;
|
|
FocusedEntityChanged(Program.Instance.FocusedEntity);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
Program.Instance.OnFocusedEntityChanged -= FocusedEntityChanged;
|
|
}
|
|
|
|
|
|
public void UpdateUI()
|
|
{
|
|
if (!CurrentEntity)
|
|
return;
|
|
var buffes = CurrentEntity.Hediffs;
|
|
var icons = SetIconCount(buffes.Count);
|
|
for (var i = 0; i < buffes.Count; i++) icons[i].Init(buffes[i]);
|
|
}
|
|
|
|
public List<BuffIconUI> SetIconCount(int count)
|
|
{
|
|
// 清空现有子对象
|
|
while (icons.Count > count)
|
|
{
|
|
Destroy(icons[icons.Count - 1].gameObject);
|
|
icons.RemoveAt(icons.Count - 1);
|
|
}
|
|
|
|
// 添加新子对象
|
|
while (icons.Count < count)
|
|
{
|
|
var newIcon = Instantiate(prefab, container);
|
|
icons.Add(newIcon);
|
|
}
|
|
|
|
return icons;
|
|
}
|
|
|
|
public void FocusedEntityChanged(Entity.Entity entity)
|
|
{
|
|
CurrentEntity = entity as LivingEntity;
|
|
UpdateUI();
|
|
}
|
|
}
|
|
} |