-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainThreadUniTask.cs
52 lines (46 loc) · 1.48 KB
/
MainThreadUniTask.cs
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#if HAVE_UNITASK
using System;
using Cysharp.Threading.Tasks;
namespace Gilzoide.MainThreadTask
{
public static class MainThreadUniTask
{
public static void Run(Action action, PlayerLoopTiming timing = PlayerLoopTiming.Update)
{
if (action != null)
{
UniTask.Post(action, timing);
}
}
public static async UniTask RunAsync(Action action, PlayerLoopTiming timing = PlayerLoopTiming.Update)
{
if (action != null)
{
await UniTask.Yield(timing);
action.Invoke();
}
}
public static async UniTask<T> RunAsync<T>(Func<T> func, PlayerLoopTiming timing = PlayerLoopTiming.Update)
{
if (func == null)
{
return default;
}
await UniTask.Yield(timing);
return func.Invoke();
}
public static void InvokeOnMainThread(this Action action, PlayerLoopTiming timing = PlayerLoopTiming.Update)
{
Run(action, timing);
}
public static UniTask InvokeOnMainThreadAsync(this Action action, PlayerLoopTiming timing = PlayerLoopTiming.Update)
{
return RunAsync(action, timing);
}
public static UniTask<T> InvokeOnMainThreadAsync<T>(this Func<T> func, PlayerLoopTiming timing = PlayerLoopTiming.Update)
{
return RunAsync(func, timing);
}
}
}
#endif