diff --git a/UnityProject/Assets/Plugins/AsyncAwaitUtil/Source/WaitForBackgroundThread.cs b/UnityProject/Assets/Plugins/AsyncAwaitUtil/Source/WaitForBackgroundThread.cs index a02a2d7..cadd66a 100644 --- a/UnityProject/Assets/Plugins/AsyncAwaitUtil/Source/WaitForBackgroundThread.cs +++ b/UnityProject/Assets/Plugins/AsyncAwaitUtil/Source/WaitForBackgroundThread.cs @@ -7,8 +7,30 @@ public class WaitForBackgroundThread { + private readonly bool canFallbackToMainThread; + private readonly bool platformSupportsMultithread; + + public WaitForBackgroundThread() : this(false) { } + + public WaitForBackgroundThread(bool canFallbackToMainThread) + { + this.canFallbackToMainThread = canFallbackToMainThread; + this.platformSupportsMultithread = Application.platform != RuntimePlatform.WebGLPlayer; + } + public ConfiguredTaskAwaitable.ConfiguredTaskAwaiter GetAwaiter() { - return Task.Run(() => {}).ConfigureAwait(false).GetAwaiter(); + if (!platformSupportsMultithread && !canFallbackToMainThread) + { + throw new InvalidOperationException($"{Application.platform} does not support background threads."); + } + + return Empty().ConfigureAwait(!platformSupportsMultithread).GetAwaiter(); + } + + private static async Task Empty() + { + // Task.Run does not run on WebGL, so we use this async method instead. + await Task.Yield(); } }