在 Unity 中可以做這樣的事情嗎?
private async void LoadSomethingAsync()
{
AudioClip clip = await Resources.LoadAsync<AudioClip>("path");
}
我知道我可以改用 Unitys Coroutines,但它們有多個缺點,為什么我想使用類似上面的代碼。
在互聯網上我也沒有找到適合我的東西,所以也許你們中的一些人知道上面的代碼是否可能。
uj5u.com熱心網友回復:
我從未使用過 Addressables,但這里有一些相關檔案:
Addressables also supports asynchronous await through the AsyncOperationHandle.Task property:
public async Start() { AsyncOperationHandle<Texture2D> handle = Addressables.LoadAssetAsync<Texture2D>("mytexture"); await handle.Task; // The task is complete. Be sure to check the Status is successful before storing the Result. }
所以對我來說,你似乎可以切換到使用 Addressables,然后這樣做:
private async void LoadSomethingAsync()
{
AsyncOperationHandle<AudioClip> handle = Addressables.LoadAssetAsync<AudioClip>("path");
await handle.Task;
if (handle.Status == AsyncOperationStatus.Succeeded)
{
AudioClip clip = handle.Result;
// use clip here
}
else
{
// handle loading error
}
}
請注意,根據檔案,此特定解決方案在 WebGL 上不可用。這對您來說可能無關緊要:
該
AsyncOperationHandle.Task屬性在 WebGL 上不可用,因為該平臺不支持多執行緒操作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/428502.html
