TMP_UpdateManager.cs
7.66 KB
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
using UnityEngine;
using UnityEngine.Profiling;
using UnityEngine.UI;
using System.Collections.Generic;
namespace TMPro
{
public class TMP_UpdateManager
{
private static TMP_UpdateManager s_Instance;
private readonly HashSet<int> m_LayoutQueueLookup = new HashSet<int>();
private readonly List<TMP_Text> m_LayoutRebuildQueue = new List<TMP_Text>();
private readonly HashSet<int> m_GraphicQueueLookup = new HashSet<int>();
private readonly List<TMP_Text> m_GraphicRebuildQueue = new List<TMP_Text>();
private readonly HashSet<int> m_InternalUpdateLookup = new HashSet<int>();
private readonly List<TMP_Text> m_InternalUpdateQueue = new List<TMP_Text>();
private readonly HashSet<int> m_CullingUpdateLookup = new HashSet<int>();
private readonly List<TMP_Text> m_CullingUpdateQueue = new List<TMP_Text>();
/// <summary>
/// Get a singleton instance of the registry
/// </summary>
static TMP_UpdateManager instance
{
get
{
if (s_Instance == null)
s_Instance = new TMP_UpdateManager();
return s_Instance;
}
}
/// <summary>
/// Register to receive rendering callbacks.
/// </summary>
TMP_UpdateManager()
{
Canvas.willRenderCanvases += DoRebuilds;
}
/// <summary>
/// Function used as a replacement for LateUpdate() to handle SDF Scale updates and Legacy Animation updates.
/// </summary>
/// <param name="textObject"></param>
internal static void RegisterTextObjectForUpdate(TMP_Text textObject)
{
Profiler.BeginSample("TMP.RegisterTextObjectForUpdate");
instance.InternalRegisterTextObjectForUpdate(textObject);
Profiler.EndSample();
}
private void InternalRegisterTextObjectForUpdate(TMP_Text textObject)
{
int id = textObject.GetInstanceID();
if (m_InternalUpdateLookup.Contains(id))
return;
m_InternalUpdateLookup.Add(id);
m_InternalUpdateQueue.Add(textObject);
}
/// <summary>
/// Function to register elements which require a layout rebuild.
/// </summary>
/// <param name="element"></param>
public static void RegisterTextElementForLayoutRebuild(TMP_Text element)
{
instance.InternalRegisterTextElementForLayoutRebuild(element);
}
private void InternalRegisterTextElementForLayoutRebuild(TMP_Text element)
{
int id = element.GetInstanceID();
if (m_LayoutQueueLookup.Contains(id))
return;
m_LayoutQueueLookup.Add(id);
m_LayoutRebuildQueue.Add(element);
}
/// <summary>
/// Function to register elements which require a layout rebuild.
/// </summary>
/// <param name="element"></param>
public static void RegisterTextElementForGraphicRebuild(TMP_Text element)
{
Profiler.BeginSample("TMP.RegisterTextElementForGraphicRebuild");
instance.InternalRegisterTextElementForGraphicRebuild(element);
Profiler.EndSample();
}
private void InternalRegisterTextElementForGraphicRebuild(TMP_Text element)
{
int id = element.GetInstanceID();
if (m_GraphicQueueLookup.Contains(id))
return;
m_GraphicQueueLookup.Add(id);
m_GraphicRebuildQueue.Add(element);
}
public static void RegisterTextElementForCullingUpdate(TMP_Text element)
{
Profiler.BeginSample("TMP.RegisterTextElementForCullingUpdate");
instance.InternalRegisterTextElementForCullingUpdate(element);
Profiler.EndSample();
}
private void InternalRegisterTextElementForCullingUpdate(TMP_Text element)
{
int id = element.GetInstanceID();
if (m_CullingUpdateLookup.Contains(id))
return;
m_CullingUpdateLookup.Add(id);
m_CullingUpdateQueue.Add(element);
}
/// <summary>
/// Callback which occurs just before the cam is rendered.
/// </summary>
void OnCameraPreCull()
{
DoRebuilds();
}
/// <summary>
/// Process the rebuild requests in the rebuild queues.
/// </summary>
void DoRebuilds()
{
// Handle text objects the require an update either as a result of scale changes or legacy animation.
for (int i = 0; i < m_InternalUpdateQueue.Count; i++)
{
m_InternalUpdateQueue[i].InternalUpdate();
}
// Handle Layout Rebuild Phase
for (int i = 0; i < m_LayoutRebuildQueue.Count; i++)
{
m_LayoutRebuildQueue[i].Rebuild(CanvasUpdate.Prelayout);
}
if (m_LayoutRebuildQueue.Count > 0)
{
m_LayoutRebuildQueue.Clear();
m_LayoutQueueLookup.Clear();
}
// Handle Graphic Rebuild Phase
for (int i = 0; i < m_GraphicRebuildQueue.Count; i++)
{
m_GraphicRebuildQueue[i].Rebuild(CanvasUpdate.PreRender);
}
// If there are no objects in the queue, we don't need to clear the lists again.
if (m_GraphicRebuildQueue.Count > 0)
{
m_GraphicRebuildQueue.Clear();
m_GraphicQueueLookup.Clear();
}
// Handle Culling Update
for (int i = 0; i < m_CullingUpdateQueue.Count; i++)
{
m_CullingUpdateQueue[i].UpdateCulling();
}
// If there are no objects in the queue, we don't need to clear the lists again.
if (m_CullingUpdateQueue.Count > 0)
{
m_CullingUpdateQueue.Clear();
m_CullingUpdateLookup.Clear();
}
}
internal static void UnRegisterTextObjectForUpdate(TMP_Text textObject)
{
Profiler.BeginSample("TMP.UnRegisterTextObjectForUpdate");
instance.InternalUnRegisterTextObjectForUpdate(textObject);
Profiler.EndSample();
}
/// <summary>
/// Function to unregister elements which no longer require a rebuild.
/// </summary>
/// <param name="element"></param>
public static void UnRegisterTextElementForRebuild(TMP_Text element)
{
instance.InternalUnRegisterTextElementForGraphicRebuild(element);
instance.InternalUnRegisterTextElementForLayoutRebuild(element);
instance.InternalUnRegisterTextObjectForUpdate(element);
}
private void InternalUnRegisterTextElementForGraphicRebuild(TMP_Text element)
{
Profiler.BeginSample("TMP.InternalUnRegisterTextElementForGraphicRebuild");
int id = element.GetInstanceID();
m_GraphicRebuildQueue.Remove(element);
m_GraphicQueueLookup.Remove(id);
Profiler.EndSample();
}
private void InternalUnRegisterTextElementForLayoutRebuild(TMP_Text element)
{
int id = element.GetInstanceID();
m_LayoutRebuildQueue.Remove(element);
m_LayoutQueueLookup.Remove(id);
}
private void InternalUnRegisterTextObjectForUpdate(TMP_Text textObject)
{
int id = textObject.GetInstanceID();
m_InternalUpdateQueue.Remove(textObject);
m_InternalUpdateLookup.Remove(id);
}
}
}