using System; using UnityEngine; namespace SpellBoundAR.Items.Selections.UI { public class ItemTypeSelectionMenu : MonoBehaviour { public event Action OnSelectionChanged; public event Action OnSelectionChangedTo; private IItemTypeData _currentSelection; public IItemTypeData PreviousSelection { get; private set; } public IItemTypeData CurrentSelection { get => _currentSelection; set { if (_currentSelection == value) return; PreviousSelection = _currentSelection; _currentSelection = value; OnSelectionChanged?.Invoke(); OnSelectionChangedTo?.Invoke(_currentSelection); } } [SerializeField] private ScriptedItemTypeDataList items; [SerializeField] private ItemTypeSelectionButton prefab; [SerializeField] private Transform parent; public ScriptedItemTypeDataList Items { get => items; set { items = value; Refresh(); } } private void OnEnable() => Refresh(); private void Refresh() { DestroyAllItems(); SpawnAllItems(); } private void DestroyAllItems() { if (!parent) return; foreach (Transform child in parent.transform) { Destroy(child.gameObject); } } private void SpawnAllItems() { if (!items) return; foreach (ScriptedItemTypeData scriptedItemTypeData in items.ItemTypes) { if (!scriptedItemTypeData) continue; Instantiate(prefab, parent.transform).Initialize(this, scriptedItemTypeData); } } } }