A game about forced loneliness, made by TACStudios
1using UnityEngine;
2using UnityEngine.Rendering;
3using UnityEngine.UI;
4using System.Collections;
5using System.Collections.Generic;
6using Object = UnityEngine.Object;
7
8#pragma warning disable 0414 // Disabled a few warnings related to serialized variables not used in this script but used in the editor.
9
10namespace TMPro
11{
12 [ExecuteAlways]
13 [RequireComponent(typeof(CanvasRenderer))]
14 public class TMP_SubMeshUI : MaskableGraphic
15 {
16 /// <summary>
17 /// The TMP Font Asset assigned to this sub text object.
18 /// </summary>
19 public TMP_FontAsset fontAsset
20 {
21 get { return m_fontAsset; }
22 set { m_fontAsset = value; }
23 }
24 [SerializeField]
25 private TMP_FontAsset m_fontAsset;
26
27
28 /// <summary>
29 /// The TMP Sprite Asset assigned to this sub text object.
30 /// </summary>
31 public TMP_SpriteAsset spriteAsset
32 {
33 get { return m_spriteAsset; }
34 set { m_spriteAsset = value; }
35 }
36 [SerializeField]
37 private TMP_SpriteAsset m_spriteAsset;
38
39
40 /// <summary>
41 ///
42 /// </summary>
43 public override Texture mainTexture
44 {
45 get
46 {
47 if (this.sharedMaterial != null)
48 return this.sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex);
49
50 return null;
51 }
52 }
53
54
55 /// <summary>
56 /// The material to be assigned to this object. Returns an instance of the material.
57 /// </summary>
58 public override Material material
59 {
60 // Return a new Instance of the Material if none exists. Otherwise return the current Material Instance.
61 get { return GetMaterial(m_sharedMaterial); }
62
63 // Assign new font material
64 set
65 {
66 if (m_sharedMaterial != null && m_sharedMaterial.GetInstanceID() == value.GetInstanceID())
67 return;
68
69 m_sharedMaterial = m_material = value;
70
71 m_padding = GetPaddingForMaterial();
72
73 SetVerticesDirty();
74 SetMaterialDirty();
75 }
76 }
77 [SerializeField]
78 private Material m_material;
79
80
81 /// <summary>
82 /// The material to be assigned to this text object.
83 /// </summary>
84 public Material sharedMaterial
85 {
86 get { return m_sharedMaterial; }
87 set { SetSharedMaterial(value); }
88 }
89 [SerializeField]
90 private Material m_sharedMaterial;
91
92
93 /// <summary>
94 ///
95 /// </summary>
96 public Material fallbackMaterial
97 {
98 get { return m_fallbackMaterial; }
99 set
100 {
101 if (m_fallbackMaterial == value) return;
102
103 if (m_fallbackMaterial != null && m_fallbackMaterial != value)
104 TMP_MaterialManager.ReleaseFallbackMaterial(m_fallbackMaterial);
105
106 m_fallbackMaterial = value;
107 TMP_MaterialManager.AddFallbackMaterialReference(m_fallbackMaterial);
108
109 SetSharedMaterial(m_fallbackMaterial);
110 }
111 }
112 private Material m_fallbackMaterial;
113
114
115 /// <summary>
116 /// The source material used by the fallback font
117 /// </summary>
118 public Material fallbackSourceMaterial
119 {
120 get { return m_fallbackSourceMaterial; }
121 set { m_fallbackSourceMaterial = value; }
122 }
123 private Material m_fallbackSourceMaterial;
124
125
126 /// <summary>
127 /// Get the material that will be used for rendering.
128 /// </summary>
129 public override Material materialForRendering
130 {
131 get
132 {
133 return TMP_MaterialManager.GetMaterialForRendering(this, m_sharedMaterial);
134 }
135 }
136
137
138 /// <summary>
139 /// Is the text object using the default font asset material.
140 /// </summary>
141 public bool isDefaultMaterial
142 {
143 get { return m_isDefaultMaterial; }
144 set { m_isDefaultMaterial = value; }
145 }
146 [SerializeField]
147 private bool m_isDefaultMaterial;
148
149
150 /// <summary>
151 /// Padding value resulting for the property settings on the material.
152 /// </summary>
153 public float padding
154 {
155 get { return m_padding; }
156 set { m_padding = value; }
157 }
158 [SerializeField]
159 private float m_padding;
160
161
162 /// <summary>
163 /// The Mesh of this text sub object.
164 /// </summary>
165 public Mesh mesh
166 {
167 get
168 {
169 if (m_mesh == null)
170 {
171 m_mesh = new Mesh();
172 m_mesh.hideFlags = HideFlags.HideAndDontSave;
173 }
174
175 return m_mesh;
176 }
177 set { m_mesh = value; }
178 }
179 private Mesh m_mesh;
180
181
182 /// <summary>
183 /// Reference to the parent Text Component.
184 /// </summary>
185 public TMP_Text textComponent
186 {
187 get
188 {
189 if (m_TextComponent == null)
190 m_TextComponent = GetComponentInParent<TextMeshProUGUI>();
191
192 return m_TextComponent;
193 }
194 }
195 [SerializeField]
196 private TextMeshProUGUI m_TextComponent;
197
198
199 [System.NonSerialized]
200 private bool m_isRegisteredForEvents;
201 private bool m_materialDirty;
202 [SerializeField]
203 private int m_materialReferenceIndex;
204
205
206
207 /// <summary>
208 /// Function to add a new sub text object.
209 /// </summary>
210 /// <param name="textComponent"></param>
211 /// <param name="materialReference"></param>
212 /// <returns></returns>
213 public static TMP_SubMeshUI AddSubTextObject(TextMeshProUGUI textComponent, MaterialReference materialReference)
214 {
215 GameObject go = new GameObject();
216 go.hideFlags = TMP_Settings.hideSubTextObjects ? HideFlags.HideAndDontSave : HideFlags.DontSave;
217
218 go.transform.SetParent(textComponent.transform, false);
219 go.transform.SetAsFirstSibling();
220 go.layer = textComponent.gameObject.layer;
221
222 #if UNITY_EDITOR
223 go.name = materialReference.material == null ? "TMP SubMesh" : "TMP SubMesh [" + materialReference.material.name + "]";
224 #endif
225
226 RectTransform rectTransform = go.AddComponent<RectTransform>();
227 rectTransform.anchorMin = Vector2.zero;
228 rectTransform.anchorMax = Vector2.one;
229 rectTransform.sizeDelta = Vector2.zero;
230 rectTransform.pivot = textComponent.rectTransform.pivot;
231
232 LayoutElement layoutElement = go.AddComponent<LayoutElement>();
233 layoutElement.ignoreLayout = true;
234
235 TMP_SubMeshUI subMesh = go.AddComponent<TMP_SubMeshUI>();
236 subMesh.m_TextComponent = textComponent;
237 subMesh.m_materialReferenceIndex = materialReference.index;
238 subMesh.m_fontAsset = materialReference.fontAsset;
239 subMesh.m_spriteAsset = materialReference.spriteAsset;
240 subMesh.m_isDefaultMaterial = materialReference.isDefaultMaterial;
241 subMesh.maskable = textComponent.maskable;
242 subMesh.SetSharedMaterial(materialReference.material);
243
244 return subMesh;
245 }
246
247
248
249 /// <summary>
250 ///
251 /// </summary>
252 protected override void OnEnable()
253 {
254 //Debug.Log("*** SubObject OnEnable() ***");
255
256 // Register Callbacks for various events.
257 if (!m_isRegisteredForEvents)
258 {
259
260 #if UNITY_EDITOR
261 TMPro_EventManager.MATERIAL_PROPERTY_EVENT.Add(ON_MATERIAL_PROPERTY_CHANGED);
262 TMPro_EventManager.FONT_PROPERTY_EVENT.Add(ON_FONT_PROPERTY_CHANGED);
263 //TMPro_EventManager.TEXTMESHPRO_PROPERTY_EVENT.Add(ON_TEXTMESHPRO_PROPERTY_CHANGED);
264 TMPro_EventManager.DRAG_AND_DROP_MATERIAL_EVENT.Add(ON_DRAG_AND_DROP_MATERIAL);
265 //TMPro_EventManager.TEXT_STYLE_PROPERTY_EVENT.Add(ON_TEXT_STYLE_CHANGED);
266 TMPro_EventManager.SPRITE_ASSET_PROPERTY_EVENT.Add(ON_SPRITE_ASSET_PROPERTY_CHANGED);
267 //TMPro_EventManager.TMP_SETTINGS_PROPERTY_EVENT.Add(ON_TMP_SETTINGS_CHANGED);
268 #endif
269
270 m_isRegisteredForEvents = true;
271 }
272
273 // Update HideFlags on previously created sub text objects.
274 if (hideFlags != HideFlags.DontSave)
275 hideFlags = HideFlags.DontSave;
276
277 m_ShouldRecalculateStencil = true;
278 RecalculateClipping();
279 RecalculateMasking();
280
281 //SetAllDirty();
282 }
283
284
285 protected override void OnDisable()
286 {
287 //Debug.Log("*** SubObject OnDisable() ***");
288 base.OnDisable();
289
290 // if (m_MaskMaterial != null)
291 // {
292 // TMP_MaterialManager.ReleaseStencilMaterial(m_MaskMaterial);
293 // m_MaskMaterial = null;
294 // }
295
296 if (m_fallbackMaterial != null)
297 {
298 TMP_MaterialManager.ReleaseFallbackMaterial(m_fallbackMaterial);
299 m_fallbackMaterial = null;
300 }
301 }
302
303
304 protected override void OnDestroy()
305 {
306 //Debug.Log("*** OnDestroy() ***");
307
308 // Destroy Mesh
309 if (m_mesh != null) DestroyImmediate(m_mesh);
310
311 if (m_MaskMaterial != null)
312 TMP_MaterialManager.ReleaseStencilMaterial(m_MaskMaterial);
313
314 if (m_fallbackMaterial != null)
315 {
316 TMP_MaterialManager.ReleaseFallbackMaterial(m_fallbackMaterial);
317 m_fallbackMaterial = null;
318 }
319
320 #if UNITY_EDITOR
321 // Unregister the event this object was listening to
322 TMPro_EventManager.MATERIAL_PROPERTY_EVENT.Remove(ON_MATERIAL_PROPERTY_CHANGED);
323 TMPro_EventManager.FONT_PROPERTY_EVENT.Remove(ON_FONT_PROPERTY_CHANGED);
324 //TMPro_EventManager.TEXTMESHPRO_PROPERTY_EVENT.Remove(ON_TEXTMESHPRO_PROPERTY_CHANGED);
325 TMPro_EventManager.DRAG_AND_DROP_MATERIAL_EVENT.Remove(ON_DRAG_AND_DROP_MATERIAL);
326 //TMPro_EventManager.TEXT_STYLE_PROPERTY_EVENT.Remove(ON_TEXT_STYLE_CHANGED);
327 TMPro_EventManager.SPRITE_ASSET_PROPERTY_EVENT.Remove(ON_SPRITE_ASSET_PROPERTY_CHANGED);
328 //TMPro_EventManager.TMP_SETTINGS_PROPERTY_EVENT.Remove(ON_TMP_SETTINGS_CHANGED);
329 #endif
330
331 m_isRegisteredForEvents = false;
332
333 RecalculateClipping();
334
335 // Notify parent text object
336 if (m_TextComponent != null)
337 {
338 m_TextComponent.havePropertiesChanged = true;
339 m_TextComponent.SetAllDirty();
340 }
341 }
342
343
344 #if UNITY_EDITOR
345 // Event received when custom material editor properties are changed.
346 void ON_MATERIAL_PROPERTY_CHANGED(bool isChanged, Material mat)
347 {
348 //Debug.Log("*** ON_MATERIAL_PROPERTY_CHANGED ***");
349 if (m_sharedMaterial == null)
350 return;
351
352 int targetMaterialID = mat.GetInstanceID();
353 int sharedMaterialID = m_sharedMaterial.GetInstanceID();
354 int maskingMaterialID = m_MaskMaterial == null ? 0 : m_MaskMaterial.GetInstanceID();
355 int fallbackSourceMaterialID = m_fallbackSourceMaterial == null ? 0 : m_fallbackSourceMaterial.GetInstanceID();
356
357 // Sync culling with parent text object
358 bool hasCullModeProperty = m_sharedMaterial.HasProperty(ShaderUtilities.ShaderTag_CullMode);
359 float cullMode = 0;
360
361 if (hasCullModeProperty)
362 {
363 cullMode = textComponent.fontSharedMaterial.GetFloat(ShaderUtilities.ShaderTag_CullMode);
364 m_sharedMaterial.SetFloat(ShaderUtilities.ShaderTag_CullMode, cullMode);
365 }
366
367 // Filter events and return if the affected material is not this object's material.
368 if (m_fallbackMaterial != null && fallbackSourceMaterialID == targetMaterialID && TMP_Settings.matchMaterialPreset)
369 {
370 TMP_MaterialManager.CopyMaterialPresetProperties(mat, m_fallbackMaterial);
371
372 // Re-sync culling with parent text object
373 m_fallbackMaterial.SetFloat(ShaderUtilities.ShaderTag_CullMode, cullMode);
374 }
375
376 // Make sure material properties are synchronized between the assigned material and masking material.
377 if (m_MaskMaterial != null)
378 {
379 UnityEditor.Undo.RecordObject(m_MaskMaterial, "Material Property Changes");
380 UnityEditor.Undo.RecordObject(m_sharedMaterial, "Material Property Changes");
381
382 if (targetMaterialID == sharedMaterialID)
383 {
384 //Debug.Log("Copy base material properties to masking material if not null.");
385 float stencilID = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilID);
386 float stencilComp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilComp);
387 m_MaskMaterial.CopyPropertiesFromMaterial(mat);
388 m_MaskMaterial.shaderKeywords = mat.shaderKeywords;
389
390 m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID);
391 m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
392 }
393 else if (targetMaterialID == maskingMaterialID)
394 {
395 // Update the padding
396 GetPaddingForMaterial(mat);
397
398 m_sharedMaterial.CopyPropertiesFromMaterial(mat);
399 m_sharedMaterial.shaderKeywords = mat.shaderKeywords;
400 m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilID, 0);
401 m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 8);
402 }
403 else if (fallbackSourceMaterialID == targetMaterialID)
404 {
405 float stencilID = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilID);
406 float stencilComp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilComp);
407 m_MaskMaterial.CopyPropertiesFromMaterial(m_fallbackMaterial);
408 m_MaskMaterial.shaderKeywords = m_fallbackMaterial.shaderKeywords;
409
410 m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID);
411 m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
412 }
413
414 // Re-sync culling with parent text object
415 if (hasCullModeProperty)
416 m_MaskMaterial.SetFloat(ShaderUtilities.ShaderTag_CullMode, cullMode);
417 }
418
419 m_padding = GetPaddingForMaterial();
420
421 SetVerticesDirty();
422 m_ShouldRecalculateStencil = true;
423 RecalculateClipping();
424 RecalculateMasking();
425 }
426
427
428 // Event to Track Material Changed resulting from Drag-n-drop.
429 void ON_DRAG_AND_DROP_MATERIAL(GameObject obj, Material currentMaterial, Material newMaterial)
430 {
431 // Check if event applies to this current object
432 if (obj == gameObject || UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(gameObject) == obj)
433 {
434 if (!m_isDefaultMaterial) return;
435
436 // Make sure we have a valid reference to the renderer.
437 //if (m_canvasRenderer == null) m_canvasRenderer = GetComponent<CanvasRenderer>();
438
439 UnityEditor.Undo.RecordObject(this, "Material Assignment");
440 UnityEditor.Undo.RecordObject(canvasRenderer, "Material Assignment");
441
442 SetSharedMaterial(newMaterial);
443 m_TextComponent.havePropertiesChanged = true;
444 }
445 }
446
447 // Event received when font asset properties are changed in Font Inspector
448 void ON_SPRITE_ASSET_PROPERTY_CHANGED(bool isChanged, UnityEngine.Object obj)
449 {
450 //if (spriteSheet != null && (obj as TMP_SpriteAsset == m_spriteAsset || obj as Texture2D == m_spriteAsset.spriteSheet))
451 //{
452 if (m_TextComponent != null)
453 {
454 m_TextComponent.havePropertiesChanged = true;
455 //m_TextComponent.SetVerticesDirty();
456 }
457
458 //}
459 }
460
461 // Event received when font asset properties are changed in Font Inspector
462 void ON_FONT_PROPERTY_CHANGED(bool isChanged, Object fontAsset)
463 {
464 if (m_fontAsset != null && fontAsset != null && fontAsset.GetInstanceID() == m_fontAsset.GetInstanceID())
465 {
466 // Copy Normal and Bold Weight
467 if (m_fallbackMaterial != null)
468 {
469 if (TMP_Settings.matchMaterialPreset)
470 {
471 TMP_MaterialManager.ReleaseFallbackMaterial(m_fallbackMaterial);
472 TMP_MaterialManager.CleanupFallbackMaterials();
473 }
474 }
475 }
476 }
477
478 /// <summary>
479 /// Event received when the TMP Settings are changed.
480 /// </summary>
481 void ON_TMP_SETTINGS_CHANGED()
482 {
483 //Debug.Log("TMP Setting have changed.");
484 //SetVerticesDirty();
485 //SetMaterialDirty();
486 }
487 #endif
488
489 /// <summary>
490 ///
491 /// </summary>
492 protected override void OnTransformParentChanged()
493 {
494 if (!this.IsActive())
495 return;
496
497 m_ShouldRecalculateStencil = true;
498 RecalculateClipping();
499 RecalculateMasking();
500 }
501
502
503 /// <summary>
504 /// Function returning the modified material for masking if necessary.
505 /// </summary>
506 /// <param name="baseMaterial"></param>
507 /// <returns></returns>
508 public override Material GetModifiedMaterial(Material baseMaterial)
509 {
510 Material mat = baseMaterial;
511
512 if (m_ShouldRecalculateStencil)
513 {
514 var rootCanvas = MaskUtilities.FindRootSortOverrideCanvas(transform);
515 m_StencilValue = maskable ? MaskUtilities.GetStencilDepth(transform, rootCanvas) : 0;
516 m_ShouldRecalculateStencil = false;
517 }
518
519 if (m_StencilValue > 0)
520 {
521 var maskMat = StencilMaterial.Add(mat, (1 << m_StencilValue) - 1, StencilOp.Keep, CompareFunction.Equal, ColorWriteMask.All, (1 << m_StencilValue) - 1, 0);
522 StencilMaterial.Remove(m_MaskMaterial);
523 m_MaskMaterial = maskMat;
524 mat = m_MaskMaterial;
525 }
526
527 return mat;
528 }
529
530
531 /// <summary>
532 /// Function called when the padding value for the material needs to be re-calculated.
533 /// </summary>
534 /// <returns></returns>
535 public float GetPaddingForMaterial()
536 {
537 float padding = ShaderUtilities.GetPadding(m_sharedMaterial, m_TextComponent.extraPadding, m_TextComponent.isUsingBold);
538
539 return padding;
540 }
541
542
543 /// <summary>
544 /// Function called when the padding value for the material needs to be re-calculated.
545 /// </summary>
546 /// <returns></returns>
547 public float GetPaddingForMaterial(Material mat)
548 {
549 float padding = ShaderUtilities.GetPadding(mat, m_TextComponent.extraPadding, m_TextComponent.isUsingBold);
550
551 return padding;
552 }
553
554
555 /// <summary>
556 ///
557 /// </summary>
558 /// <param name="isExtraPadding"></param>
559 /// <param name="isBold"></param>
560 public void UpdateMeshPadding(bool isExtraPadding, bool isUsingBold)
561 {
562 m_padding = ShaderUtilities.GetPadding(m_sharedMaterial, isExtraPadding, isUsingBold);
563 }
564
565
566 /// <summary>
567 ///
568 /// </summary>
569 public override void SetAllDirty()
570 {
571 //SetLayoutDirty();
572 //SetVerticesDirty();
573 //SetMaterialDirty();
574 }
575
576
577 /// <summary>
578 ///
579 /// </summary>
580 public override void SetVerticesDirty()
581 {
582 // Do nothing as updates of the text are driven by the parent text component
583 }
584
585
586 /// <summary>
587 ///
588 /// </summary>
589 public override void SetLayoutDirty()
590 {
591
592 }
593
594
595 /// <summary>
596 ///
597 /// </summary>
598 public override void SetMaterialDirty()
599 {
600 m_materialDirty = true;
601
602 UpdateMaterial();
603
604 if (m_OnDirtyMaterialCallback != null)
605 m_OnDirtyMaterialCallback();
606 }
607
608
609 /// <summary>
610 ///
611 /// </summary>
612 public void SetPivotDirty()
613 {
614 if (!this.IsActive())
615 return;
616
617 this.rectTransform.pivot = m_TextComponent.rectTransform.pivot;
618 }
619
620 Transform GetRootCanvasTransform()
621 {
622 if (m_RootCanvasTransform == null)
623 m_RootCanvasTransform = m_TextComponent.canvas.rootCanvas.transform;
624
625 return m_RootCanvasTransform;
626 }
627 private Transform m_RootCanvasTransform;
628
629 /// <summary>
630 /// Override Cull function as this is handled by the parent text object.
631 /// </summary>
632 /// <param name="clipRect"></param>
633 /// <param name="validRect"></param>
634 public override void Cull(Rect clipRect, bool validRect)
635 {
636 // Do nothing as this functionality is handled by the parent text object.
637 }
638
639
640 /// <summary>
641 ///
642 /// </summary>
643 protected override void UpdateGeometry()
644 {
645 // Need to override to prevent Unity from changing the geometry of the object.
646 //Debug.Log("UpdateGeometry()");
647 }
648
649
650 /// <summary>
651 ///
652 /// </summary>
653 /// <param name="update"></param>
654 public override void Rebuild(CanvasUpdate update)
655 {
656 if (update == CanvasUpdate.PreRender)
657 {
658 if (!m_materialDirty) return;
659
660 UpdateMaterial();
661 m_materialDirty = false;
662 }
663 }
664
665
666 /// <summary>
667 /// Function to update the material from the parent text object.
668 /// </summary>
669 public void RefreshMaterial()
670 {
671 UpdateMaterial();
672 }
673
674
675 /// <summary>
676 ///
677 /// </summary>
678 protected override void UpdateMaterial()
679 {
680 //Debug.Log("*** STO-UI - UpdateMaterial() *** FRAME (" + Time.frameCount + ")");
681
682 if (m_sharedMaterial == null)
683 return;
684
685 //if (canvasRenderer == null) m_canvasRenderer = this.canvasRenderer;
686
687 // Special handling to keep the Culling of the material in sync with parent text object
688 if (m_sharedMaterial.HasProperty(ShaderUtilities.ShaderTag_CullMode) && textComponent.fontSharedMaterial != null)
689 {
690 float cullMode = textComponent.fontSharedMaterial.GetFloat(ShaderUtilities.ShaderTag_CullMode);
691 m_sharedMaterial.SetFloat(ShaderUtilities.ShaderTag_CullMode, cullMode);
692 }
693
694 canvasRenderer.materialCount = 1;
695 canvasRenderer.SetMaterial(materialForRendering, 0);
696 //canvasRenderer.SetTexture(materialForRendering.mainTexture);
697
698 #if UNITY_EDITOR
699 if (m_sharedMaterial != null && gameObject.name != "TMP SubMeshUI [" + m_sharedMaterial.name + "]")
700 gameObject.name = "TMP SubMeshUI [" + m_sharedMaterial.name + "]";
701 #endif
702 }
703
704
705 // IClippable implementation
706 /// <summary>
707 /// Method called when the state of a parent changes.
708 /// </summary>
709 public override void RecalculateClipping()
710 {
711 //Debug.Log("*** RecalculateClipping() ***");
712 base.RecalculateClipping();
713 }
714
715
716 /// <summary>
717 ///
718 /// </summary>
719 // public override void RecalculateMasking()
720 // {
721 // //Debug.Log("RecalculateMasking()");
722 //
723 // this.m_ShouldRecalculateStencil = true;
724 // SetMaterialDirty();
725 // }
726
727
728
729 /// <summary>
730 /// Method which returns an instance of the shared material
731 /// </summary>
732 /// <returns></returns>
733 Material GetMaterial()
734 {
735 // Make sure we have a valid reference to the renderer.
736 //if (m_renderer == null) m_renderer = GetComponent<Renderer>();
737
738 //if (m_material == null || m_isNewSharedMaterial)
739 //{
740 // m_renderer.material = m_sharedMaterial;
741 // m_material = m_renderer.material;
742 // m_sharedMaterial = m_material;
743 // m_padding = ShaderUtilities.GetPadding(m_sharedMaterial, m_TextMeshPro.extraPadding, false);
744 // m_isNewSharedMaterial = false;
745 //}
746
747 return m_sharedMaterial;
748 }
749
750
751 // Function called internally when a new material is assigned via the fontMaterial property.
752 Material GetMaterial(Material mat)
753 {
754 // Check in case Object is disabled. If so, we don't have a valid reference to the Renderer.
755 // This can occur when the Duplicate Material Context menu is used on an inactive object.
756 //if (m_renderer == null)
757 // m_renderer = GetComponent<Renderer>();
758
759 // Create Instance Material only if the new material is not the same instance previously used.
760 if (m_material == null || m_material.GetInstanceID() != mat.GetInstanceID())
761 m_material = CreateMaterialInstance(mat);
762
763 m_sharedMaterial = m_material;
764
765 // Compute and Set new padding values for this new material.
766 m_padding = GetPaddingForMaterial();
767
768 SetVerticesDirty();
769 SetMaterialDirty();
770
771 return m_sharedMaterial;
772 }
773
774
775 /// <summary>
776 /// Method used to create an instance of the material
777 /// </summary>
778 /// <param name="source"></param>
779 /// <returns></returns>
780 Material CreateMaterialInstance(Material source)
781 {
782 Material mat = new Material(source);
783 mat.shaderKeywords = source.shaderKeywords;
784 mat.name += " (Instance)";
785
786 return mat;
787 }
788
789
790 /// <summary>
791 /// Method returning the shared material assigned to the text object.
792 /// </summary>
793 /// <returns></returns>
794 Material GetSharedMaterial()
795 {
796 //if (canvasRenderer == null)
797 // canvasRenderer = GetComponent<CanvasRenderer>();
798
799 return canvasRenderer.GetMaterial();
800 }
801
802
803 /// <summary>
804 /// Method to set the shared material.
805 /// </summary>
806 /// <param name="mat"></param>
807 void SetSharedMaterial(Material mat)
808 {
809 //Debug.Log("*** SetSharedMaterial UI() *** FRAME (" + Time.frameCount + ")");
810
811 // Assign new material.
812 m_sharedMaterial = mat;
813 m_Material = m_sharedMaterial;
814
815 //m_isDefaultMaterial = false;
816 //if (mat.GetInstanceID() == m_fontAsset.material.GetInstanceID())
817 // m_isDefaultMaterial = true;
818
819 // Compute and Set new padding values for this new material.
820 m_padding = GetPaddingForMaterial();
821
822 //SetVerticesDirty();
823 SetMaterialDirty();
824
825 #if UNITY_EDITOR
826 //if (m_sharedMaterial != null)
827 // gameObject.name = "TMP SubMesh [" + m_sharedMaterial.name + "]";
828 #endif
829 }
830 }
831}