异步加载
资源地址就是在Addressables Groups里看到的具体资源的地址,只要给定地址就能找到需要的资源
使用地址加载
// 参数为资源地址,包含后缀.prefab,handle.Result就是实例化的物体,而不是prefab
Addressables.InstantiateAsync("address of prefab");
// 参数为资源地址,包含资源后缀名
Addressables.LoadAssetAsync("address of asset");
使用直接资源引用AssetReference加载
AssetReference asset;
// 参数为资源地址,包含后缀.prefab
Addressables.InstantiateAsync(asset);
// 参数为资源地址,包含资源后缀名
Addressables.LoadAssetAsync(asset);
异步回调
匿名委托
Addressables.InstantiateAsync($"address of prefab").Completed += (handle) =>
{
if (handle.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded)
{
var obj = handle.Result;
}
}
回调函数
Addressables.InstantiateAsync($"address of prefab").Completed += OnCompleted;
private void OnCompleted(AsyncOperationHandle<SceneInstance> handle)
{
if (handle.Status == UnityEngine.ResourceManagement.AsyncOperations.AsyncOperationStatus.Succeeded)
{
var obj = handle.Result;
}
}
async
public static async void PlaySoundEffect(AssetReference audioClipAsset)
{
var handle = audioClipAsset.LoadAssetAsync<AudioClip>();
await handle.Task;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
var audioClip = handle.Result;
}
}
async Task
private async static Task<T> LoadAssetAsync<T>() where T : Object
{
var path =