-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniTaskExamples.cs
113 lines (93 loc) · 3.89 KB
/
UniTaskExamples.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
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
105
106
107
108
109
110
111
112
113
using Cysharp.Threading.Tasks;
using Cysharp.Threading.Tasks.Linq;
using System.Threading;
using UnityEngine;
public class UniTaskExamples
{
// AsyncReactiveProperty (same as UnRx Reactive Properties)
AsyncReactiveProperty<bool> reactivePropertyBoolRx = new AsyncReactiveProperty<bool>(false);
void SubscribeToReactiveProperty()
{
reactivePropertyBoolRx.Where(_boolRx => _boolRx == true).Subscribe(_ => Debug.Log("Reactive Property is true"));
}
void ChangeReactiveProperty()
{
reactivePropertyBoolRx.Value = !reactivePropertyBoolRx.Value;
}
#region Periodic
// Do an action every X time.
CancellationTokenSource cts = new CancellationTokenSource();
void StartPeriodTask()
{
cts = new CancellationTokenSource();
PeriodicTask(1f, cts.Token).Forget();
}
void CancelPeriodTask()
{
cts.Cancel();
cts.Dispose();
}
private async UniTaskVoid PeriodicTask(float seconds, CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
await UniTask.Delay(TimeSpan.FromSeconds(seconds), cancellationToken: cancellationToken);
// Call your method here.
Debug.Log("Call method here");
}
}
#endregion
#region EveryFrames
// UniTaskAsyncEnumberal (same as UniRx Observable)
int frameNumber = 1000;
CancellationTokenSource cts = new CancellationTokenSource();
void TakeOnEveryFrameNumber()
{
UniTaskAsyncEnumerable.IntervalFrame(frameNumber).TakeUntilCanceled(cts.Token).Subscribe(_ => Debug.Log("Take"));
}
void StopTakeOnEveryFrameNumber()
{
cts.Cancel();
cts.Dispose();
}
void StartEveryFrames()
{
CancellationTokenSource cts = new CancellationTokenSource(); // Create a new CTS
IntervalFrame(cts.Token).Forget(); // Fire and forget a UniTask with cancellation token. Forget() suppresses warnings.
//cts.Cancel(); // Cancel the UniTask to stop it firing
EveryUpdate(cts.Token).Forget(); // Fire every frame
FireOnceTimer(cts.Token).Forget(); // Fire after X seconds
}
async UniTask IntervalFrame(CancellationToken token) // IntervalFrame is similar to Update, but fires every X frames
{
await foreach (var _ in UniTaskAsyncEnumerable.IntervalFrame(100).WithCancellation(token)) // Set frame count in IntervalFrame()
{
Debug.Log("Interval frame: " + Time.frameCount);
}
}
async UniTask EveryUpdate(CancellationToken token) // EveryUpdate is basically a cancellable Update(). It fires every frame.
{
await foreach (var _ in UniTaskAsyncEnumerable.EveryUpdate().WithCancellation(token)) // Set frame count in IntervalFrame()
{
Debug.Log("Every Update: " + Time.frameCount);
}
}
async UniTask FireOnceTimer(CancellationToken token)
{
await foreach (var _ in UniTaskAsyncEnumerable.Timer(TimeSpan.FromSeconds(3)).WithCancellation(token)) // Fire once after 3 seconds
{
Debug.Log("Timer: " + Time.frameCount);
}
}
#endregion
#region Delay
async void UniTaskDelay(int milliseconds)
{
CancellationTokenSource cts = new CancellationTokenSource();
Debug.Log("Delay started");
//cts.Cancel(); // To cancel the delay. Note that the method will continue, so "Delay completed" will still be printed.
await UniTask.Delay(milliseconds, cancellationToken: cts.Token); // Similar to DelayFrame
Debug.Log("Delay completed");
}
#endregion
}