load text asset

This commit is contained in:
Le Duc Anh
2025-12-25 10:06:21 +07:00
parent cd3c9d8f48
commit d4a181f379
@@ -14,6 +14,7 @@ namespace BrewMonster.Scripts
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()
@@ -23,6 +24,11 @@ namespace BrewMonster.Scripts
Addressables.InitializeAsync().Completed += OnInitializeComplete;
}
public bool IsInitialized()
{
return _isInitialized;
}
void OnInitializeComplete(AsyncOperationHandle<IResourceLocator> handle)
{
if (handle.Status == AsyncOperationStatus.Succeeded)
@@ -37,6 +43,33 @@ namespace BrewMonster.Scripts
}
}
/// <summary>
/// Load a text asset asynchronously. The address should look like this: "elements.data"
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="assetPath"></param>
/// <returns></returns>
public async Task<TextAsset> LoadAssetAsync<T>(string assetPath)
{
if (_loadedAssets.ContainsKey(assetPath))
{
return _loadedTextAssets[assetPath].Result;
}
try
{
var handle = Addressables.LoadAssetAsync<TextAsset>(assetPath);
await handle.Task;
_loadedTextAssets[assetPath] = handle;
return handle.Result;
}
catch (Exception e)
{
BMLogger.LogError(e.StackTrace);
return null;
}
}
/// <summary>
/// Load an asset asynchronously. The address should look like this: "models/npcs/npc/魅灵首领/魅灵首领/魅灵首领.prefab"
/// </summary>