119 lines
3.8 KiB
C#
119 lines
3.8 KiB
C#
using System.Collections.Generic;
|
|
using BriarQueen.Data.Assets;
|
|
using NaughtyAttributes;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
|
|
namespace BriarQueen.Framework.Registries
|
|
{
|
|
[CreateAssetMenu(menuName = "Briar Queen/Registries/Asset Registry", fileName = "New Asset Registry")]
|
|
public class AssetRegistry : ScriptableObject
|
|
{
|
|
[Header("Asset References (Generic Addressables)")]
|
|
[ReorderableList]
|
|
[SerializeField]
|
|
private List<AssetEntry> _sceneReferences = new();
|
|
|
|
[ReorderableList]
|
|
[SerializeField]
|
|
private List<AssetEntry> _levelReferences = new();
|
|
|
|
[ReorderableList]
|
|
[SerializeField]
|
|
private List<AssetEntry> _itemReferences = new();
|
|
|
|
[ReorderableList]
|
|
[SerializeField]
|
|
private List<AssetEntry> _uiReferences = new();
|
|
|
|
private Dictionary<string, AssetReference> _assetDictionary;
|
|
|
|
public IReadOnlyList<AssetEntry> SceneReferences => _sceneReferences;
|
|
public IReadOnlyList<AssetEntry> LevelReferences => _levelReferences;
|
|
public IReadOnlyList<AssetEntry> ItemReferences => _itemReferences;
|
|
public IReadOnlyList<AssetEntry> UIReferences => _uiReferences;
|
|
|
|
public void EnsureInitialized()
|
|
{
|
|
if (_assetDictionary != null)
|
|
return;
|
|
|
|
RebuildLookup();
|
|
}
|
|
|
|
public void RebuildLookup()
|
|
{
|
|
_assetDictionary = new Dictionary<string, AssetReference>();
|
|
|
|
AddEntries(_sceneReferences, "Scenes");
|
|
AddEntries(_levelReferences, "Levels");
|
|
AddEntries(_itemReferences, "Items");
|
|
AddEntries(_uiReferences, "UI");
|
|
}
|
|
|
|
private void AddEntries(List<AssetEntry> entries, string category)
|
|
{
|
|
if (entries == null)
|
|
return;
|
|
|
|
foreach (var entry in entries)
|
|
{
|
|
if (entry == null)
|
|
continue;
|
|
|
|
if (string.IsNullOrWhiteSpace(entry.AssetKey))
|
|
{
|
|
Debug.LogWarning(
|
|
$"[AssetRegistry] Skipping {category} entry '{entry.name}' because AssetKey is empty.", this);
|
|
continue;
|
|
}
|
|
|
|
if (entry.Asset == null)
|
|
{
|
|
Debug.LogWarning(
|
|
$"[AssetRegistry] Skipping {category} entry '{entry.name}' because AssetReference is null.",
|
|
this);
|
|
continue;
|
|
}
|
|
|
|
if (_assetDictionary.ContainsKey(entry.AssetKey))
|
|
{
|
|
Debug.LogError(
|
|
$"[AssetRegistry] Duplicate AssetKey detected: '{entry.AssetKey}' from entry '{entry.name}'.",
|
|
this);
|
|
continue;
|
|
}
|
|
|
|
_assetDictionary.Add(entry.AssetKey, entry.Asset);
|
|
}
|
|
}
|
|
|
|
public bool TryGetReference(string key, out AssetReference reference)
|
|
{
|
|
EnsureInitialized();
|
|
return _assetDictionary.TryGetValue(key, out reference);
|
|
}
|
|
|
|
public IEnumerable<string> GetAllKeys()
|
|
{
|
|
EnsureInitialized();
|
|
return _assetDictionary.Keys;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
public void SetDiscoveredEntries(
|
|
List<AssetEntry> sceneEntries,
|
|
List<AssetEntry> levelEntries,
|
|
List<AssetEntry> itemEntries,
|
|
List<AssetEntry> uiEntries)
|
|
{
|
|
_sceneReferences = sceneEntries ?? new List<AssetEntry>();
|
|
_levelReferences = levelEntries ?? new List<AssetEntry>();
|
|
_itemReferences = itemEntries ?? new List<AssetEntry>();
|
|
_uiReferences = uiEntries ?? new List<AssetEntry>();
|
|
|
|
RebuildLookup();
|
|
}
|
|
#endif
|
|
}
|
|
} |