forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAwaitable.UnityEvents.cs
More file actions
72 lines (70 loc) · 2.57 KB
/
Copy pathAwaitable.UnityEvents.cs
File metadata and controls
72 lines (70 loc) · 2.57 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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine.Events;
namespace UnityEngine
{
public static class UnityEventAwaitableExtensions
{
public static Awaitable.Awaiter GetAwaiter(this UnityEvent ev)
{
var awaitable = Awaitable.NewManagedAwaitable();
UnityAction cb = null;
cb = () =>
{
ev.RemoveListener(cb);
awaitable.RaiseManagedCompletion();
};
ev.AddListener(cb);
return awaitable.GetAwaiter();
}
public static Awaitable<T>.Awaiter GetAwaiter<T>(this UnityEvent<T> ev)
{
var awaitable = Awaitable<T>.GetManaged();
UnityAction<T> cb = null;
cb = (T arg) =>
{
ev.RemoveListener(cb);
awaitable.SetResultAndRaiseContinuation(arg);
};
ev.AddListener(cb);
return awaitable.GetAwaiter();
}
public static Awaitable<(T0, T1)>.Awaiter GetAwaiter<T0, T1>(this UnityEvent<T0, T1> ev)
{
var awaitable = Awaitable<(T0, T1)>.GetManaged();
UnityAction<T0,T1> cb = null;
cb = (T0 arg0, T1 arg1) =>
{
ev.RemoveListener(cb);
awaitable.SetResultAndRaiseContinuation((arg0, arg1));
};
ev.AddListener(cb);
return awaitable.GetAwaiter();
}
public static Awaitable<(T0, T1, T2)>.Awaiter GetAwaiter<T0, T1, T2>(this UnityEvent<T0, T1, T2> ev)
{
var awaitable = Awaitable<(T0, T1, T2)>.GetManaged();
UnityAction<T0, T1, T2> cb = null;
cb = (T0 arg0, T1 arg1, T2 arg2) =>
{
ev.RemoveListener(cb);
awaitable.SetResultAndRaiseContinuation((arg0, arg1, arg2));
};
ev.AddListener(cb);
return awaitable.GetAwaiter();
}
public static Awaitable<(T0, T1, T2, T3)>.Awaiter GetAwaiter<T0, T1, T2, T3>(this UnityEvent<T0, T1, T2, T3> ev)
{
var awaitable = Awaitable<(T0, T1, T2, T3)>.GetManaged();
UnityAction<T0, T1, T2, T3> cb = null;
cb = (T0 arg0, T1 arg1, T2 arg2, T3 arg3) =>
{
ev.RemoveListener(cb);
awaitable.SetResultAndRaiseContinuation((arg0, arg1, arg2, arg3));
};
ev.AddListener(cb);
return awaitable.GetAwaiter();
}
}
}