Files
test/Assets/PerfectWorld/Scripts/Addressable/AddressableManager.cs
T
2025-12-25 16:41:11 +07:00

268 lines
9.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.AddressableAssets.ResourceLocators;
using UnityEngine.ResourceManagement.AsyncOperations;
namespace BrewMonster.Scripts
{
public class AddressableManager : MonoSingleton<AddressableManager>
{
private bool _isInitialized = false;
private Dictionary<string, AsyncOperationHandle<GameObject>> _loadedAssets = new();
private Dictionary<string, AsyncOperationHandle<TextAsset>> _loadedTextAssets = new();
public event Action OnDispose;
protected override void Initialize()
{
base.Initialize();
_isInitialized = false;
Addressables.InitializeAsync().Completed += OnInitializeComplete;
}
public bool IsInitialized()
{
return _isInitialized;
}
void OnInitializeComplete(AsyncOperationHandle<IResourceLocator> handle)
{
if (handle.Status == AsyncOperationStatus.Succeeded)
{
_isInitialized = true;
BMLogger.Log($"AddressableManager: Initialized");
}
else
{
// print out the error
BMLogger.LogError($"AddressableManager: Failed to initialize: {handle.OperationException?.Message} {handle.OperationException?.StackTrace}");
}
}
/// <summary>
/// Load a text asset asynchronously.
/// NOTE: The key must match the Addressables "Address" (or another valid key like a label/GUID).
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="assetPath"></param>
/// <returns></returns>
public async Task<TextAsset> LoadTextAssetAsync(string assetPath)
{
if (_loadedTextAssets.ContainsKey(assetPath))
{
return _loadedTextAssets[assetPath].Result;
}
try
{
// First, try the key exactly as provided. If it doesn't exist in the catalog,
// fall back to common project conventions (e.g. full Asset path).
foreach (var key in GetCandidateKeys(assetPath))
{
var locationsHandle = Addressables.LoadResourceLocationsAsync(key, typeof(TextAsset));
await locationsHandle.Task;
if (locationsHandle.Status != AsyncOperationStatus.Succeeded || locationsHandle.Result == null || locationsHandle.Result.Count == 0)
{
Addressables.Release(locationsHandle);
continue;
}
Addressables.Release(locationsHandle);
var handle = Addressables.LoadAssetAsync<TextAsset>(key);
await handle.Task;
if (handle.Status == AsyncOperationStatus.Succeeded && handle.Result != null)
{
_loadedTextAssets[assetPath] = handle;
return handle.Result;
}
// If load failed, release and try next candidate.
if (handle.IsValid())
{
Addressables.Release(handle);
}
}
BMLogger.LogError($"AddressableManager: No Location found for TextAsset key='{assetPath}'. " +
$"Tried: {string.Join(", ", GetCandidateKeys(assetPath).Select(k => $"'{k}'"))}");
LogSimilarKeys(assetPath);
return null;
}
catch (Exception e)
{
BMLogger.LogError($"AddressableManager: Failed to load TextAsset '{assetPath}': {e}");
return null;
}
}
private static IEnumerable<string> GetCandidateKeys(string assetPath)
{
if (string.IsNullOrWhiteSpace(assetPath))
{
yield break;
}
// Exact key (what caller asked for)
yield return assetPath;
// Common fallback used by this repo's Addressables settings: full asset path address.
// Example in `Assets/AddressableAssetsData/AssetGroups/configuration.asset`:
// m_Address: Assets/Addressable/elements.txt
if (!assetPath.Contains("/") && !assetPath.Contains("\\"))
{
yield return $"Assets/Addressable/{assetPath}";
}
}
private static void LogSimilarKeys(string needle)
{
// Helpful diagnostics in dev builds: show a few keys containing the substring.
try
{
var lower = needle?.ToLowerInvariant();
if (string.IsNullOrEmpty(lower))
{
return;
}
const int max = 20;
var matches = new List<string>(max);
foreach (var locator in Addressables.ResourceLocators)
{
foreach (var keyObj in locator.Keys)
{
if (keyObj is not string keyStr)
{
continue;
}
if (!keyStr.ToLowerInvariant().Contains(lower))
{
continue;
}
matches.Add(keyStr);
if (matches.Count >= max)
{
goto Done;
}
}
}
Done:
if (matches.Count > 0)
{
BMLogger.LogWarning($"AddressableManager: Similar Addressables keys for '{needle}': {string.Join(", ", matches)}");
}
}
catch
{
// ignore diagnostics failures
}
}
/// <summary>
/// Load an asset asynchronously. The address should look like this: "models/npcs/npc/魅灵首领/魅灵首领/魅灵首领.prefab"
/// </summary>
/// <returns></returns>
public async Task<GameObject> LoadPrefabAsync(string assetPath)
{
if (_loadedAssets.ContainsKey(assetPath))
{
return _loadedAssets[assetPath].Result;
}
try
{
var handle = Addressables.LoadAssetAsync<GameObject>(assetPath);
await handle.Task;
_loadedAssets[assetPath] = handle;
return handle.Result;
}
catch (System.Exception e)
{
BMLogger.LogError(e.StackTrace);
return null;
}
}
/// <summary>
/// When the asset is no longer needed, call this method to unload it.
/// </summary>
/// <param name="assetPath">The asset path used when loading the asset</param>
public void ReleaseAsset(string 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();
}
}
}