forked from UnityCommunity/UnityLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonoBehaviourExtensions.cs
36 lines (31 loc) · 1.28 KB
/
MonoBehaviourExtensions.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
using System;
using System.Collections;
using UnityEngine;
namespace Sacristan.Utils.Extensions
{
public static class MonoBehaviourExtensions
{
public static void InvokeSafe(this MonoBehaviour behavior, System.Action method, float delayInSeconds)
{
behavior.StartCoroutine(InvokeSafeRoutine(method, delayInSeconds));
}
public static void InvokeRepeatingSafe(this MonoBehaviour behavior, System.Action method, float delayInSeconds, float repeatRateInSeconds)
{
behavior.StartCoroutine(InvokeSafeRepeatingRoutine(method, delayInSeconds, repeatRateInSeconds));
}
private static IEnumerator InvokeSafeRepeatingRoutine(System.Action method, float delayInSeconds, float repeatRateInSeconds)
{
yield return new WaitForSeconds(delayInSeconds);
while (true)
{
if (method != null) method.Invoke();
yield return new WaitForSeconds(repeatRateInSeconds);
}
}
private static IEnumerator InvokeSafeRoutine(System.Action method, float delayInSeconds)
{
yield return new WaitForSeconds(delayInSeconds);
if (method != null) method.Invoke();
}
}
}