first commit
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
using System.Collections.Generic;
|
||||
using OTG.Lab06.Abilities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace OTG.Lab06.UI
|
||||
{
|
||||
public sealed class AbilityBarUI : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private AbilityManager abilityManager;
|
||||
[SerializeField] private AbilitySlotUI slotPrefab;
|
||||
[SerializeField] private Transform slotRoot;
|
||||
[SerializeField] private TooltipUI tooltip;
|
||||
|
||||
private readonly List<AbilitySlotUI> slots = new List<AbilitySlotUI>();
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
if (abilityManager != null)
|
||||
{
|
||||
abilityManager.AbilitiesChanged += RebuildOrRefresh;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
if (abilityManager != null)
|
||||
{
|
||||
abilityManager.AbilitiesChanged -= RebuildOrRefresh;
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (abilityManager != null)
|
||||
{
|
||||
RebuildOrRefresh(abilityManager.Abilities);
|
||||
}
|
||||
}
|
||||
|
||||
public void Bind(AbilityManager manager, AbilitySlotUI prefab, Transform root, TooltipUI tooltipUI)
|
||||
{
|
||||
if (abilityManager != null)
|
||||
{
|
||||
abilityManager.AbilitiesChanged -= RebuildOrRefresh;
|
||||
}
|
||||
|
||||
abilityManager = manager;
|
||||
slotPrefab = prefab;
|
||||
slotRoot = root;
|
||||
tooltip = tooltipUI;
|
||||
|
||||
if (isActiveAndEnabled && abilityManager != null)
|
||||
{
|
||||
abilityManager.AbilitiesChanged += RebuildOrRefresh;
|
||||
RebuildOrRefresh(abilityManager.Abilities);
|
||||
}
|
||||
}
|
||||
|
||||
private void RebuildOrRefresh(IReadOnlyList<AbilityRuntime> abilities)
|
||||
{
|
||||
if (slotPrefab == null || slotRoot == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
while (slots.Count < abilities.Count)
|
||||
{
|
||||
AbilitySlotUI slot = Instantiate(slotPrefab, slotRoot);
|
||||
slots.Add(slot);
|
||||
}
|
||||
|
||||
for (int i = 0; i < slots.Count; i++)
|
||||
{
|
||||
bool active = i < abilities.Count;
|
||||
slots[i].gameObject.SetActive(active);
|
||||
if (active)
|
||||
{
|
||||
slots[i].Bind(abilities[i], i, tooltip);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user