Subversion Repository Public Repository

Nextrek

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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//----------------------------------------------
//            NGUI: Next-Gen UI kit
// Copyright © 2011-2013 Tasharen Entertainment
//----------------------------------------------

// This class has not been needed for a very long time (ever since script execution order feature was added).
// I'm keeping it here in case you are still using it in your own projects, but note that it's no longer supported.

/*using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Update manager allows for simple programmatic ordering of update events.
/// </summary>

[ExecuteInEditMode]
[AddComponentMenu("NGUI/Internal/Update Manager")]
public class UpdateManager : MonoBehaviour
{
	public delegate void OnUpdate (float delta);

	public class UpdateEntry
	{
		public int index = 0;
		public OnUpdate func;
		public MonoBehaviour mb;
		public bool isMonoBehaviour = false;
	}

	public class DestroyEntry
	{
		public Object obj;
		public float time;
	}

	static int Compare (UpdateEntry a, UpdateEntry b)
	{
		if (a.index < b.index) return 1;
		if (a.index > b.index) return -1;
		return 0;
	}

	static UpdateManager mInst;
	List<UpdateEntry> mOnUpdate = new List<UpdateEntry>();
	List<UpdateEntry> mOnLate = new List<UpdateEntry>();
	List<UpdateEntry> mOnCoro = new List<UpdateEntry>();
	BetterList<DestroyEntry> mDest = new BetterList<DestroyEntry>();
	float mTime = 0f;

	/// <summary>
	/// Ensure that there is an instance of this class present.
	/// </summary>

	static void CreateInstance ()
	{
		if (mInst == null)
		{
			mInst = GameObject.FindObjectOfType(typeof(UpdateManager)) as UpdateManager;

			if (mInst == null && Application.isPlaying)
			{
				GameObject go = new GameObject("_UpdateManager");
				DontDestroyOnLoad(go);
				//go.hideFlags = HideFlags.HideAndDontSave;
				mInst = go.AddComponent<UpdateManager>();
			}
		}
	}

	/// <summary>
	/// Update the specified list.
	/// </summary>

	void UpdateList (List<UpdateEntry> list, float delta)
	{
		for (int i = list.Count; i > 0; )
		{
			UpdateEntry ent = list[--i];

			// If it's a monobehaviour we need to do additional checks
			if (ent.isMonoBehaviour)
			{
				// If the monobehaviour is null, remove this entry
				if (ent.mb == null)
				{
					list.RemoveAt(i);
					continue;
				}

				// If the monobehaviour or its game object are disabled, move on to the next entry
				if (!ent.mb.enabled || !NGUITools.GetActive(ent.mb.gameObject)) continue;
			}

			// Call the function
			ent.func(delta);
		}
	}

	/// <summary>
	/// Start the coroutine.
	/// </summary>

	void Start ()
	{
		if (Application.isPlaying)
		{
			mTime = Time.realtimeSinceStartup;
			StartCoroutine(CoroutineFunction());
		}
	}

	/// <summary>
	/// Don't keep this class around after stopping the Play mode.
	/// </summary>

	void OnApplicationQuit () { DestroyImmediate(gameObject); }

	/// <summary>
	/// Call all update functions.
	/// </summary>

	void Update ()
	{
		if (mInst != this) NGUITools.Destroy(gameObject);
		else UpdateList(mOnUpdate, Time.deltaTime);
	}

	/// <summary>
	/// Call all late update functions and destroy this class if no callbacks have been registered.
	/// </summary>

	void LateUpdate ()
	{
		UpdateList(mOnLate, Time.deltaTime);
		if (!Application.isPlaying) CoroutineUpdate();
	}

	/// <summary>
	/// Call all coroutine update functions and destroy all delayed destroy objects.
	/// </summary>

	bool CoroutineUpdate ()
	{
		float time = Time.realtimeSinceStartup;
		float delta = time - mTime;
		if (delta < 0.001f) return true;

		mTime = time;

		UpdateList(mOnCoro, delta);

		bool appIsPlaying = Application.isPlaying;

		for (int i = mDest.size; i > 0; )
		{
			DestroyEntry de = mDest.buffer[--i];

			if (!appIsPlaying || de.time < mTime)
			{
				if (de.obj != null)
				{
					NGUITools.Destroy(de.obj);
					de.obj = null;
				}
				mDest.RemoveAt(i);
			}
		}

		// Nothing left to update? Destroy this game object.
		if (mOnUpdate.Count == 0 && mOnLate.Count == 0 && mOnCoro.Count == 0 && mDest.size == 0)
		{
			NGUITools.Destroy(gameObject);
			return false;
		}
		return true;
	}

	/// <summary>
	/// Coroutine update function.
	/// </summary>

	IEnumerator CoroutineFunction ()
	{
		while (Application.isPlaying)
		{
			if (CoroutineUpdate()) yield return null;
			else break;
		}
	}

	/// <summary>
	/// Generic add function.
	/// Technically 'mb' is not necessary as it can be retrieved by calling 'func.Target as MonoBehaviour'.
	/// Unfortunately Flash export fails to compile with that, claiming the following:
	/// "Error: Access of possibly undefined property Target through a reference with static type Function."
	/// </summary>

	void Add (MonoBehaviour mb, int updateOrder, OnUpdate func, List<UpdateEntry> list)
	{
#if !UNITY_FLASH
		// Flash export fails at life.
		for (int i = 0, imax = list.Count; i < imax; ++i)
		{
			UpdateEntry ent = list[i];
			if (ent.func == func) return;
		}
#endif

		UpdateEntry item = new UpdateEntry();
		item.index = updateOrder;
		item.func = func;
		item.mb = mb;
		item.isMonoBehaviour = (mb != null);

		list.Add(item);
		if (updateOrder != 0) list.Sort(Compare);
	}

	/// <summary>
	/// Add a new update function with the specified update order.
	/// </summary>

	static public void AddUpdate (MonoBehaviour mb, int updateOrder, OnUpdate func) { CreateInstance(); mInst.Add(mb, updateOrder, func, mInst.mOnUpdate); }

	/// <summary>
	/// Add a new late update function with the specified update order.
	/// </summary>

	static public void AddLateUpdate (MonoBehaviour mb, int updateOrder, OnUpdate func) { CreateInstance(); mInst.Add(mb, updateOrder, func, mInst.mOnLate); }

	/// <summary>
	/// Add a new coroutine update function with the specified update order.
	/// </summary>

	static public void AddCoroutine (MonoBehaviour mb, int updateOrder, OnUpdate func) { CreateInstance(); mInst.Add(mb, updateOrder, func, mInst.mOnCoro); }

	/// <summary>
	/// Destroy the object after the specified delay in seconds.
	/// </summary>

	static public void AddDestroy (Object obj, float delay)
	{
		if (obj == null) return;

		if (Application.isPlaying)
		{
			if (delay > 0f)
			{
				CreateInstance();

				DestroyEntry item = new DestroyEntry();
				item.obj = obj;
				item.time = Time.realtimeSinceStartup + delay;
				mInst.mDest.Add(item);
			}
			else Destroy(obj);
		}
		else DestroyImmediate(obj);
	}
}*/

Commits for Nextrek/SpaceCrew/SpaceCrew/Assets/NGUI/Examples/Scripts/Other/UpdateManager.cs

Diff revisions: vs.
Revision Author Commited Message
83 FMMortaroli picture FMMortaroli Tue 13 May, 2014 11:32:51 +0000