diff --git a/UnityGLTF/Assets/UnityGLTF/Scripts/Async/AsyncCoroutineHelper.cs b/UnityGLTF/Assets/UnityGLTF/Scripts/Async/AsyncCoroutineHelper.cs index 8e304ea2e..d1ea3437b 100644 --- a/UnityGLTF/Assets/UnityGLTF/Scripts/Async/AsyncCoroutineHelper.cs +++ b/UnityGLTF/Assets/UnityGLTF/Scripts/Async/AsyncCoroutineHelper.cs @@ -2,95 +2,38 @@ using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; + namespace UnityGLTF { - public interface IAsyncCoroutineHelper - { - Task RunAsTask(IEnumerator coroutine, string name); - Task YieldOnTimeout(); - } - - public class AsyncCoroutineHelper : MonoBehaviour, IAsyncCoroutineHelper - { - public float BudgetPerFrameInSeconds = 0.01f; - - private Queue _actions = new Queue(); - private WaitForEndOfFrame _waitForEndOfFrame = new WaitForEndOfFrame(); - private float _timeout; - - public Task RunAsTask(IEnumerator coroutine, string name) - { - TaskCompletionSource tcs = new TaskCompletionSource(); - lock (_actions) - { - _actions.Enqueue( - new CoroutineInfo - { - Coroutine = coroutine, - Tcs = tcs, - Name = name - } - ); - } - - return tcs.Task; - } - - public async Task YieldOnTimeout() - { - if (Time.realtimeSinceStartup > _timeout) - { - await RunAsTask(EmptyYieldEnum(), nameof(EmptyYieldEnum)); - } - } - - private void Start() - { - _timeout = Time.realtimeSinceStartup + BudgetPerFrameInSeconds; - } - - private void Update() - { - StartCoroutine(ResetFrameTimeout()); - - CoroutineInfo ? coroutineInfo = null; - - lock (_actions) - { - if (_actions.Count > 0) - { - coroutineInfo = _actions.Dequeue(); - } - } - - if (coroutineInfo != null) - { - StartCoroutine(CallMethodOnMainThread(coroutineInfo.Value)); - } - } - - private IEnumerator CallMethodOnMainThread(CoroutineInfo coroutineInfo) - { - yield return coroutineInfo.Coroutine; - coroutineInfo.Tcs.SetResult(true); - } - - private IEnumerator EmptyYieldEnum() - { - yield break; - } - - private IEnumerator ResetFrameTimeout() - { - yield return _waitForEndOfFrame; - _timeout = Time.realtimeSinceStartup + BudgetPerFrameInSeconds; - } - - private struct CoroutineInfo - { - public IEnumerator Coroutine; - public TaskCompletionSource Tcs; - public string Name; - } - } + public class AsyncCoroutineHelper : MonoBehaviour + { + public float BudgetPerFrameInSeconds = 0.01f; + + private WaitForEndOfFrame _waitForEndOfFrame = new WaitForEndOfFrame(); + private float _timeout; + + public async Task YieldOnTimeout() + { + if (Time.realtimeSinceStartup > _timeout) + { + await Task.Delay(1); + } + } + + private void Start() + { + _timeout = Time.realtimeSinceStartup + BudgetPerFrameInSeconds; + + StartCoroutine(ResetFrameTimeout()); + } + + private IEnumerator ResetFrameTimeout() + { + while (true) + { + yield return _waitForEndOfFrame; + _timeout = Time.realtimeSinceStartup + BudgetPerFrameInSeconds; + } + } + } } diff --git a/UnityGLTF/Assets/UnityGLTF/Scripts/GLTFSceneImporter.cs b/UnityGLTF/Assets/UnityGLTF/Scripts/GLTFSceneImporter.cs index 0fc527cef..97e18a499 100644 --- a/UnityGLTF/Assets/UnityGLTF/Scripts/GLTFSceneImporter.cs +++ b/UnityGLTF/Assets/UnityGLTF/Scripts/GLTFSceneImporter.cs @@ -136,7 +136,7 @@ protected struct GLBStream public long StartPosition; } - protected IAsyncCoroutineHelper _asyncCoroutineHelper; + protected AsyncCoroutineHelper _asyncCoroutineHelper; protected GameObject _lastLoadedScene; protected readonly GLTFMaterial DefaultMaterial = new GLTFMaterial();