forked from muckSponge/UnityAsync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwait.cs
More file actions
104 lines (89 loc) · 5.09 KB
/
Await.cs
File metadata and controls
104 lines (89 loc) · 5.09 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Runtime.CompilerServices;
using System.Threading;
namespace UnityAsync
{
public static partial class Await
{
static readonly AwaitInstructionAwaiter<WaitForFrames> nextUpdate = new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(1));
static readonly AwaitInstructionAwaiter<WaitForFrames> nextLateUpdate = new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(1), FrameScheduler.LateUpdate);
static readonly AwaitInstructionAwaiter<WaitForFrames> nextFixedUpdate = new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(1), FrameScheduler.FixedUpdate);
/// <summary>
/// Quick access to Unity's <see cref="System.Threading.SynchronizationContext"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static SynchronizationContext UnitySyncContext() => AsyncManager.UnitySyncContext;
/// <summary>
/// Quick access to the background <see cref="System.Threading.SynchronizationContext"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static SynchronizationContext BackgroundSyncContext() => AsyncManager.BackgroundSyncContext;
/// <summary>
/// Convenience function to skip a single frame, equivalent to Unity's <c>yield return null</c>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static AwaitInstructionAwaiter<WaitForFrames> NextUpdate() => nextUpdate;
/// <summary>
/// Convenience function to skip a number of frames, equivalent to multiple <c>yield return null</c>s.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static AwaitInstructionAwaiter<WaitForFrames> Updates(int count) => new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(count));
/// <summary>
/// Convenience function to skip a single frame and continue in the LateUpdate loop.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static AwaitInstructionAwaiter<WaitForFrames> NextLateUpdate() => nextLateUpdate;
/// <summary>
/// Convenience function to skip multiple frames and continue in the LateUpdate loop.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static AwaitInstructionAwaiter<WaitForFrames> LateUpdates(int count) => new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(count), FrameScheduler.LateUpdate);
/// <summary>
/// Convenience function to skip a single FixedUpdate frame and continue in the FixedUpdate loop. Equivalent to
/// Unity's <see cref="UnityEngine.WaitForFixedUpdate"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static AwaitInstructionAwaiter<WaitForFrames> NextFixedUpdate() => nextFixedUpdate;
/// <summary>
/// Convenience function to skip multiple FixedUpdate frames and continue in the FixedUpdate loop.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static AwaitInstructionAwaiter<WaitForFrames> FixedUpdates(int count) => new AwaitInstructionAwaiter<WaitForFrames>(new WaitForFrames(count), FrameScheduler.FixedUpdate);
/// <summary>
/// Convenience function to wait for a number of in-game seconds before continuing, equivalent to Unity's
/// <see cref="UnityEngine.WaitForSeconds"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static WaitForSeconds Seconds(float duration) => new WaitForSeconds(duration);
/// <summary>
/// Convenience function to wait for a number of unscaled seconds before continuing. Equivalent to Unity's
/// <see cref="UnityEngine.WaitForSecondsRealtime"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static WaitForSecondsRealtime SecondsRealtime(float duration) => new WaitForSecondsRealtime(duration);
/// <summary>
/// Convenience function to wait for a condition to return true. Equivalent to Unity's
/// <see cref="UnityEngine.WaitUntil"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static WaitUntil Until(Func<bool> condition) => new WaitUntil(condition);
/// <summary>
/// Convenience function to wait for a condition to return false. Equivalent to Unity's
/// <see cref="UnityEngine.WaitWhile"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static WaitWhile While(Func<bool> condition) => new WaitWhile(condition);
/// <summary>
/// Convenience function to wait for a condition to return true. Equivalent to Unity's
/// <see cref="UnityEngine.WaitUntil"/> but state is passed to avoid closure.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static WaitUntil<TState> Until<TState>(TState state, Func<TState, bool> condition) => new WaitUntil<TState>(state, condition);
/// <summary>
/// Convenience function to wait for a condition to return false. Equivalent to Unity's
/// <see cref="UnityEngine.WaitWhile"/> but state is passed to avoid closure.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static WaitWhile<TState> While<TState>(TState state, Func<TState, bool> condition) => new WaitWhile<TState>(state, condition);
}
}