forked from Deadcows/MyBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyDelayedActions.cs
83 lines (70 loc) · 2.09 KB
/
MyDelayedActions.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
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
namespace MyBox
{
public static class MyDelayedActions
{
/// <summary>
/// Invoke Action on Delay
/// </summary>
public static Coroutine DelayedAction(float waitSeconds, Action action, bool unscaled = false)
{
return DelayedActionCoroutine(waitSeconds, action, unscaled).StartCoroutine();
}
/// <summary>
/// Invoke Action next frame
/// </summary>
public static void DelayedAction(Action action)
{
Coroutine().StartCoroutine();
IEnumerator Coroutine()
{
yield return null;
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(DelayedActionCoroutine(waitSeconds, action, unscaled));
}
/// <summary>
/// Invoke Action next frame
/// </summary>
public static Coroutine DelayedAction(this MonoBehaviour invoker, Action action)
{
return invoker.StartCoroutine(Coroutine());
IEnumerator Coroutine()
{
yield return null;
action?.Invoke();
}
}
/// <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));
}
private static IEnumerator DelayedActionCoroutine(float waitSeconds, Action action, bool unscaled = false)
{
if (unscaled) yield return new WaitForSecondsRealtime(waitSeconds);
else yield return new WaitForSeconds(waitSeconds);
if (action != null) action.Invoke();
}
}
}