forked from Deadcows/MyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyCoroutine.cs
109 lines (88 loc) · 2.78 KB
/
MyCoroutine.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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public static class MyCoroutine
{
#region Parallel group management
/// <summary>
/// Add coroutine to a parallel group and start it
/// </summary>
public static void AsParallelGroup(this IEnumerator coroutine, MonoBehaviour parent, string groupName)
{
if (!Runners.ContainsKey(groupName))
Runners.Add(groupName, 0);
Runners[groupName]++;
parent.StartCoroutine(DoParallel(coroutine, parent, groupName));
}
/// <summary>
/// Get amount of coroutines, that is processing (not finished) in group
/// </summary>
public static int GroupProcessing(string groupName)
{
return Runners.ContainsKey(groupName) ? Runners[groupName] : 0;
}
/// <summary>
/// Wait till all coroutines in group is finisged
/// </summary>
public static IEnumerator WaitGroupProcessed(string groupName)
{
while (GroupProcessing(groupName) > 0)
yield return null;
}
private static readonly Dictionary<string, int> Runners = new Dictionary<string, int>();
private static IEnumerator DoParallel(IEnumerator coroutine, MonoBehaviour parent, string groupName)
{
yield return parent.StartCoroutine(coroutine);
Runners[groupName]--;
}
#endregion
/// <summary>
/// Invoke Action on Delay
/// </summary>
public static IEnumerator DelayedAction(float waitSeconds, Action action, bool unscaled = false)
{
if (unscaled) yield return new WaitForUnscaledSeconds(waitSeconds);
else yield return new WaitForSeconds(waitSeconds);
action?.Invoke();
}
/// <summary>
/// Invoke Action on Delay
/// </summary>
public static Coroutine DelayedAction(this MonoBehaviour invoker, float waitSeconds, Action action, bool unscaled = false)
{
return invoker.StartCoroutine(DelayedAction(waitSeconds, action, unscaled));
}
/// <summary>
/// Invoke Action next frame
/// </summary>
public static IEnumerator DelayedAction(Action action)
{
yield return null;
action?.Invoke();
}
/// <summary>
/// Invoke Action next frame
/// </summary>
public static Coroutine DelayedAction(this MonoBehaviour invoker, Action action)
{
return invoker.StartCoroutine(DelayedAction(action));
}
/// <summary>
/// Set GO as selected next frame (EventSystem.current.SetSelectedGameObject)
/// </summary>
public static IEnumerator DelayedUiSelection(GameObject objectToSelect)
{
yield return null;
EventSystem.current.SetSelectedGameObject(null);
EventSystem.current.SetSelectedGameObject(objectToSelect);
}
/// <summary>
/// Set GO as selected next frame (EventSystem.current.SetSelectedGameObject)
/// </summary>
public static Coroutine DelayedUiSelection(this MonoBehaviour invoker, GameObject objectToSelect)
{
return invoker.StartCoroutine(DelayedUiSelection(objectToSelect));
}
}