diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs index d994e6ec5..4ed58c24b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContextExtensions.cs @@ -1,4 +1,7 @@ +using System; +using System.Collections.Generic; using System.IO; +using System.Threading; using System.Threading.Tasks; namespace UniGLTF @@ -27,22 +30,71 @@ namespace UniGLTF self.Root.name = Path.GetFileNameWithoutExtension(path); } + class TemporarySynchronizationContext : SynchronizationContext + { + Queue m_callbacks = new Queue(); + + /// + /// await して中断された継続がここにくる。 + /// + /// ex. await Task.Run(() =>{ /* ここ */ }); + /// + /// + /// + public override void Post(SendOrPostCallback d, object state) + { + // Debug.Log($"Post"); + m_callbacks.Enqueue(() => d(state)); + } + + /// + /// 一個デキューして実行する。 + /// Unityであれば毎フレームこのように消化しているであろう。 + /// + /// https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Scripting/UnitySynchronizationContext.cs + /// + /// + public bool ExecuteOneCallback() + { + while (m_callbacks.Count == 0) + { + // Debug.Log($"empty callbacks"); + return false; + } + + var callback = m_callbacks.Dequeue(); + callback(); + return true; + } + } + /// /// Build unity objects from parsed gltf /// public static void Load(this ImporterContext self) { var tcs = new TemporarySynchronizationContext(); - using (tcs.Hijack()) + + // 元に戻すためバックアップ + var backup = SynchronizationContext.Current; + // 一時的に変更 + SynchronizationContext.SetSynchronizationContext(tcs); + try { var task = self.LoadAsync(); + // 中断された await を消化する while (!task.IsCompleted) { // execute synchronous tcs.ExecuteOneCallback(); } } + finally + { + // 元に戻す + SynchronizationContext.SetSynchronizationContext(backup); + } } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TemporarySynchronizationContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TemporarySynchronizationContext.cs deleted file mode 100644 index 1e9a9c3cf..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TemporarySynchronizationContext.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Threading; -using System.Threading.Tasks; -using UnityEngine; - -namespace UniGLTF -{ - /// - /// await で post される先を1次的に乗っ取る - /// - public class TemporarySynchronizationContext : SynchronizationContext - { - Queue m_callbacks = new Queue(); - - public override void Post(SendOrPostCallback d, object state) - { - // Debug.Log($"Post"); - m_callbacks.Enqueue(() => d(state)); - } - - public bool ExecuteOneCallback() - { - while (m_callbacks.Count == 0) - { - // Debug.Log($"empty callbacks"); - return false; - } - - var callback = m_callbacks.Dequeue(); - callback(); - return true; - } - - class Hijacker : IDisposable - { - SynchronizationContext m_backup; - public Hijacker(SynchronizationContext context) - { - m_backup = SynchronizationContext.Current; - SynchronizationContext.SetSynchronizationContext(context); - } - - public void Dispose() - { - SynchronizationContext.SetSynchronizationContext(m_backup); - } - } - - public IDisposable Hijack() - { - return new Hijacker(this); - } - } -} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TemporarySynchronizationContext.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TemporarySynchronizationContext.cs.meta deleted file mode 100644 index c96a371af..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TemporarySynchronizationContext.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 62f8f380aadfc284f9bdc2d58f84bf48 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: