-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathTaskManager.cs
191 lines (166 loc) · 3.83 KB
/
TaskManager.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/// TaskManager.cs
///
/// This is a convenient coroutine API for Unity.
///
/// Example usage:
/// IEnumerator MyAwesomeTask()
/// {
/// while(true) {
/// // ...
/// yield return null;
//// }
/// }
///
/// IEnumerator TaskKiller(float delay, Task t)
/// {
/// yield return new WaitForSeconds(delay);
/// t.Stop();
/// }
///
/// // From anywhere
/// Task my_task = new Task(MyAwesomeTask());
/// new Task(TaskKiller(5, my_task));
///
/// The code above will schedule MyAwesomeTask() and keep it running
/// concurrently until either it terminates on its own, or 5 seconds elapses
/// and triggers the TaskKiller Task that was created.
///
/// Note that to facilitate this API's behavior, a "TaskManager" GameObject is
/// created lazily on first use of the Task API and placed in the scene root
/// with the internal TaskManager component attached. All coroutine dispatch
/// for Tasks is done through this component.
using UnityEngine;
using System.Collections;
/// A Task object represents a coroutine. Tasks can be started, paused, and stopped.
/// It is an error to attempt to start a task that has been stopped or which has
/// naturally terminated.
public class Task
{
/// Returns true if and only if the coroutine is running. Paused tasks
/// are considered to be running.
public bool Running {
get {
return task.Running;
}
}
/// Returns true if and only if the coroutine is currently paused.
public bool Paused {
get {
return task.Paused;
}
}
/// Delegate for termination subscribers. manual is true if and only if
/// the coroutine was stopped with an explicit call to Stop().
public delegate void FinishedHandler(bool manual);
/// Termination event. Triggered when the coroutine completes execution.
public event FinishedHandler Finished;
/// Creates a new Task object for the given coroutine.
///
/// If autoStart is true (default) the task is automatically started
/// upon construction.
public Task(IEnumerator c, bool autoStart = true)
{
task = TaskManager.CreateTask(c);
task.Finished += TaskFinished;
if(autoStart)
Start();
}
/// Begins execution of the coroutine
public void Start()
{
task.Start();
}
/// Discontinues execution of the coroutine at its next yield.
public void Stop()
{
task.Stop();
}
public void Pause()
{
task.Pause();
}
public void Unpause()
{
task.Unpause();
}
void TaskFinished(bool manual)
{
FinishedHandler handler = Finished;
if(handler != null)
handler(manual);
}
TaskManager.TaskState task;
}
class TaskManager : MonoBehaviour
{
public class TaskState
{
public bool Running {
get {
return running;
}
}
public bool Paused {
get {
return paused;
}
}
public delegate void FinishedHandler(bool manual);
public event FinishedHandler Finished;
IEnumerator coroutine;
bool running;
bool paused;
bool stopped;
public TaskState(IEnumerator c)
{
coroutine = c;
}
public void Pause()
{
paused = true;
}
public void Unpause()
{
paused = false;
}
public void Start()
{
running = true;
singleton.StartCoroutine(CallWrapper());
}
public void Stop()
{
stopped = true;
running = false;
}
IEnumerator CallWrapper()
{
yield return null;
IEnumerator e = coroutine;
while(running) {
if(paused)
yield return null;
else {
if(e != null && e.MoveNext()) {
yield return e.Current;
}
else {
running = false;
}
}
}
FinishedHandler handler = Finished;
if(handler != null)
handler(stopped);
}
}
static TaskManager singleton;
public static TaskState CreateTask(IEnumerator coroutine)
{
if(singleton == null) {
GameObject go = new GameObject("TaskManager");
singleton = go.AddComponent<TaskManager>();
}
return new TaskState(coroutine);
}
}