4. Reactive Extensions for Unity
https://github.com/neuecc/UniRx/
async/await(UniTask)
async UniTask<string> DemoAsync()
{
// You can await Unity's AsyncObject
var asset = await Resources.LoadAsync<TextAsset>("foo");
// .ConfigureAwait accepts progress callback
await SceneManager.LoadSceneAsync("scene2").ConfigureAwai
// await frame-based operation(you can also await frame c
await UniTask.Delay(TimeSpan.FromSeconds(3));
// like 'yield return WaitForEndOfFrame', or Rx's Observe
await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
// You can await standard task
await Task.Run(() => 100);
// get async webrequest
async UniTask<string> GetTextAsync(UnityWebRequest req)
{
var op = await req.SendWebRequest();
return op.downloadHandler.text;
}
var task1 = GetTextAsync(UnityWebRequest.Get("http://goog
var task2 = GetTextAsync(UnityWebRequest.Get("http://bing
var task3 = GetTextAsync(UnityWebRequest.Get("http://yaho
// concurrent async-wait and get result easily by tuple s
var (google, bing, yahoo) = await UniTask.WhenAll(task1,
// You can handle timeout easily
await GetTextAsync(UnityWebRequest.Get("http://unity.com"
10. 非同期を同期的に扱う仕組み
static string GetSync(int page)
{
try
{
var url = "http://...?page=" + page;
var html = GetHttpStringSync(url);
return html;
}
catch
{
return "Error";
}
}
static async Task<string> GetAsync(int page)
{
try
{
var url = "http://...?page=" + page;
var html = await GetHttpStringAsync(url);
return html;
}
catch
{
return "Error";
}
}
11. Synchronous Asynchronous
Single(1)Multiple(*)
var x = f(); var x = await f();
var query = from person in sequence
where person.Age >= 20
select person.Name;
foreach (var item in query)
{
OnNext(item);
}
var query = from person in sequence
where person.Age >= 20
select person.Name;
query.Subscribe(item =>
{
OnNext(item);
});
IEnumerble<T> IObservable<T>
T Task<T>
44. public enum AwaiterStatus
{
/// <summary>The operation has not yet completed.</summary>
Pending = 0,
/// <summary>The operation completed successfully.</summary>
Succeeded = 1,
/// <summary>The operation completed with an error.</summary>
Faulted = 2,
/// <summary>The operation completed due to cancellation.</summary>
Canceled = 3
}
ValueTaskSourceStatusに合わせています。Taskは本当
に不要なゴミが多くて……)
45. public async UniTask<int> FooAsync()
{
await UniTask.Yield();
return 10;
}
public enum AwaiterStatus
{
/// <summary>The operation has not yet completed.</summary>
Pending = 0,
/// <summary>The operation completed successfully.</summary>
Succeeded = 1,
/// <summary>The operation completed with an error.</summary>
Faulted = 2,
/// <summary>The operation completed due to cancellation.</summary>
Canceled = 3
}
46. public async UniTask<int> FooAsync()
{
await UniTask.Yield();
return 10;
}
public enum AwaiterStatus
{
/// <summary>The operation has not yet completed.</summary>
Pending = 0,
/// <summary>The operation completed successfully.</summary>
Succeeded = 1,
/// <summary>The operation completed with an error.</summary>
Faulted = 2,
/// <summary>The operation completed due to cancellation.</summary>
Canceled = 3
}
47. public async UniTask<int> FooAsync()
{
await UniTask.Yield();
return 10;
}
public enum AwaiterStatus
{
/// <summary>The operation has not yet completed.</summary>
Pending = 0,
/// <summary>The operation completed successfully.</summary>
Succeeded = 1,
/// <summary>The operation completed with an error.</summary>
Faulted = 2,
/// <summary>The operation completed due to cancellation.</summary>
Canceled = 3
}
48. public async UniTask<int> FooAsync()
{
await UniTask.Yield();
throw new System.Exception("Error");
}
public enum AwaiterStatus
{
/// <summary>The operation has not yet completed.</summary>
Pending = 0,
/// <summary>The operation completed successfully.</summary>
Succeeded = 1,
/// <summary>The operation completed with an error.</summary>
Faulted = 2,
/// <summary>The operation completed due to cancellation.</summary>
Canceled = 3
}
49. public async UniTask<int> FooAsync()
{
await UniTask.Yield();
throw new System.Exception("Error");
}
public enum AwaiterStatus
{
/// <summary>The operation has not yet completed.</summary>
Pending = 0,
/// <summary>The operation completed successfully.</summary>
Succeeded = 1,
/// <summary>The operation completed with an error.</summary>
Faulted = 2,
/// <summary>The operation completed due to cancellation.</summary>
Canceled = 3
}
50. public async UniTask<int> FooAsync()
{
await UniTask.Yield();
throw new OperationCanceledException();
}
public enum AwaiterStatus
{
/// <summary>The operation has not yet completed.</summary>
Pending = 0,
/// <summary>The operation completed successfully.</summary>
Succeeded = 1,
/// <summary>The operation completed with an error.</summary>
Faulted = 2,
/// <summary>The operation completed due to cancellation.</summary>
Canceled = 3
}
51. public async UniTask<int> FooAsync()
{
await UniTask.Yield();
throw new OperationCanceledException();
}
public async UniTask<int> BarAsync()
{
var x = await FooAsync();
return x * 2;
}
public void Baz()
{
BarAsync().Forget();
}
52. public async UniTask<int> FooAsync()
{
await UniTask.Yield();
throw new OperationCanceledException();
}
public async UniTask<int> BarAsync()
{
var x = await FooAsync();
return x * 2;
}
public void Baz()
{
BarAsync().Forget();
}
53. public async UniTask<int> BarAsync()
{
try
{
var x = await FooAsync();
return x * 2;
}
catch (Exception ex) when (!(ex is OperationCanceledException))
{
return -1;
}
}
54. public async UniTask<int> BarAsync()
{
try
{
var x = await FooAsync();
return x * 2;
}
catch (Exception ex) when (!(ex is OperationCanceledException))
{
// なんか復旧不能な例外なのでダイアログ出してタイトルに戻る的なことをするとして
DialogService.ShowReturnToTitleAsync().Forget(); // fire and forget的に処理
// 元の呼びもとにはキャンセルの連鎖扱いで全てすっ飛ばして終了させる
throw new OperationCanceledException();
}
}