Skip to content

Commit

Permalink
Add WaitForTask.
Browse files Browse the repository at this point in the history
  • Loading branch information
kring committed Sep 23, 2024
1 parent 50bf57a commit c741e27
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
25 changes: 25 additions & 0 deletions Runtime/WaitForTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using UnityEngine;

namespace CesiumForUnity
{
/// <summary>
/// A YieldInstruction that can be yielded from a coroutine in order to wait
/// until a given task completes.
/// </summary>
public class WaitForTask : CustomYieldInstruction
{
private IAsyncResult _task;

/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="task">The task to wait for.</param>
public WaitForTask(IAsyncResult task)
{
this._task = task;
}

public override bool keepWaiting => !this._task.IsCompleted;
}
}
11 changes: 11 additions & 0 deletions Runtime/WaitForTask.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 4 additions & 16 deletions Tests/TestCesium3DTileset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public IEnumerator SampleHeightMostDetailedWorksWithAnEmptyArrayOfPositions()

Task<CesiumSampleHeightResult> task = tileset.SampleHeightMostDetailed();

while (!task.IsCompleted)
{
yield return null;
}
yield return new WaitForTask(task);

CesiumSampleHeightResult result = task.Result;
Assert.IsNotNull(result);
Expand All @@ -52,10 +49,7 @@ public IEnumerator SampleHeightMostDetailedWorksWithASinglePosition()

Task<CesiumSampleHeightResult> task = tileset.SampleHeightMostDetailed(new double3(-105.1, 40.1, 1.0));

while (!task.IsCompleted)
{
yield return null;
}
yield return new WaitForTask(task);

CesiumSampleHeightResult result = task.Result;
Assert.IsNotNull(result);
Expand Down Expand Up @@ -89,10 +83,7 @@ public IEnumerator SampleHeightMostDetailedWorksWithMultiplePositions()
new double3(-105.1, 40.1, 1.0),
new double3(105.1, -40.1, 1.0));

while (!task.IsCompleted)
{
yield return null;
}
yield return new WaitForTask(task);

CesiumSampleHeightResult result = task.Result;
Assert.IsNotNull(result);
Expand Down Expand Up @@ -131,10 +122,7 @@ public IEnumerator SampleHeightMostDetailedIndicatesNotSampledForPositionOutside
// Somewhere in Sydney, not Melbourne
Task<CesiumSampleHeightResult> task = tileset.SampleHeightMostDetailed(new double3(151.20972, -33.87100, 1.0));

while (!task.IsCompleted)
{
yield return null;
}
yield return new WaitForTask(task);

CesiumSampleHeightResult result = task.Result;
Assert.IsNotNull(result);
Expand Down

0 comments on commit c741e27

Please sign in to comment.