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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
/*
------------------- Code Monkey -------------------
Thank you for downloading the Code Monkey Utilities
I hope you find them useful in your projects
If you have any questions use the contact form
Cheers!
unitycodemonkey.com
--------------------------------------------------
*/
using System;
using UnityEngine;
using System.Collections.Generic;
namespace CodeMonkey.Utils {
/*
* Calls function on every Update until it returns true
* */
public class FunctionUpdater {
/*
* Class to hook Actions into MonoBehaviour
* */
private class MonoBehaviourHook : MonoBehaviour {
public Action OnUpdate;
private void Update() {
if (OnUpdate != null) OnUpdate();
}
}
private static List<FunctionUpdater> updaterList; // Holds a reference to all active updaters
private static GameObject initGameObject; // Global game object used for initializing class, is destroyed on scene change
private static void InitIfNeeded() {
if (initGameObject == null) {
initGameObject = new GameObject("FunctionUpdater_Global");
updaterList = new List<FunctionUpdater>();
}
}
public static FunctionUpdater Create(Action updateFunc) {
return Create(() => { updateFunc(); return false; }, "", true, false);
}
public static FunctionUpdater Create(Action updateFunc, string functionName) {
return Create(() => { updateFunc(); return false; }, functionName, true, false);
}
public static FunctionUpdater Create(Func<bool> updateFunc) {
return Create(updateFunc, "", true, false);
}
public static FunctionUpdater Create(Func<bool> updateFunc, string functionName) {
return Create(updateFunc, functionName, true, false);
}
public static FunctionUpdater Create(Func<bool> updateFunc, string functionName, bool active) {
return Create(updateFunc, functionName, active, false);
}
public static FunctionUpdater Create(Func<bool> updateFunc, string functionName, bool active, bool stopAllWithSameName) {
InitIfNeeded();
if (stopAllWithSameName) {
StopAllUpdatersWithName(functionName);
}
GameObject gameObject = new GameObject("FunctionUpdater Object " + functionName, typeof(MonoBehaviourHook));
FunctionUpdater functionUpdater = new FunctionUpdater(gameObject, updateFunc, functionName, active);
gameObject.GetComponent<MonoBehaviourHook>().OnUpdate = functionUpdater.Update;
updaterList.Add(functionUpdater);
return functionUpdater;
}
private static void RemoveUpdater(FunctionUpdater funcUpdater) {
InitIfNeeded();
updaterList.Remove(funcUpdater);
}
public static void DestroyUpdater(FunctionUpdater funcUpdater) {
InitIfNeeded();
if (funcUpdater != null) {
funcUpdater.DestroySelf();
}
}
public static void StopUpdaterWithName(string functionName) {
InitIfNeeded();
for (int i = 0; i < updaterList.Count; i++) {
if (updaterList[i].functionName == functionName) {
updaterList[i].DestroySelf();
return;
}
}
}
public static void StopAllUpdatersWithName(string functionName) {
InitIfNeeded();
for (int i = 0; i < updaterList.Count; i++) {
if (updaterList[i].functionName == functionName) {
updaterList[i].DestroySelf();
i--;
}
}
}
private GameObject gameObject;
private string functionName;
private bool active;
private Func<bool> updateFunc; // Destroy Updater if return true;
public FunctionUpdater(GameObject gameObject, Func<bool> updateFunc, string functionName, bool active) {
this.gameObject = gameObject;
this.updateFunc = updateFunc;
this.functionName = functionName;
this.active = active;
}
public void Pause() {
active = false;
}
public void Resume() {
active = true;
}
private void Update() {
if (!active) return;
if (updateFunc()) {
DestroySelf();
}
}
public void DestroySelf() {
RemoveUpdater(this);
if (gameObject != null) {
UnityEngine.Object.Destroy(gameObject);
}
}
}
}
|