2025-10-10 14:08:23 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Entity;
|
2025-09-03 19:59:22 +08:00
|
|
|
using Prefab;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace UI
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
public class BuffIconListUI : MonoBehaviour
|
2025-09-03 19:59:22 +08:00
|
|
|
{
|
|
|
|
|
public BuffIconUI prefab;
|
|
|
|
|
public Transform container;
|
2025-10-10 14:08:23 +08:00
|
|
|
private readonly List<BuffIconUI> icons = new(); // 用于存储所有的图标
|
|
|
|
|
|
|
|
|
|
public LivingEntity CurrentEntity { get; private set; }
|
2025-09-03 19:59:22 +08:00
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
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)
|
2025-09-03 19:59:22 +08:00
|
|
|
{
|
2025-10-10 14:08:23 +08:00
|
|
|
var newIcon = Instantiate(prefab, container);
|
|
|
|
|
icons.Add(newIcon);
|
2025-09-03 19:59:22 +08:00
|
|
|
}
|
2025-10-10 14:08:23 +08:00
|
|
|
|
|
|
|
|
return icons;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void FocusedEntityChanged(Entity.Entity entity)
|
|
|
|
|
{
|
|
|
|
|
CurrentEntity = entity as LivingEntity;
|
|
|
|
|
UpdateUI();
|
2025-09-03 19:59:22 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|