From e973260dee66d08d8b53628e8069f0a0ff56b3ef Mon Sep 17 00:00:00 2001 From: NguyenVanDat Date: Mon, 8 Dec 2025 09:48:37 +0700 Subject: [PATCH] fix addressable release asset --- .../Scripts/Addressable/AddressableManager.cs | 69 ++++++++++++++++++- .../Scripts/Network/EC_ManMessageMono.cs | 1 - .../PerfectWorld/Scripts/Vfx/BaseVfxObject.cs | 2 +- Assets/Scripts/CECGameRun.cs | 12 ++-- 4 files changed, 73 insertions(+), 11 deletions(-) diff --git a/Assets/PerfectWorld/Scripts/Addressable/AddressableManager.cs b/Assets/PerfectWorld/Scripts/Addressable/AddressableManager.cs index b3bea36612..1dda787681 100644 --- a/Assets/PerfectWorld/Scripts/Addressable/AddressableManager.cs +++ b/Assets/PerfectWorld/Scripts/Addressable/AddressableManager.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; @@ -13,6 +14,7 @@ namespace BrewMonster.Scripts private bool _isInitialized = false; private Dictionary> _loadedAssets = new(); + public event Action OnDispose; protected override void Initialize() { @@ -63,10 +65,71 @@ namespace BrewMonster.Scripts /// /// When the asset is no longer needed, call this method to unload it. /// - /// - public void UnloadAsset(string assetPath) + /// The asset path used when loading the asset + public void ReleaseAsset(string assetPath) { - Addressables.Release(assetPath); + if (_loadedAssets.TryGetValue(assetPath, out var handle)) + { + if (handle.IsValid()) + { + Addressables.Release(handle); + } + _loadedAssets.Remove(assetPath); + BMLogger.Log($"AddressableManager: Released asset: {assetPath}"); + } + else + { + BMLogger.LogWarning($"AddressableManager: Asset not found in cache: {assetPath}"); + } + } + + /// + /// Release a specific asset by its handle directly. + /// + /// The async operation handle to release + public void ReleaseAsset(AsyncOperationHandle handle) + { + if (handle.IsValid()) + { + Addressables.Release(handle); + } + } + + /// + /// Release all loaded assets from the cache. + /// + public void ReleaseAllAssets() + { + foreach (var kvp in _loadedAssets) + { + if (kvp.Value.IsValid()) + { + Addressables.Release(kvp.Value); + } + } + _loadedAssets.Clear(); + BMLogger.Log("AddressableManager: Released all assets"); + } + + /// + /// Check if an asset is currently loaded in the cache. + /// + /// The asset path to check + /// True if the asset is loaded + public bool IsAssetLoaded(string assetPath) + { + return _loadedAssets.ContainsKey(assetPath) && _loadedAssets[assetPath].IsValid(); + } + + /// + /// Get the count of currently loaded assets. + /// + public int LoadedAssetCount => _loadedAssets.Count; + + private void OnDestroy() + { + OnDispose?.Invoke(); + ReleaseAllAssets(); } } } diff --git a/Assets/PerfectWorld/Scripts/Network/EC_ManMessageMono.cs b/Assets/PerfectWorld/Scripts/Network/EC_ManMessageMono.cs index e419a72a88..633e0d2414 100644 --- a/Assets/PerfectWorld/Scripts/Network/EC_ManMessageMono.cs +++ b/Assets/PerfectWorld/Scripts/Network/EC_ManMessageMono.cs @@ -46,7 +46,6 @@ namespace BrewMonster.Managers private void OnDestroy() { EC_ManMessage.Dispose(); - CECGameRun.Dispose(); } private void Update() diff --git a/Assets/PerfectWorld/Scripts/Vfx/BaseVfxObject.cs b/Assets/PerfectWorld/Scripts/Vfx/BaseVfxObject.cs index 82145416b2..a2c7b5313a 100644 --- a/Assets/PerfectWorld/Scripts/Vfx/BaseVfxObject.cs +++ b/Assets/PerfectWorld/Scripts/Vfx/BaseVfxObject.cs @@ -44,7 +44,7 @@ namespace BrewMonster.PerfectWorld.Scripts.Vfx { if(!string.IsNullOrEmpty(_vfxPath)) { - AddressableManager.Instance.UnloadAsset(_vfxPath); + AddressableManager.Instance.ReleaseAsset(_vfxPath); } } diff --git a/Assets/Scripts/CECGameRun.cs b/Assets/Scripts/CECGameRun.cs index 4c2f3ce59f..40ecf6c3bc 100644 --- a/Assets/Scripts/CECGameRun.cs +++ b/Assets/Scripts/CECGameRun.cs @@ -35,20 +35,20 @@ public partial class CECGameRun // _playerPrefab = _gameRunConfig.PlayerPrefab; // _monsterPrefab = _gameRunConfig.MonsterPrefab; // _npcServerPrefab = _gameRunConfig.NpcServerPrefab; - // _testVfxPrefab = _gameRunConfig.TestVfxPrefab; LoadPrefabs(); // LoadPrefabs(); EC_ManMessage.RegisterHandler(this); + + AddressableManager.Instance.OnDispose += Dispose; } - public static void Dispose() + private static void Dispose() { instance = null; - AddressableManager.Instance.UnloadAsset(AddressResourceConfig.PlayerPrefab); - AddressableManager.Instance.UnloadAsset(AddressResourceConfig.MonsterPrefab); - AddressableManager.Instance.UnloadAsset(AddressResourceConfig.NpcServerPrefab); - AddressableManager.Instance.UnloadAsset(AddressResourceConfig.TestVfxPrefab); + AddressableManager.Instance.ReleaseAsset(AddressResourceConfig.PlayerPrefab); + AddressableManager.Instance.ReleaseAsset(AddressResourceConfig.MonsterPrefab); + AddressableManager.Instance.ReleaseAsset(AddressResourceConfig.NpcServerPrefab); } private async void LoadPrefabs()