'Is it possible to load resources in Unity with async/await?
Is it possible in Unity to do something like this?
private async void LoadSomethingAsync()
{
AudioClip clip = await Resources.LoadAsync<AudioClip>("path");
}
I know I could use Unitys Coroutines instead, but they have multiple downsides why I would like to use something like the code above.
On the internet I didn't found something that works for me either, so maybe someone of you knows if something like the code above is possible or not.
Solution 1:[1]
You can use Resources.LoadAsync(path) method for asynchronously loads an asset stored at path in a Resources folder.
The following code is an example to load an audio clip from the resource folder asynchronously,
private IEnumerator LoadAudioAsync(string path) {
ResourceRequest resourceRequest = Resources.LoadAsync<AudioClip>(path);
yield return resourceRequest;
AudioClip audioClip = resourceRequest.asset as AudioClip;
}
Use can also use Addressables to load the resources. But it supports only Unity 2019.3+ versions.
public async void LoadAudioAsync(string path) {
AsyncOperationHandle<AudioClip> resourceRequest = Addressables.LoadAssetAsync<AudioClip>(path);
await resourceRequest.Task;
}
For more examples: https://csharp.hotexamples.com/examples/UnityEngine/ResourceRequest/-/php-resourcerequest-class-examples.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
