AssetManager 캐시 기능 추가

This commit is contained in:
NTG_DESKTOP 2025-07-30 03:52:13 +09:00
parent 4b3e6187ff
commit 4f735d3910
7 changed files with 54 additions and 32 deletions

View File

@ -199,9 +199,11 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
_button: {fileID: 7048029967178229479}
_backgroundImage: {fileID: 2629700089687513121}
_icon: {fileID: 8460685313298392783}
_countText: {fileID: 6606259529242263844}
_markImage: {fileID: 4825096369561648102}
_animator: {fileID: 8864952879369828923}
--- !u!114 &3263594629702172720
MonoBehaviour:
m_ObjectHideFlags: 0

View File

@ -15,6 +15,15 @@ namespace DDD
{
public class AssetManager : Singleton<AssetManager>, IManager
{
private static readonly Dictionary<string, AsyncOperationHandle> _cachedHandles = new();
protected override void OnApplicationQuit()
{
base.OnApplicationQuit();
ReleaseAllCached();
}
public void PreInit()
{
@ -32,31 +41,26 @@ public void PostInit()
public static async Task<T> LoadAsset<T>(string key) where T : UnityEngine.Object
{
var handle = Addressables.LoadAssetAsync<T>(key);
await handle.Task;
if (handle.Status == AsyncOperationStatus.Succeeded)
return handle.Result;
Debug.LogError($"Addressable load failed : {key}");
return null;
}
public static async Task<T> LoadAsset<T>(AssetReference reference) where T : UnityEngine.Object
{
if (reference == null)
if (_cachedHandles.TryGetValue(key, out var handle))
{
Debug.LogError("Null AssetReference");
return null;
if (handle.IsValid() && handle.Result is T result)
return result;
Debug.LogWarning($"[AssetManager] Type mismatch or invalid handle for key: {key}");
return handle.Result as T;
}
var handle = reference.LoadAssetAsync<T>();
await handle.Task;
// ✅ 새로 로드
var newHandle = Addressables.LoadAssetAsync<T>(key);
await newHandle.Task;
if (handle.Status == AsyncOperationStatus.Succeeded)
return handle.Result;
if (newHandle.Status == AsyncOperationStatus.Succeeded)
{
_cachedHandles[key] = newHandle;
return newHandle.Result;
}
Debug.LogError($"AssetReference load failed: {reference.RuntimeKey}");
Debug.LogError($"[AssetManager] Failed to load asset: {key}");
return null;
}
@ -90,6 +94,24 @@ public static async Task UnloadScene(SceneInstance sceneInstance)
await handle.Task;
}
public static void ReleaseAllCached()
{
if (_cachedHandles.Count == 0) return;
foreach (var kvp in _cachedHandles)
{
var handle = kvp.Value;
if (handle.IsValid())
{
Addressables.Release(handle);
Debug.Log($"[AssetManager] Released handle for key: {kvp.Key}");
}
}
_cachedHandles.Clear();
Debug.Log("[AssetManager] 모든 캐시된 Addressable 리소스를 해제했습니다.");
}
public static bool HasLabel(string addressKey, string label)
{
#if UNITY_EDITOR

View File

@ -1,6 +1,5 @@
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
namespace DDD
{

View File

@ -11,7 +11,7 @@ public class InventoryView : MonoBehaviour, IEventHandler<InventoryChangedEvent>
[SerializeField] private Transform _slotParent;
private RestaurantManagementSo _restaurantManagementSo;
private InventoryCategoryType _currenInventoryCategoryType = InventoryCategoryType.None;
private InventoryCategoryType _currenInventoryCategoryType = InventoryCategoryType.Food;
private readonly Dictionary<string, ItemSlotUi> _slotLookup = new();
private GameObject _firstSlot;
@ -47,7 +47,7 @@ public async Task Initialize()
{
var itemSlotUi = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _slotParent);
var slot = itemSlotUi.GetComponent<ItemSlotUi>();
slot.Initialize(model, new InventorySlotUiStrategy());
await slot.Initialize(model, new InventorySlotUiStrategy());
itemSlotUi.name = ItemSlotUiName + model.Id;
var interactor = itemSlotUi.GetComponent<TodayMenuInteractor>();

View File

@ -28,7 +28,6 @@ public async Task Initialize(ItemViewModel model, IItemSlotUiStrategy strategy)
await Strategy.Setup(this, model);
var controller = await strategy.GetAnimatorController();
_animator.runtimeAnimatorController = controller;
//AssetManager.ReleaseCachedByKey(strategy.AnimatorControllerKey);
}
public void SetBackgroundColor(Color color) => _backgroundImage.color = color;

View File

@ -20,9 +20,9 @@ protected override GameObject GetInitialSelected()
return _categoryTabs.GetFirstInteractableButton;
}
public override void Open()
public async override void Open()
{
_ = _inventoryView.Initialize();
await _inventoryView.Initialize();
_sectionTabs.Initialize(OnSectionTabSelected);
_categoryTabs.Initialize(OnCategoryTabSelected);
EventBus.Register<TodayMenuRemovedEvent>(this);

View File

@ -41,7 +41,7 @@ private async void Initialize()
{
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayFoodContent);
var slot = go.GetComponent<ItemSlotUi>();
slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
var todayMenuInteractor = go.GetComponent<TodayMenuInteractor>();
todayMenuInteractor.Initialize(TodayMenuEventType.Remove);
@ -59,7 +59,7 @@ private async void Initialize()
{
var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayDrinkContent);
var slot = go.GetComponent<ItemSlotUi>();
slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
var todayMenuInteractor = go.GetComponent<TodayMenuInteractor>();
todayMenuInteractor.Initialize(TodayMenuEventType.Remove);
@ -93,14 +93,14 @@ private void UpdateView()
var model = ItemViewModelFactory.CreateByRecipeId(recipeId);
var foodSlot = _foodSlots[foodIndex];
foodSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
_ = foodSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
foodSlot.SetCount(kvp.Value);
foodIndex++;
}
for (int i = foodIndex; i < _foodSlots.Count; i++)
{
_foodSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
_ = _foodSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe));
}
int drinkIndex = 0;
@ -112,14 +112,14 @@ private void UpdateView()
var model = ItemViewModelFactory.CreateByRecipeId(recipeId);
var drinkSlot = _drinkSlots[drinkIndex];
drinkSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
_ = drinkSlot.Initialize(model, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
drinkSlot.SetCount(kvp.Value);
drinkIndex++;
}
for (int i = drinkIndex; i < _drinkSlots.Count; i++)
{
_drinkSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
_ = _drinkSlots[i].Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe));
}
}
}