UniTask/Assets/Scenes/SandboxMain.cs

124 lines
2.5 KiB
C#
Raw Normal View History

2019-05-30 18:41:23 +08:00
using System;
using System.Collections;
2019-05-19 23:14:47 +08:00
using System.Collections.Generic;
2020-04-15 14:23:23 +08:00
using System.IO;
2019-05-30 18:41:23 +08:00
using System.Threading;
2020-04-18 04:07:59 +08:00
using System.Threading.Tasks;
2019-05-19 23:14:47 +08:00
using UniRx.Async;
using UnityEngine;
2019-05-30 18:41:23 +08:00
using UnityEngine.Networking;
using UnityEngine.UI;
2019-05-19 23:14:47 +08:00
public class SandboxMain : MonoBehaviour
{
2019-05-30 18:41:23 +08:00
public Button okButton;
public Button cancelButton;
2020-04-18 04:07:59 +08:00
public Text text;
2019-05-30 18:41:23 +08:00
CancellationTokenSource cts;
2019-05-19 23:14:47 +08:00
2020-04-18 04:07:59 +08:00
UniTaskCompletionSource2 ucs;
void Start()
2019-05-19 23:14:47 +08:00
{
2020-04-18 04:07:59 +08:00
// Setup unobserverd tskexception handling
TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
// Optional: disable ExecutionContext if you don't use AsyncLocal.
//if (!ExecutionContext.IsFlowSuppressed())
//{
// ExecutionContext.SuppressFlow();
//}
//// Optional: disable SynchronizationContext(to boostup performance) if you completely use UniTask only
//SynchronizationContext.SetSynchronizationContext(null);
// -----
Application.logMessageReceived += Application_logMessageReceived;
ucs = new UniTaskCompletionSource2();
okButton.onClick.AddListener(UniTask.VoidUnityAction(async () =>
{
await OuterAsync(true);
}));
cancelButton.onClick.AddListener(async () =>
{
text.text = "";
ucs.SetResult();
await ucs.Task;
});
}
private void Application_logMessageReceived(string condition, string stackTrace, LogType type)
{
text.text += "\n" + condition;
}
async UniTask2 OuterAsync(bool b)
{
UnityEngine.Debug.Log("START OUTER");
await InnerAsync(b);
await InnerAsync(b);
UnityEngine.Debug.Log("END OUTER");
// throw new InvalidOperationException("NAZO ERROR!?"); // error!?
}
async UniTask2 InnerAsync(bool b)
{
if (b)
{
UnityEngine.Debug.Log("Start delay:" + Time.frameCount);
await UniTask2.DelayFrame(60);
UnityEngine.Debug.Log("End delay:" + Time.frameCount);
await UniTask2.DelayFrame(60);
UnityEngine.Debug.Log("Onemore end delay:" + Time.frameCount);
}
else
{
//UnityEngine.Debug.Log("Empty END");
}
}
2019-05-30 18:41:23 +08:00
2020-04-15 14:23:23 +08:00
2020-04-18 04:07:59 +08:00
private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
// e.SetObserved();
// or other custom write code.
UnityEngine.Debug.LogError("Unobserved:" + e.Exception.ToString());
2019-05-19 23:14:47 +08:00
}
}
2019-05-30 18:41:23 +08:00