fix addressable release asset
This commit is contained in:
@@ -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<string, AsyncOperationHandle<GameObject>> _loadedAssets = new();
|
||||
public event Action OnDispose;
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
@@ -63,10 +65,71 @@ namespace BrewMonster.Scripts
|
||||
/// <summary>
|
||||
/// When the asset is no longer needed, call this method to unload it.
|
||||
/// </summary>
|
||||
/// <param name="assetPath"></param>
|
||||
public void UnloadAsset(string assetPath)
|
||||
/// <param name="assetPath">The asset path used when loading the asset</param>
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release a specific asset by its handle directly.
|
||||
/// </summary>
|
||||
/// <param name="handle">The async operation handle to release</param>
|
||||
public void ReleaseAsset(AsyncOperationHandle<GameObject> handle)
|
||||
{
|
||||
if (handle.IsValid())
|
||||
{
|
||||
Addressables.Release(handle);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release all loaded assets from the cache.
|
||||
/// </summary>
|
||||
public void ReleaseAllAssets()
|
||||
{
|
||||
foreach (var kvp in _loadedAssets)
|
||||
{
|
||||
if (kvp.Value.IsValid())
|
||||
{
|
||||
Addressables.Release(kvp.Value);
|
||||
}
|
||||
}
|
||||
_loadedAssets.Clear();
|
||||
BMLogger.Log("AddressableManager: Released all assets");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check if an asset is currently loaded in the cache.
|
||||
/// </summary>
|
||||
/// <param name="assetPath">The asset path to check</param>
|
||||
/// <returns>True if the asset is loaded</returns>
|
||||
public bool IsAssetLoaded(string assetPath)
|
||||
{
|
||||
return _loadedAssets.ContainsKey(assetPath) && _loadedAssets[assetPath].IsValid();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the count of currently loaded assets.
|
||||
/// </summary>
|
||||
public int LoadedAssetCount => _loadedAssets.Count;
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
OnDispose?.Invoke();
|
||||
ReleaseAllAssets();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,6 @@ namespace BrewMonster.Managers
|
||||
private void OnDestroy()
|
||||
{
|
||||
EC_ManMessage.Dispose();
|
||||
CECGameRun.Dispose();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
|
||||
@@ -44,7 +44,7 @@ namespace BrewMonster.PerfectWorld.Scripts.Vfx
|
||||
{
|
||||
if(!string.IsNullOrEmpty(_vfxPath))
|
||||
{
|
||||
AddressableManager.Instance.UnloadAsset(_vfxPath);
|
||||
AddressableManager.Instance.ReleaseAsset(_vfxPath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user