diff --git a/src/UniTask/Assets/Editor/PackageExporter.cs b/src/UniTask/Assets/Editor/PackageExporter.cs index d1e8327..396373f 100644 --- a/src/UniTask/Assets/Editor/PackageExporter.cs +++ b/src/UniTask/Assets/Editor/PackageExporter.cs @@ -12,7 +12,7 @@ public static class PackageExporter public static void Export() { // configure - var root = "UniTask"; + var root = "Plugins/UniTask"; var exportPath = "./UniTask.unitypackage"; var path = Path.Combine(Application.dataPath, root); diff --git a/src/UniTask/Assets/Plugins/UniTask/CompilerServices/AsyncUniTaskMethodBuilder.cs b/src/UniTask/Assets/Plugins/UniTask/CompilerServices/AsyncUniTaskMethodBuilder.cs index 15925db..320261e 100644 --- a/src/UniTask/Assets/Plugins/UniTask/CompilerServices/AsyncUniTaskMethodBuilder.cs +++ b/src/UniTask/Assets/Plugins/UniTask/CompilerServices/AsyncUniTaskMethodBuilder.cs @@ -136,19 +136,31 @@ namespace Cysharp.Threading.Tasks.CompilerServices // don't use boxed stateMachine. } +#if DEBUG || !UNITY_2018_3_OR_NEWER + + object id; + // 9. For Debugger Attach - [DebuggerHidden] public object ObjectIdForDebugger { get { + if (id == null) + { + id = new object(); + } + return id; + /* if (promise == null) { promise = AutoResetUniTaskCompletionSource.Create(); } return promise; + */ } } + +#endif } [StructLayout(LayoutKind.Auto)] @@ -281,18 +293,30 @@ namespace Cysharp.Threading.Tasks.CompilerServices // don't use boxed stateMachine. } +#if DEBUG || !UNITY_2018_3_OR_NEWER + + object id; + // 9. For Debugger Attach - [DebuggerHidden] public object ObjectIdForDebugger { get { + if (id == null) + { + id = new object(); + } + return id; + /* if (promise == null) { promise = AutoResetUniTaskCompletionSource.Create(); } return promise; + */ } } + +#endif } } \ No newline at end of file diff --git a/src/UniTask/Assets/Plugins/UniTask/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs b/src/UniTask/Assets/Plugins/UniTask/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs index fd89d2f..8ae9807 100644 --- a/src/UniTask/Assets/Plugins/UniTask/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs +++ b/src/UniTask/Assets/Plugins/UniTask/CompilerServices/AsyncUniTaskVoidMethodBuilder.cs @@ -106,7 +106,6 @@ namespace Cysharp.Threading.Tasks.CompilerServices object id; // 9. For Debugger Attach - [DebuggerHidden] public object ObjectIdForDebugger { get diff --git a/src/UniTask/Assets/Plugins/UniTask/External/AddressableAsyncExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/External/AddressableAsyncExtensions.cs index fbf8336..934e58f 100644 --- a/src/UniTask/Assets/Plugins/UniTask/External/AddressableAsyncExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/External/AddressableAsyncExtensions.cs @@ -1,4 +1,6 @@ -#if UNITASK_ADDRESSABLE_SUPPORT +// asmdef Version Defines, enabled when com.unity.addressables is imported. + +#if UNITASK_ADDRESSABLE_SUPPORT using Cysharp.Threading.Tasks.Internal; using System; diff --git a/src/UniTask/Assets/Plugins/UniTask/Triggers/AsyncTriggerBase.cs b/src/UniTask/Assets/Plugins/UniTask/Triggers/AsyncTriggerBase.cs index d959dca..a3415a8 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Triggers/AsyncTriggerBase.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Triggers/AsyncTriggerBase.cs @@ -356,11 +356,11 @@ namespace Cysharp.Threading.Tasks.Triggers } else { - if (handlers != null) + if (handlers == null) { handlers = new List>(); - handlers.Add(handler); } + handlers.Add(handler); } } diff --git a/src/UniTask/Assets/Plugins/UniTask/Triggers/AsyncTriggerExtensions.cs b/src/UniTask/Assets/Plugins/UniTask/Triggers/AsyncTriggerExtensions.cs index 49bc5d2..49e7f0b 100644 --- a/src/UniTask/Assets/Plugins/UniTask/Triggers/AsyncTriggerExtensions.cs +++ b/src/UniTask/Assets/Plugins/UniTask/Triggers/AsyncTriggerExtensions.cs @@ -31,11 +31,18 @@ namespace Cysharp.Threading.Tasks.Triggers static T GetOrAddComponent(GameObject gameObject) where T : Component { +#if UNITY_2019_2_OR_NEWER + if (!gameObject.TryGetComponent(out var component)) + { + component = gameObject.AddComponent(); + } +#else var component = gameObject.GetComponent(); if (component == null) { component = gameObject.AddComponent(); } +#endif return component; } diff --git a/src/UniTask/Assets/Scenes/SandboxMain.cs b/src/UniTask/Assets/Scenes/SandboxMain.cs index a9b144c..b7fcafb 100644 --- a/src/UniTask/Assets/Scenes/SandboxMain.cs +++ b/src/UniTask/Assets/Scenes/SandboxMain.cs @@ -1,4 +1,5 @@ using System; +using Cysharp.Threading.Tasks.Triggers; using System.Collections; using System.Collections.Generic; using System.IO; @@ -38,7 +39,19 @@ public class SandboxMain : MonoBehaviour CancellationTokenSource cts; UniTaskCompletionSource ucs; + async UniTask FooAsync() + { + // use F10, will crash. + var loop = int.Parse("9"); + await UniTask.DelayFrame(loop); + Debug.Log("OK"); + await UniTask.DelayFrame(loop); + + Debug.Log("Again"); + + return 10; + } async UniTask RunStandardDelayAsync() @@ -57,7 +70,7 @@ public class SandboxMain : MonoBehaviour var scheduled = job.Schedule(); - + UnityEngine.Debug.Log("OK"); @@ -76,7 +89,7 @@ public class SandboxMain : MonoBehaviour ShowPlayerLoop.DumpPlayerLoop("Current", playerLoop); - RunStandardDelayAsync().Forget(); + //RunStandardDelayAsync().Forget(); //for (int i = 0; i < 14; i++) //{ @@ -89,7 +102,10 @@ public class SandboxMain : MonoBehaviour // ----- - RunJobAsync().Forget(); + // RunJobAsync().Forget(); + + //ClickOnce().Forget(); + //ClickForever().Forget(); //var cor = UniTask.ToCoroutine(async () => // { @@ -102,26 +118,101 @@ public class SandboxMain : MonoBehaviour //StartCoroutine(cor); + //this.TryGetComponent( + + + CancellationTokenSource cts = new CancellationTokenSource(); + + var trigger = this.GetAsyncUpdateTrigger(); + Go(trigger, cts.Token).Forget(); + //Go(trigger).Forget(); + //Go(trigger).Forget(); + + Application.logMessageReceived += Application_logMessageReceived; - ucs = new UniTaskCompletionSource(); + //ucs = new UniTaskCompletionSource(); - okButton.onClick.AddListener(async () => + //okButton.onClick.AddListener(async () => + //{ + // await InnerAsync(false); + //}); + + okButton.onClick.AddListener(() => { - await InnerAsync(false); + FooAsync().Forget(); }); - cancelButton.onClick.AddListener(async () => + cancelButton.onClick.AddListener(() => { text.text = ""; // ucs.TrySetResult(); - await ucs.Task; + cts.Cancel(); }); } + async UniTaskVoid Go(AsyncUpdateTrigger trigger, CancellationToken ct) + { + await UniTask.Yield(PlayerLoopTiming.LastPostLateUpdate); + UnityEngine.Debug.Log("AWAIT BEFO:" + Time.frameCount); + var handler = trigger.GetUpdateAsyncHandler(ct); + + try + { + while (true) + { + await handler.UpdateAsync(); + } + } + finally + { + UnityEngine.Debug.Log("AWAIT END:" + Time.frameCount); + } + } + + async UniTaskVoid ClickOnce() + { + try + { + await okButton.OnClickAsync(); + UnityEngine.Debug.Log("CLICKED ONCE"); + } + catch (Exception ex) + { + UnityEngine.Debug.Log(ex.ToString()); + } + finally + { + UnityEngine.Debug.Log("END ONCE"); + } + } + + async UniTaskVoid ClickForever() + { + try + { + using (var handler = okButton.GetAsyncClickEventHandler()) + { + while (true) + { + await handler.OnClickAsync(); + UnityEngine.Debug.Log("Clicked"); + } + } + } + catch (Exception ex) + { + UnityEngine.Debug.Log(ex.ToString()); + } + finally + { + UnityEngine.Debug.Log("END"); + } + } + async UniTask SimpleAwait() { await UniTask.Yield(); @@ -174,7 +265,10 @@ public class SandboxMain : MonoBehaviour private void Application_logMessageReceived(string condition, string stackTrace, LogType type) { - text.text += "\n" + condition; + if (text != null) + { + text.text += "\n" + condition; + } } async UniTask OuterAsync(bool b) diff --git a/src/UniTask/UniTask.unitypackage b/src/UniTask/UniTask.unitypackage new file mode 100644 index 0000000..39a9043 Binary files /dev/null and b/src/UniTask/UniTask.unitypackage differ