Files
YachtDice/Assets/Scripts/Shop/ShopCatalog.cs
T
2026-03-02 12:49:12 +07:00

46 lines
1.1 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace YachtDice.Shop
{
[CreateAssetMenu(fileName = "ShopCatalog", menuName = "YachtDice/Shop/Catalog")]
public class ShopCatalog : ScriptableObject
{
[SerializeField] private List<ScriptableObject> items = new();
private List<IShopItem> _cachedItems;
public IReadOnlyList<IShopItem> All
{
get
{
if (_cachedItems == null) RebuildCache();
return _cachedItems;
}
}
public IShopItem FindById(string id)
{
var all = All;
for (int i = 0; i < all.Count; i++)
{
if (all[i] != null && all[i].Id == id)
return all[i];
}
return null;
}
private void RebuildCache()
{
_cachedItems = new List<IShopItem>();
foreach (var t in items)
{
if (t is IShopItem shopItem)
_cachedItems.Add(shopItem);
}
}
private void OnValidate() => _cachedItems = null;
}
}