-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathWaitForBackgroundThread.cs
More file actions
36 lines (30 loc) · 1.11 KB
/
WaitForBackgroundThread.cs
File metadata and controls
36 lines (30 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using UnityEngine;
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()
{
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();
}
}