UniTask官方使用方法

Getting started

// extension awaiter/methods can be used by this namespace
using Cysharp.Threading.Tasks;

// You can return type as struct UniTask(or UniTask), it is unity specialized lightweight alternative of Task
// zero allocation and fast excution for zero overhead async/await integrate with Unity
async UniTask DemoAsync()
{
    // You can await Unity's AsyncObject
    var asset = await Resources.LoadAsync("foo");
    var txt = (await UnityWebRequest.Get("https://...").SendWebRequest()).downloadHandler.text;
    await SceneManager.LoadSceneAsync("scene2");

    // .WithCancellation enables Cancel, GetCancellationTokenOnDestroy synchornizes with lifetime of GameObject
    var asset2 = await Resources.LoadAsync("bar").WithCancellation(this.GetCancellationTokenOnDestroy());

    // .ToUniTask accepts progress callback(and all opti

你可能感兴趣的:(Unity进阶,unity,UniTask)