Unity3d 返回值的异步方法

async-await 异步功能
开启方法:

Unity 2017,及以上版本(Edit -> Project Settings -> Player-> Scripting Runtime Version”-> “Experimental (.NET 4.6 Equivalent).

第一种方法:
IEnumerator CustomCoroutineWithReturnValue()
{
yield return new WaitForSeconds(1.0f);
yield return "asdf";
}
public async void Start()
{
// You can also get the final yielded value from the coroutine
var value = (string)(await CustomCoroutineWithReturnValue());
// value is equal to "asdf" here
}
第二种方法:返回值的异步方法,我们使用通用版本的Task
async Task GetAssetBundle(string url)
{
return (await new WWW(url)).assetBundle
}
public async void Start()
{
var assetBundle = await GetAssetBundle("www.my-server.com/myfile");
}


你可能感兴趣的:(unity3d)