TextMeshPro.cs
17.4 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
namespace TMPro
{
[DisallowMultipleComponent]
[RequireComponent(typeof(MeshRenderer))]
[RequireComponent(typeof(MeshFilter))]
[AddComponentMenu("Mesh/TextMeshPro - Text")]
[ExecuteAlways]
public partial class TextMeshPro : TMP_Text, ILayoutElement
{
// Public Properties and Serializable Properties
/// <summary>
/// Sets the Renderer's sorting Layer ID
/// </summary>
public int sortingLayerID
{
get { return m_renderer.sortingLayerID; }
set { m_renderer.sortingLayerID = value; }
}
/// <summary>
/// Sets the Renderer's sorting order within the assigned layer.
/// </summary>
public int sortingOrder
{
get { return m_renderer.sortingOrder; }
set { m_renderer.sortingOrder = value; }
}
/// <summary>
/// Determines if the size of the text container will be adjusted to fit the text object when it is first created.
/// </summary>
public override bool autoSizeTextContainer
{
get { return m_autoSizeTextContainer; }
set { if (m_autoSizeTextContainer == value) return; m_autoSizeTextContainer = value; if (m_autoSizeTextContainer) { TMP_UpdateManager.RegisterTextElementForLayoutRebuild(this); SetLayoutDirty(); } }
}
/// <summary>
/// Returns a reference to the Text Container
/// </summary>
[Obsolete("The TextContainer is now obsolete. Use the RectTransform instead.")]
public TextContainer textContainer
{
get
{
return null;
}
}
/// <summary>
/// Returns a reference to the Transform
/// </summary>
public new Transform transform
{
get
{
if (m_transform == null)
m_transform = GetComponent<Transform>();
return m_transform;
}
}
#pragma warning disable 0108
/// <summary>
/// Returns the rendered assigned to the text object.
/// </summary>
public Renderer renderer
{
get
{
if (m_renderer == null)
m_renderer = GetComponent<Renderer>();
return m_renderer;
}
}
/// <summary>
/// Returns the mesh assigned to the text object.
/// </summary>
public override Mesh mesh
{
get
{
if (m_mesh == null)
{
m_mesh = new Mesh();
m_mesh.hideFlags = HideFlags.HideAndDontSave;
this.meshFilter.mesh = m_mesh;
}
return m_mesh;
}
}
/// <summary>
/// Returns the Mesh Filter of the text object.
/// </summary>
public MeshFilter meshFilter
{
get
{
if (m_meshFilter == null)
m_meshFilter = GetComponent<MeshFilter>();
return m_meshFilter;
}
}
// MASKING RELATED PROPERTIES
/// <summary>
/// Sets the mask type
/// </summary>
public MaskingTypes maskType
{
get { return m_maskType; }
set { m_maskType = value; SetMask(m_maskType); }
}
/// <summary>
/// Function used to set the mask type and coordinates in World Space
/// </summary>
/// <param name="type"></param>
/// <param name="maskCoords"></param>
public void SetMask(MaskingTypes type, Vector4 maskCoords)
{
SetMask(type);
SetMaskCoordinates(maskCoords);
}
/// <summary>
/// Function used to set the mask type, coordinates and softness
/// </summary>
/// <param name="type"></param>
/// <param name="maskCoords"></param>
/// <param name="softnessX"></param>
/// <param name="softnessY"></param>
public void SetMask(MaskingTypes type, Vector4 maskCoords, float softnessX, float softnessY)
{
SetMask(type);
SetMaskCoordinates(maskCoords, softnessX, softnessY);
}
/// <summary>
/// Schedule rebuilding of the text geometry.
/// </summary>
public override void SetVerticesDirty()
{
//Debug.Log("SetVerticesDirty()");
if (m_verticesAlreadyDirty || this == null || !this.IsActive())
return;
TMP_UpdateManager.RegisterTextElementForGraphicRebuild(this);
m_verticesAlreadyDirty = true;
}
/// <summary>
///
/// </summary>
public override void SetLayoutDirty()
{
m_isPreferredWidthDirty = true;
m_isPreferredHeightDirty = true;
if (m_layoutAlreadyDirty || this == null || !this.IsActive())
return;
//TMP_UpdateManager.RegisterTextElementForLayoutRebuild(this);
m_layoutAlreadyDirty = true;
//LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform);
m_isLayoutDirty = true;
}
/// <summary>
/// Schedule updating of the material used by the text object.
/// </summary>
public override void SetMaterialDirty()
{
//Debug.Log("SetMaterialDirty()");
//if (!this.IsActive())
// return;
//m_isMaterialDirty = true;
UpdateMaterial();
//TMP_UpdateManager.RegisterTextElementForGraphicRebuild(this);
}
/// <summary>
///
/// </summary>
public override void SetAllDirty()
{
m_isInputParsingRequired = true;
SetLayoutDirty();
SetVerticesDirty();
SetMaterialDirty();
}
/// <summary>
///
/// </summary>
/// <param name="update"></param>
public override void Rebuild(CanvasUpdate update)
{
if (this == null) return;
if (update == CanvasUpdate.Prelayout)
{
if (m_autoSizeTextContainer)
{
m_rectTransform.sizeDelta = GetPreferredValues(Mathf.Infinity, Mathf.Infinity);
}
}
else if (update == CanvasUpdate.PreRender)
{
this.OnPreRenderObject();
m_verticesAlreadyDirty = false;
m_layoutAlreadyDirty = false;
if (!m_isMaterialDirty) return;
UpdateMaterial();
m_isMaterialDirty = false;
}
}
/// <summary>
///
/// </summary>
protected override void UpdateMaterial()
{
//Debug.Log("*** UpdateMaterial() ***");
//if (!this.IsActive())
// return;
if (m_sharedMaterial == null)
return;
if (m_renderer == null) m_renderer = this.renderer;
// Only update the material if it has changed.
if (m_renderer.sharedMaterial.GetInstanceID() != m_sharedMaterial.GetInstanceID())
m_renderer.sharedMaterial = m_sharedMaterial;
}
/// <summary>
/// Function to be used to force recomputing of character padding when Shader / Material properties have been changed via script.
/// </summary>
public override void UpdateMeshPadding()
{
m_padding = ShaderUtilities.GetPadding(m_sharedMaterial, m_enableExtraPadding, m_isUsingBold);
m_isMaskingEnabled = ShaderUtilities.IsMaskingEnabled(m_sharedMaterial);
m_havePropertiesChanged = true;
checkPaddingRequired = false;
// Return if text object is not awake yet.
if (m_textInfo == null) return;
// Update sub text objects
for (int i = 1; i < m_textInfo.materialCount; i++)
m_subTextObjects[i].UpdateMeshPadding(m_enableExtraPadding, m_isUsingBold);
}
/// <summary>
/// Function to force regeneration of the mesh before its normal process time. This is useful when changes to the text object properties need to be applied immediately.
/// </summary>
public override void ForceMeshUpdate()
{
//Debug.Log("ForceMeshUpdate() called.");
m_havePropertiesChanged = true;
OnPreRenderObject();
}
/// <summary>
/// Function to force regeneration of the mesh before its normal process time. This is useful when changes to the text object properties need to be applied immediately.
/// </summary>
/// <param name="ignoreInactive">If set to true, the text object will be regenerated regardless of is active state.</param>
public override void ForceMeshUpdate(bool ignoreInactive)
{
m_havePropertiesChanged = true;
m_ignoreActiveState = true;
OnPreRenderObject();
}
/// <summary>
/// Function used to evaluate the length of a text string.
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public override TMP_TextInfo GetTextInfo(string text)
{
StringToCharArray(text, ref m_TextParsingBuffer);
SetArraySizes(m_TextParsingBuffer);
m_renderMode = TextRenderFlags.DontRender;
ComputeMarginSize();
GenerateTextMesh();
m_renderMode = TextRenderFlags.Render;
return this.textInfo;
}
/// <summary>
/// Function to clear the geometry of the Primary and Sub Text objects.
/// </summary>
public override void ClearMesh(bool updateMesh)
{
if (m_textInfo.meshInfo[0].mesh == null) m_textInfo.meshInfo[0].mesh = m_mesh;
m_textInfo.ClearMeshInfo(updateMesh);
}
/// <summary>
/// Function to force the regeneration of the text object.
/// </summary>
/// <param name="flags"> Flags to control which portions of the geometry gets uploaded.</param>
//public override void ForceMeshUpdate(TMP_VertexDataUpdateFlags flags) { }
/// <summary>
/// Function to update the geometry of the main and sub text objects.
/// </summary>
/// <param name="mesh"></param>
/// <param name="index"></param>
public override void UpdateGeometry(Mesh mesh, int index)
{
mesh.RecalculateBounds();
}
/// <summary>
/// Function to upload the updated vertex data and renderer.
/// </summary>
public override void UpdateVertexData(TMP_VertexDataUpdateFlags flags)
{
int materialCount = m_textInfo.materialCount;
for (int i = 0; i < materialCount; i++)
{
Mesh mesh;
if (i == 0)
mesh = m_mesh;
else
{
// Clear unused vertices
// TODO: Causes issues when sorting geometry as last vertex data attribute get wiped out.
//m_textInfo.meshInfo[i].ClearUnusedVertices();
mesh = m_subTextObjects[i].mesh;
}
//mesh.MarkDynamic();
if ((flags & TMP_VertexDataUpdateFlags.Vertices) == TMP_VertexDataUpdateFlags.Vertices)
mesh.vertices = m_textInfo.meshInfo[i].vertices;
if ((flags & TMP_VertexDataUpdateFlags.Uv0) == TMP_VertexDataUpdateFlags.Uv0)
mesh.uv = m_textInfo.meshInfo[i].uvs0;
if ((flags & TMP_VertexDataUpdateFlags.Uv2) == TMP_VertexDataUpdateFlags.Uv2)
mesh.uv2 = m_textInfo.meshInfo[i].uvs2;
//if ((flags & TMP_VertexDataUpdateFlags.Uv4) == TMP_VertexDataUpdateFlags.Uv4)
// mesh.uv4 = m_textInfo.meshInfo[i].uvs4;
if ((flags & TMP_VertexDataUpdateFlags.Colors32) == TMP_VertexDataUpdateFlags.Colors32)
mesh.colors32 = m_textInfo.meshInfo[i].colors32;
mesh.RecalculateBounds();
}
}
/// <summary>
/// Function to upload the updated vertex data and renderer.
/// </summary>
public override void UpdateVertexData()
{
int materialCount = m_textInfo.materialCount;
for (int i = 0; i < materialCount; i++)
{
Mesh mesh;
if (i == 0)
mesh = m_mesh;
else
{
// Clear unused vertices
m_textInfo.meshInfo[i].ClearUnusedVertices();
mesh = m_subTextObjects[i].mesh;
}
//mesh.MarkDynamic();
mesh.vertices = m_textInfo.meshInfo[i].vertices;
mesh.uv = m_textInfo.meshInfo[i].uvs0;
mesh.uv2 = m_textInfo.meshInfo[i].uvs2;
//mesh.uv4 = m_textInfo.meshInfo[i].uvs4;
mesh.colors32 = m_textInfo.meshInfo[i].colors32;
mesh.RecalculateBounds();
}
}
public void UpdateFontAsset()
{
LoadFontAsset();
}
private bool m_currentAutoSizeMode;
public void CalculateLayoutInputHorizontal()
{
//Debug.Log("*** CalculateLayoutInputHorizontal() ***");
if (!this.gameObject.activeInHierarchy)
return;
//IsRectTransformDriven = true;
m_currentAutoSizeMode = m_enableAutoSizing;
if (m_isCalculateSizeRequired || m_rectTransform.hasChanged)
{
//Debug.Log("Calculating Layout Horizontal");
//m_LayoutPhase = AutoLayoutPhase.Horizontal;
//m_isRebuildingLayout = true;
m_minWidth = 0;
m_flexibleWidth = 0;
//m_renderMode = TextRenderFlags.GetPreferredSizes; // Set Text to not Render and exit early once we have new width values.
if (m_enableAutoSizing)
{
m_fontSize = m_fontSizeMax;
}
// Set Margins to Infinity
m_marginWidth = k_LargePositiveFloat;
m_marginHeight = k_LargePositiveFloat;
if (m_isInputParsingRequired || m_isTextTruncated)
ParseInputText();
GenerateTextMesh();
m_renderMode = TextRenderFlags.Render;
//m_preferredWidth = (int)m_preferredWidth + 1f;
ComputeMarginSize();
//Debug.Log("Preferred Width: " + m_preferredWidth + " Margin Width: " + m_marginWidth + " Preferred Height: " + m_preferredHeight + " Margin Height: " + m_marginHeight + " Rendered Width: " + m_renderedWidth + " Height: " + m_renderedHeight + " RectTransform Width: " + m_rectTransform.rect);
m_isLayoutDirty = true;
}
}
public void CalculateLayoutInputVertical()
{
//Debug.Log("*** CalculateLayoutInputVertical() ***");
// Check if object is active
if (!this.gameObject.activeInHierarchy) // || IsRectTransformDriven == false)
return;
//IsRectTransformDriven = true;
if (m_isCalculateSizeRequired || m_rectTransform.hasChanged)
{
//Debug.Log("Calculating Layout InputVertical");
//m_LayoutPhase = AutoLayoutPhase.Vertical;
//m_isRebuildingLayout = true;
m_minHeight = 0;
m_flexibleHeight = 0;
//m_renderMode = TextRenderFlags.GetPreferredSizes;
if (m_enableAutoSizing)
{
m_currentAutoSizeMode = true;
m_enableAutoSizing = false;
}
m_marginHeight = k_LargePositiveFloat;
GenerateTextMesh();
m_enableAutoSizing = m_currentAutoSizeMode;
m_renderMode = TextRenderFlags.Render;
//m_preferredHeight = (int)m_preferredHeight + 1f;
ComputeMarginSize();
//Debug.Log("Preferred Height: " + m_preferredHeight + " Margin Height: " + m_marginHeight + " Preferred Width: " + m_preferredWidth + " Margin Width: " + m_marginWidth + " Rendered Width: " + m_renderedWidth + " Height: " + m_renderedHeight + " RectTransform Width: " + m_rectTransform.rect);
m_isLayoutDirty = true;
}
m_isCalculateSizeRequired = false;
}
}
}