86 lines
3.0 KiB
C#
86 lines
3.0 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using UnityEngine.ResourceManagement.AsyncOperations;
|
|
using ZXK.Framework;
|
|
/*******************************************************************************
|
|
*Create By CG
|
|
*Function Addressables加载释放管理
|
|
*******************************************************************************/
|
|
namespace ZXK.UTility
|
|
{
|
|
public class AddressablesManager : MonoSingleton<AddressablesManager>
|
|
{
|
|
/// <summary>
|
|
/// 通过Addressables加载资源
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="address"></param>
|
|
/// <param name="onLoaded"></param>
|
|
public void LoadAsset<T>(string address,System.Action<T> onLoaded) where T : Object
|
|
{
|
|
StartCoroutine(LoadAssetCoroutine(address, onLoaded));
|
|
}
|
|
/// <summary>
|
|
/// 通过标签加载所有相关资源
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="lable"></param>
|
|
/// <param name="onLoaded"></param>
|
|
public void LoadAssets<T>(string lable, System.Action<List<GameObject>> onLoaded) where T : Object
|
|
{
|
|
StartCoroutine(LoadAssetsCoroutine<T>(lable, onLoaded));
|
|
}
|
|
/// <summary>
|
|
/// 通过资源路径加载资源
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="address"></param>
|
|
/// <param name="onLoaded"></param>
|
|
/// <returns></returns>
|
|
private IEnumerator LoadAssetCoroutine<T>(string address, System.Action<T> onLoaded) where T : Object
|
|
{
|
|
AsyncOperationHandle<T> handle = Addressables.LoadAssetAsync<T>(address);
|
|
yield return handle;
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
|
{
|
|
onLoaded?.Invoke(handle.Result);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"资源加载失败:{address}");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 通过标签加载所有相关资源
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="lable"></param>
|
|
/// <param name="onLoaded"></param>
|
|
/// <returns></returns>
|
|
private IEnumerator LoadAssetsCoroutine<T>(string lable, System.Action<List<GameObject>> onLoaded) where T : Object
|
|
{
|
|
AsyncOperationHandle<IList<T>> handle = Addressables.LoadAssetsAsync<T>(lable,null);
|
|
yield return handle;
|
|
if (handle.Status == AsyncOperationStatus.Succeeded)
|
|
{
|
|
onLoaded?.Invoke(handle.Result as List<GameObject>);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogError($"资源加载失败:{lable}");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 卸载资源
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="asset"></param>
|
|
public void ReleaseAsset<T>(T asset) where T : Object
|
|
{
|
|
Addressables.Release(asset);
|
|
}
|
|
}
|
|
} |