Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix loading time issue #414

Merged
merged 3 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 32 additions & 89 deletions UnityGLTF/Assets/UnityGLTF/Scripts/Async/AsyncCoroutineHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoroutineInfo> _actions = new Queue<CoroutineInfo>();
private WaitForEndOfFrame _waitForEndOfFrame = new WaitForEndOfFrame();
private float _timeout;

public Task RunAsTask(IEnumerator coroutine, string name)
{
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
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<bool> 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;
}
}
}
}
2 changes: 1 addition & 1 deletion UnityGLTF/Assets/UnityGLTF/Scripts/GLTFSceneImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down