diff --git a/Scripts/Format/VRMImporter.cs b/Scripts/Format/VRMImporter.cs index ab6eab8d1..033f59914 100644 --- a/Scripts/Format/VRMImporter.cs +++ b/Scripts/Format/VRMImporter.cs @@ -6,6 +6,9 @@ using UniGLTF; using System.Collections.Generic; using System.Collections; using UniTask; +#if (NET_4_6 && UNITY_2018_1_OR_NEWER) +using System.Threading.Tasks; +#endif namespace VRM @@ -524,32 +527,26 @@ namespace VRM } #if (NET_4_6 && UNITY_2018_1_OR_NEWER) - /// - /// Gets an awaiter that returns the loaded GameObject. - /// - public static Schedulable LoadVrmAsync(string path) + + public static Task LoadVrmAsync(string path) { var context = new VRMImporterContext(path); context.ParseVrm(File.ReadAllBytes(path)); - return LoadVrmAsyncInternal(context); + return LoadVrmAsyncInternal(context).ToTask(); } - /// - /// Gets an awaiter that returns the loaded GameObject. - /// - public static Schedulable LoadVrmAsync(Byte[] bytes) + + public static Task LoadVrmAsync(Byte[] bytes) { var context = new VRMImporterContext(); context.ParseVrm(bytes); return LoadVrmAsync(context); } - /// - /// Gets an awaiter that returns the loaded GameObject. - /// - public static Schedulable LoadVrmAsync(VRMImporterContext ctx) + + public static Task LoadVrmAsync(VRMImporterContext ctx) { - return LoadVrmAsyncInternal(ctx); + return LoadVrmAsyncInternal(ctx).ToTask(); } #endif diff --git a/Scripts/UniTask/Schedulable.cs b/Scripts/UniTask/Schedulable.cs index 8f751d3a6..bb0237693 100644 --- a/Scripts/UniTask/Schedulable.cs +++ b/Scripts/UniTask/Schedulable.cs @@ -2,8 +2,7 @@ using System.Collections; using System.Collections.Generic; #if (NET_4_6 && UNITY_2018_1_OR_NEWER) -using System.Threading; -using System.Runtime.CompilerServices; +using System.Threading.Tasks; #endif namespace UniTask @@ -186,9 +185,16 @@ namespace UniTask } #if (NET_4_6 && UNITY_2018_1_OR_NEWER) - public static SchedulableAwaiter GetAwaiter(this Schedulable schedulable) + public static Task ToTask(this Schedulable schedulable) { - return new SchedulableAwaiter(schedulable, true); + return ToTask(schedulable, Scheduler.MainThread); + } + + public static Task ToTask(this Schedulable schedulable, IScheduler scheduler) + { + var tcs = new TaskCompletionSource(); + schedulable.Subscribe(scheduler, r => tcs.SetResult(r), ex => tcs.SetException(ex)); + return tcs.Task; } #endif @@ -233,57 +239,5 @@ namespace UniTask }, scheduler); } */ -#if (NET_4_6 && UNITY_2018_1_OR_NEWER) - public class SchedulableAwaiter : INotifyCompletion - { - private readonly Schedulable target = null; - - private T result; - - private readonly bool continueOnCapturedContext; - - public T GetResult() - { - return result; - } - - public bool IsCompleted { get; private set; } - - public SchedulableAwaiter(Schedulable target) - { - this.target = target; - continueOnCapturedContext = true; - } - - public SchedulableAwaiter(Schedulable target, bool continueOnCapturedContext) - { - this.target = target; - this.continueOnCapturedContext = continueOnCapturedContext; - } - - public void OnCompleted(Action continuation) - { - var context = SynchronizationContext.Current; - if (continueOnCapturedContext && context != null) - { - target.Subscribe(target.Parent.Schedulder, r => - { - result = r; - IsCompleted = true; - context.Post(_ => continuation(), null); - }, ex => { throw ex; }); - } - else - { - target.Subscribe(target.Parent.Schedulder, r => - { - result = r; - IsCompleted = true; - continuation(); - }, ex => { throw ex; }); - } - } - } -#endif } }