From dccb90f9f5ca0b14ebb2cb236315d0f1bf562f49 Mon Sep 17 00:00:00 2001 From: Brice Date: Thu, 16 May 2019 15:42:08 +0200 Subject: [PATCH] Exception and fallback for WaitForBackgroundThread on WebGL --- .../Source/WaitForBackgroundThread.cs | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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(); } }