diff --git a/Assets/_DDD/_Addressables/Prefabs/Uis/ItemSlotUi.prefab b/Assets/_DDD/_Addressables/Prefabs/Uis/ItemSlotUi.prefab index 9558b01c4..82281c06c 100644 --- a/Assets/_DDD/_Addressables/Prefabs/Uis/ItemSlotUi.prefab +++ b/Assets/_DDD/_Addressables/Prefabs/Uis/ItemSlotUi.prefab @@ -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 diff --git a/Assets/_DDD/_Scripts/AssetManagement/AssetManager.cs b/Assets/_DDD/_Scripts/AssetManagement/AssetManager.cs index 81789ebea..77529b4e7 100644 --- a/Assets/_DDD/_Scripts/AssetManagement/AssetManager.cs +++ b/Assets/_DDD/_Scripts/AssetManagement/AssetManager.cs @@ -15,6 +15,15 @@ namespace DDD { public class AssetManager : Singleton, IManager { + private static readonly Dictionary _cachedHandles = new(); + + protected override void OnApplicationQuit() + { + base.OnApplicationQuit(); + + ReleaseAllCached(); + } + public void PreInit() { @@ -32,31 +41,26 @@ public void PostInit() public static async Task LoadAsset(string key) where T : UnityEngine.Object { - var handle = Addressables.LoadAssetAsync(key); - await handle.Task; - - if (handle.Status == AsyncOperationStatus.Succeeded) - return handle.Result; - - Debug.LogError($"Addressable load failed : {key}"); - return null; - } - - public static async Task LoadAsset(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(); - await handle.Task; + // ✅ 새로 로드 + var newHandle = Addressables.LoadAssetAsync(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 diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventorySlotUiStrategy.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventorySlotUiStrategy.cs index 7c7708100..31e81f536 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventorySlotUiStrategy.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventorySlotUiStrategy.cs @@ -1,6 +1,5 @@ using System.Threading.Tasks; using UnityEngine; -using UnityEngine.AddressableAssets; namespace DDD { diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventoryView.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventoryView.cs index f34cc4179..ca36603b0 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventoryView.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/InventoryUi/InventoryView.cs @@ -11,7 +11,7 @@ public class InventoryView : MonoBehaviour, IEventHandler [SerializeField] private Transform _slotParent; private RestaurantManagementSo _restaurantManagementSo; - private InventoryCategoryType _currenInventoryCategoryType = InventoryCategoryType.None; + private InventoryCategoryType _currenInventoryCategoryType = InventoryCategoryType.Food; private readonly Dictionary _slotLookup = new(); private GameObject _firstSlot; @@ -47,7 +47,7 @@ public async Task Initialize() { var itemSlotUi = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _slotParent); var slot = itemSlotUi.GetComponent(); - slot.Initialize(model, new InventorySlotUiStrategy()); + await slot.Initialize(model, new InventorySlotUiStrategy()); itemSlotUi.name = ItemSlotUiName + model.Id; var interactor = itemSlotUi.GetComponent(); diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemSlotUi.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemSlotUi.cs index 0d2c16e84..c4f2f064f 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemSlotUi.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/ItemUi/ItemSlotUi.cs @@ -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; diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/RestaurantManagementUi.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/RestaurantManagementUi.cs index 44568c15a..16205a5cc 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/RestaurantManagementUi.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/RestaurantManagementUi.cs @@ -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(this); diff --git a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuView.cs b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuView.cs index 544c159cd..03b6eb698 100644 --- a/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuView.cs +++ b/Assets/_DDD/_Scripts/GameUi/RestaurantManagementUi/TodayMenuUi/TodayMenuView.cs @@ -41,7 +41,7 @@ private async void Initialize() { var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayFoodContent); var slot = go.GetComponent(); - slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe)); + await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.FoodRecipe)); var todayMenuInteractor = go.GetComponent(); todayMenuInteractor.Initialize(TodayMenuEventType.Remove); @@ -59,7 +59,7 @@ private async void Initialize() { var go = Instantiate(_restaurantManagementSo.ItemSlotUiPrefab, _todayDrinkContent); var slot = go.GetComponent(); - slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe)); + await slot.Initialize(null, new TodayMenuSlotUiStrategy(RecipeType.DrinkRecipe)); var todayMenuInteractor = go.GetComponent(); 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)); } } }