This repository has been archived by the owner on Dec 20, 2020. It is now read-only.
/
BetterCoroutinesExtensions.cs
108 lines (101 loc) · 4.92 KB
/
BetterCoroutinesExtensions.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
using System.Collections;
using Archon.SwissArmyLib.Events.Loops;
using Archon.SwissArmyLib.Utils;
using UnityEngine;
namespace Archon.SwissArmyLib.Coroutines
{
/// <summary>
/// A bunch of helpful extensions for starting and stopping coroutines.
/// </summary>
public static class BetterCoroutinesExtensions
{
/// <summary>
/// Starts a new coroutine.
/// </summary>
/// <param name="unityObject"></param>
/// <param name="enumerator"></param>
/// <param name="updateLoop">Which update loop should the coroutine be part of?</param>
/// <returns>The id of the coroutine.</returns>
public static int StartBetterCoroutine(this Object unityObject, IEnumerator enumerator,
UpdateLoop updateLoop = UpdateLoop.Update)
{
return BetterCoroutines.Start(enumerator, updateLoop);
}
/// <summary>
/// Starts a new coroutine.
/// </summary>
/// <param name="unityObject"></param>
/// <param name="enumerator"></param>
/// <param name="updateLoopId">Which update loop should the coroutine be part of?</param>
/// <returns>The id of the coroutine.</returns>
public static int StartBetterCoroutine(this Object unityObject, IEnumerator enumerator,
int updateLoopId)
{
return BetterCoroutines.Start(enumerator, updateLoopId);
}
/// <summary>
/// Starts a new coroutine with its lifetime linked to this component.
/// The coroutine will be stopped when the linked component is disabled or destroyed.
/// </summary>
/// <param name="monoBehaviour">The component to link the coroutine to.</param>
/// <param name="enumerator"></param>
/// <param name="updateLoop">Which update loop should the coroutine be part of?</param>
/// <returns>The id of the coroutine.</returns>
public static int StartBetterCoroutineLinked(this MonoBehaviour monoBehaviour, IEnumerator enumerator,
UpdateLoop updateLoop = UpdateLoop.Update)
{
return BetterCoroutines.Start(enumerator, monoBehaviour, updateLoop);
}
/// <summary>
/// Starts a new coroutine with its lifetime linked to this component.
/// The coroutine will be stopped when the linked component is disabled or destroyed.
/// </summary>
/// <param name="monoBehaviour">The component to link the coroutine to.</param>
/// <param name="enumerator"></param>
/// <param name="updateLoopId">Which update loop should the coroutine be part of?</param>
/// <returns>The id of the coroutine.</returns>
public static int StartBetterCoroutineLinked(this MonoBehaviour monoBehaviour, IEnumerator enumerator,
int updateLoopId)
{
return BetterCoroutines.Start(enumerator, monoBehaviour, updateLoopId);
}
/// <summary>
/// Starts a new coroutine with its lifetime linked to this gameobject.
/// The coroutine will be stopped when the linked gameobject is disabled or destroyed.
/// </summary>
/// <param name="gameObject">The gameobject to link the coroutine to.</param>
/// <param name="enumerator"></param>
/// <param name="updateLoop">Which update loop should the coroutine be part of?</param>
/// <returns>The id of the coroutine.</returns>
public static int StartBetterCoroutineLinked(this GameObject gameObject, IEnumerator enumerator,
UpdateLoop updateLoop = UpdateLoop.Update)
{
return BetterCoroutines.Start(enumerator, gameObject, updateLoop);
}
/// <summary>
/// Starts a new coroutine with its lifetime linked to this gameobject.
/// The coroutine will be stopped when the linked gameobject is disabled or destroyed.
/// </summary>
/// <param name="gameObject">The gameobject to link the coroutine to.</param>
/// <param name="enumerator"></param>
/// <param name="updateLoopId">Which update loop should the coroutine be part of?</param>
/// <returns>The id of the coroutine.</returns>
public static int StartBetterCoroutineLinked(this GameObject gameObject, IEnumerator enumerator,
int updateLoopId)
{
return BetterCoroutines.Start(enumerator, gameObject, updateLoopId);
}
/// <summary>
/// Stops a running coroutine prematurely.
///
/// This will stop any child coroutines as well.
/// </summary>
/// <param name="unityObject"></param>
/// <param name="coroutineId">The id of the coroutine to stop.</param>
/// <returns>True if the coroutine was found and stopped, otherwise false.</returns>
public static bool StopBetterCoroutine(this Object unityObject, int coroutineId)
{
return BetterCoroutines.Stop(coroutineId);
}
}
}