diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TemporarySynchronizationContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TemporarySynchronizationContext.cs new file mode 100644 index 000000000..836edeeaa --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TemporarySynchronizationContext.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; + +namespace UniGLTF +{ + /// + /// await で post される先を1次的に乗っ取る + /// + public class TemporarySynchronizationContext : SynchronizationContext + { + Queue m_callbacks = new Queue(); + + public override void Post(SendOrPostCallback d, object state) + { + m_callbacks.Enqueue(() => d(state)); + } + + public void ExecuteSync() + { + while (m_callbacks.Count > 0) + { + var callback = m_callbacks.Dequeue(); + callback(); + } + } + + public IEnumerator AsCoroutine() + { + throw new NotImplementedException(); + } + + public Task AsTask() + { + // repost to SynchronizationContext.Current + throw new NotImplementedException(); + } + + 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 new file mode 100644 index 000000000..c96a371af --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TemporarySynchronizationContext.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 62f8f380aadfc284f9bdc2d58f84bf48 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: