TMP_SubMeshUI.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. #pragma warning disable 0414 // Disabled a few warnings related to serialized variables not used in this script but used in the editor.
  6. namespace TMPro
  7. {
  8. [ExecuteAlways]
  9. public class TMP_SubMeshUI : MaskableGraphic, IClippable, IMaskable, IMaterialModifier
  10. {
  11. /// <summary>
  12. /// The TMP Font Asset assigned to this sub text object.
  13. /// </summary>
  14. public TMP_FontAsset fontAsset
  15. {
  16. get { return m_fontAsset; }
  17. set { m_fontAsset = value; }
  18. }
  19. [SerializeField]
  20. private TMP_FontAsset m_fontAsset;
  21. /// <summary>
  22. /// The TMP Sprite Asset assigned to this sub text object.
  23. /// </summary>
  24. public TMP_SpriteAsset spriteAsset
  25. {
  26. get { return m_spriteAsset; }
  27. set { m_spriteAsset = value; }
  28. }
  29. [SerializeField]
  30. private TMP_SpriteAsset m_spriteAsset;
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. public override Texture mainTexture
  35. {
  36. get
  37. {
  38. if (this.sharedMaterial != null)
  39. return this.sharedMaterial.GetTexture(ShaderUtilities.ID_MainTex);
  40. return null;
  41. }
  42. }
  43. /// <summary>
  44. /// The material to be assigned to this object. Returns an instance of the material.
  45. /// </summary>
  46. public override Material material
  47. {
  48. // Return a new Instance of the Material if none exists. Otherwise return the current Material Instance.
  49. get { return GetMaterial(m_sharedMaterial); }
  50. // Assign new font material
  51. set
  52. {
  53. if (m_sharedMaterial != null && m_sharedMaterial.GetInstanceID() == value.GetInstanceID())
  54. return;
  55. m_sharedMaterial = m_material = value;
  56. m_padding = GetPaddingForMaterial();
  57. SetVerticesDirty();
  58. SetMaterialDirty();
  59. }
  60. }
  61. [SerializeField]
  62. private Material m_material;
  63. /// <summary>
  64. /// The material to be assigned to this text object.
  65. /// </summary>
  66. public Material sharedMaterial
  67. {
  68. get { return m_sharedMaterial; }
  69. set { SetSharedMaterial(value); }
  70. }
  71. [SerializeField]
  72. private Material m_sharedMaterial;
  73. /// <summary>
  74. ///
  75. /// </summary>
  76. public Material fallbackMaterial
  77. {
  78. get { return m_fallbackMaterial; }
  79. set
  80. {
  81. if (m_fallbackMaterial == value) return;
  82. if (m_fallbackMaterial != null && m_fallbackMaterial != value)
  83. TMP_MaterialManager.ReleaseFallbackMaterial(m_fallbackMaterial);
  84. m_fallbackMaterial = value;
  85. TMP_MaterialManager.AddFallbackMaterialReference(m_fallbackMaterial);
  86. SetSharedMaterial(m_fallbackMaterial);
  87. }
  88. }
  89. private Material m_fallbackMaterial;
  90. /// <summary>
  91. /// The source material used by the fallback font
  92. /// </summary>
  93. public Material fallbackSourceMaterial
  94. {
  95. get { return m_fallbackSourceMaterial; }
  96. set { m_fallbackSourceMaterial = value; }
  97. }
  98. private Material m_fallbackSourceMaterial;
  99. /// <summary>
  100. /// Get the material that will be used for rendering.
  101. /// </summary>
  102. public override Material materialForRendering
  103. {
  104. get
  105. {
  106. return TMP_MaterialManager.GetMaterialForRendering(this, m_sharedMaterial);
  107. }
  108. }
  109. /// <summary>
  110. /// Is the text object using the default font asset material.
  111. /// </summary>
  112. public bool isDefaultMaterial
  113. {
  114. get { return m_isDefaultMaterial; }
  115. set { m_isDefaultMaterial = value; }
  116. }
  117. [SerializeField]
  118. private bool m_isDefaultMaterial;
  119. /// <summary>
  120. /// Padding value resulting for the property settings on the material.
  121. /// </summary>
  122. public float padding
  123. {
  124. get { return m_padding; }
  125. set { m_padding = value; }
  126. }
  127. [SerializeField]
  128. private float m_padding;
  129. /// <summary>
  130. /// The Mesh Renderer of this text sub object.
  131. /// </summary>
  132. public new CanvasRenderer canvasRenderer
  133. {
  134. get { if (m_canvasRenderer == null) m_canvasRenderer = GetComponent<CanvasRenderer>();
  135. return m_canvasRenderer;
  136. }
  137. }
  138. [SerializeField]
  139. private CanvasRenderer m_canvasRenderer;
  140. /// <summary>
  141. /// The Mesh of this text sub object.
  142. /// </summary>
  143. public Mesh mesh
  144. {
  145. get
  146. {
  147. if (m_mesh == null)
  148. {
  149. m_mesh = new Mesh();
  150. m_mesh.hideFlags = HideFlags.HideAndDontSave;
  151. }
  152. return m_mesh;
  153. }
  154. set { m_mesh = value; }
  155. }
  156. private Mesh m_mesh;
  157. [SerializeField]
  158. private TextMeshProUGUI m_TextComponent;
  159. [System.NonSerialized]
  160. private bool m_isRegisteredForEvents;
  161. private bool m_materialDirty;
  162. [SerializeField]
  163. private int m_materialReferenceIndex;
  164. /// <summary>
  165. /// Function to add a new sub text object.
  166. /// </summary>
  167. /// <param name="textComponent"></param>
  168. /// <param name="materialReference"></param>
  169. /// <returns></returns>
  170. public static TMP_SubMeshUI AddSubTextObject(TextMeshProUGUI textComponent, MaterialReference materialReference)
  171. {
  172. GameObject go = new GameObject("TMP UI SubObject [" + materialReference.material.name + "]", typeof(RectTransform));
  173. go.transform.SetParent(textComponent.transform, false);
  174. go.layer = textComponent.gameObject.layer;
  175. RectTransform rectTransform = go.GetComponent<RectTransform>();
  176. rectTransform.anchorMin = Vector2.zero;
  177. rectTransform.anchorMax = Vector2.one;
  178. rectTransform.sizeDelta = Vector2.zero;
  179. rectTransform.pivot = textComponent.rectTransform.pivot;
  180. TMP_SubMeshUI subMesh = go.AddComponent<TMP_SubMeshUI>();
  181. subMesh.m_canvasRenderer = subMesh.canvasRenderer;
  182. subMesh.m_TextComponent = textComponent;
  183. subMesh.m_materialReferenceIndex = materialReference.index;
  184. subMesh.m_fontAsset = materialReference.fontAsset;
  185. subMesh.m_spriteAsset = materialReference.spriteAsset;
  186. subMesh.m_isDefaultMaterial = materialReference.isDefaultMaterial;
  187. subMesh.SetSharedMaterial(materialReference.material);
  188. return subMesh;
  189. }
  190. /// <summary>
  191. ///
  192. /// </summary>
  193. protected override void OnEnable()
  194. {
  195. //Debug.Log("*** SubObject OnEnable() ***");
  196. // Register Callbacks for various events.
  197. if (!m_isRegisteredForEvents)
  198. {
  199. #if UNITY_EDITOR
  200. TMPro_EventManager.MATERIAL_PROPERTY_EVENT.Add(ON_MATERIAL_PROPERTY_CHANGED);
  201. TMPro_EventManager.FONT_PROPERTY_EVENT.Add(ON_FONT_PROPERTY_CHANGED);
  202. //TMPro_EventManager.TEXTMESHPRO_PROPERTY_EVENT.Add(ON_TEXTMESHPRO_PROPERTY_CHANGED);
  203. TMPro_EventManager.DRAG_AND_DROP_MATERIAL_EVENT.Add(ON_DRAG_AND_DROP_MATERIAL);
  204. //TMPro_EventManager.TEXT_STYLE_PROPERTY_EVENT.Add(ON_TEXT_STYLE_CHANGED);
  205. TMPro_EventManager.SPRITE_ASSET_PROPERTY_EVENT.Add(ON_SPRITE_ASSET_PROPERTY_CHANGED);
  206. //TMPro_EventManager.TMP_SETTINGS_PROPERTY_EVENT.Add(ON_TMP_SETTINGS_CHANGED);
  207. #endif
  208. m_isRegisteredForEvents = true;
  209. }
  210. m_ShouldRecalculateStencil = true;
  211. RecalculateClipping();
  212. RecalculateMasking();
  213. //SetAllDirty();
  214. }
  215. protected override void OnDisable()
  216. {
  217. //Debug.Log("*** SubObject OnDisable() ***");
  218. //m_canvasRenderer.Clear();
  219. TMP_UpdateRegistry.UnRegisterCanvasElementForRebuild(this);
  220. if (m_MaskMaterial != null)
  221. {
  222. TMP_MaterialManager.ReleaseStencilMaterial(m_MaskMaterial);
  223. m_MaskMaterial = null;
  224. }
  225. if (m_fallbackMaterial != null)
  226. {
  227. TMP_MaterialManager.ReleaseFallbackMaterial(m_fallbackMaterial);
  228. m_fallbackMaterial = null;
  229. }
  230. base.OnDisable();
  231. }
  232. protected override void OnDestroy()
  233. {
  234. //Debug.Log("*** OnDestroy() ***");
  235. // Destroy Mesh
  236. if (m_mesh != null) DestroyImmediate(m_mesh);
  237. if (m_MaskMaterial != null)
  238. TMP_MaterialManager.ReleaseStencilMaterial(m_MaskMaterial);
  239. if (m_fallbackMaterial != null)
  240. {
  241. TMP_MaterialManager.ReleaseFallbackMaterial(m_fallbackMaterial);
  242. m_fallbackMaterial = null;
  243. }
  244. #if UNITY_EDITOR
  245. // Unregister the event this object was listening to
  246. TMPro_EventManager.MATERIAL_PROPERTY_EVENT.Remove(ON_MATERIAL_PROPERTY_CHANGED);
  247. TMPro_EventManager.FONT_PROPERTY_EVENT.Remove(ON_FONT_PROPERTY_CHANGED);
  248. //TMPro_EventManager.TEXTMESHPRO_PROPERTY_EVENT.Remove(ON_TEXTMESHPRO_PROPERTY_CHANGED);
  249. TMPro_EventManager.DRAG_AND_DROP_MATERIAL_EVENT.Remove(ON_DRAG_AND_DROP_MATERIAL);
  250. //TMPro_EventManager.TEXT_STYLE_PROPERTY_EVENT.Remove(ON_TEXT_STYLE_CHANGED);
  251. TMPro_EventManager.SPRITE_ASSET_PROPERTY_EVENT.Remove(ON_SPRITE_ASSET_PROPERTY_CHANGED);
  252. //TMPro_EventManager.TMP_SETTINGS_PROPERTY_EVENT.Remove(ON_TMP_SETTINGS_CHANGED);
  253. #endif
  254. m_isRegisteredForEvents = false;
  255. RecalculateClipping();
  256. }
  257. #if UNITY_EDITOR
  258. // Event received when custom material editor properties are changed.
  259. void ON_MATERIAL_PROPERTY_CHANGED(bool isChanged, Material mat)
  260. {
  261. //Debug.Log("*** ON_MATERIAL_PROPERTY_CHANGED ***");
  262. int targetMaterialID = mat.GetInstanceID();
  263. int sharedMaterialID = m_sharedMaterial.GetInstanceID();
  264. int maskingMaterialID = m_MaskMaterial == null ? 0 : m_MaskMaterial.GetInstanceID();
  265. int fallbackSourceMaterialID = m_fallbackSourceMaterial == null ? 0 : m_fallbackSourceMaterial.GetInstanceID();
  266. // Filter events and return if the affected material is not this object's material.
  267. //if (targetMaterialID != sharedMaterialID && targetMaterialID != maskingMaterialID) return;
  268. // Filter events and return if the affected material is not this object's material.
  269. if (m_fallbackMaterial != null && fallbackSourceMaterialID == targetMaterialID)
  270. TMP_MaterialManager.CopyMaterialPresetProperties(mat, m_fallbackMaterial);
  271. if (m_TextComponent == null) m_TextComponent = GetComponentInParent<TextMeshProUGUI>();
  272. // Make sure material properties are synchronized between the assigned material and masking material.
  273. if (m_MaskMaterial != null)
  274. {
  275. UnityEditor.Undo.RecordObject(m_MaskMaterial, "Material Property Changes");
  276. UnityEditor.Undo.RecordObject(m_sharedMaterial, "Material Property Changes");
  277. if (targetMaterialID == sharedMaterialID)
  278. {
  279. //Debug.Log("Copy base material properties to masking material if not null.");
  280. float stencilID = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilID);
  281. float stencilComp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilComp);
  282. m_MaskMaterial.CopyPropertiesFromMaterial(mat);
  283. m_MaskMaterial.shaderKeywords = mat.shaderKeywords;
  284. m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID);
  285. m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
  286. }
  287. else if (targetMaterialID == maskingMaterialID)
  288. {
  289. // Update the padding
  290. GetPaddingForMaterial(mat);
  291. m_sharedMaterial.CopyPropertiesFromMaterial(mat);
  292. m_sharedMaterial.shaderKeywords = mat.shaderKeywords;
  293. m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilID, 0);
  294. m_sharedMaterial.SetFloat(ShaderUtilities.ID_StencilComp, 8);
  295. }
  296. else if (fallbackSourceMaterialID == targetMaterialID)
  297. {
  298. float stencilID = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilID);
  299. float stencilComp = m_MaskMaterial.GetFloat(ShaderUtilities.ID_StencilComp);
  300. m_MaskMaterial.CopyPropertiesFromMaterial(m_fallbackMaterial);
  301. m_MaskMaterial.shaderKeywords = m_fallbackMaterial.shaderKeywords;
  302. m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilID, stencilID);
  303. m_MaskMaterial.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
  304. }
  305. }
  306. m_padding = GetPaddingForMaterial();
  307. SetVerticesDirty();
  308. m_ShouldRecalculateStencil = true;
  309. RecalculateClipping();
  310. RecalculateMasking();
  311. }
  312. // Event to Track Material Changed resulting from Drag-n-drop.
  313. void ON_DRAG_AND_DROP_MATERIAL(GameObject obj, Material currentMaterial, Material newMaterial)
  314. {
  315. // Check if event applies to this current object
  316. #if UNITY_2018_2_OR_NEWER
  317. if (obj == gameObject || UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(gameObject) == obj)
  318. #else
  319. if (obj == gameObject || UnityEditor.PrefabUtility.GetPrefabParent(gameObject) == obj)
  320. #endif
  321. {
  322. if (!m_isDefaultMaterial) return;
  323. // Make sure we have a valid reference to the renderer.
  324. if (m_canvasRenderer == null) m_canvasRenderer = GetComponent<CanvasRenderer>();
  325. UnityEditor.Undo.RecordObject(this, "Material Assignment");
  326. UnityEditor.Undo.RecordObject(m_canvasRenderer, "Material Assignment");
  327. SetSharedMaterial(newMaterial);
  328. m_TextComponent.havePropertiesChanged = true;
  329. }
  330. }
  331. // Event received when font asset properties are changed in Font Inspector
  332. void ON_SPRITE_ASSET_PROPERTY_CHANGED(bool isChanged, UnityEngine.Object obj)
  333. {
  334. //if (spriteSheet != null && (obj as TMP_SpriteAsset == m_spriteAsset || obj as Texture2D == m_spriteAsset.spriteSheet))
  335. //{
  336. if (m_TextComponent != null)
  337. {
  338. m_TextComponent.havePropertiesChanged = true;
  339. //m_TextComponent.SetVerticesDirty();
  340. }
  341. //}
  342. }
  343. // Event received when font asset properties are changed in Font Inspector
  344. void ON_FONT_PROPERTY_CHANGED(bool isChanged, TMP_FontAsset font)
  345. {
  346. if (m_fontAsset != null && font.GetInstanceID() == m_fontAsset.GetInstanceID())
  347. {
  348. // Copy Normal and Bold Weight
  349. if (m_fallbackMaterial != null)
  350. {
  351. m_fallbackMaterial.SetFloat(ShaderUtilities.ID_WeightNormal, m_fontAsset.normalStyle);
  352. m_fallbackMaterial.SetFloat(ShaderUtilities.ID_WeightBold, m_fontAsset.boldStyle);
  353. }
  354. }
  355. }
  356. /// <summary>
  357. /// Event received when the TMP Settings are changed.
  358. /// </summary>
  359. void ON_TMP_SETTINGS_CHANGED()
  360. {
  361. //Debug.Log("TMP Setting have changed.");
  362. //SetVerticesDirty();
  363. //SetMaterialDirty();
  364. }
  365. #endif
  366. /// <summary>
  367. ///
  368. /// </summary>
  369. protected override void OnTransformParentChanged()
  370. {
  371. if (!this.IsActive())
  372. return;
  373. m_ShouldRecalculateStencil = true;
  374. RecalculateClipping();
  375. RecalculateMasking();
  376. }
  377. /// <summary>
  378. /// Function returning the modified material for masking if necessary.
  379. /// </summary>
  380. /// <param name="baseMaterial"></param>
  381. /// <returns></returns>
  382. public override Material GetModifiedMaterial(Material baseMaterial)
  383. {
  384. Material mat = baseMaterial;
  385. if (m_ShouldRecalculateStencil)
  386. {
  387. m_StencilValue = TMP_MaterialManager.GetStencilID(gameObject);
  388. m_ShouldRecalculateStencil = false;
  389. }
  390. if (m_StencilValue > 0)
  391. {
  392. mat = TMP_MaterialManager.GetStencilMaterial(baseMaterial, m_StencilValue);
  393. if (m_MaskMaterial != null)
  394. TMP_MaterialManager.ReleaseStencilMaterial(m_MaskMaterial);
  395. m_MaskMaterial = mat;
  396. }
  397. return mat;
  398. }
  399. /// <summary>
  400. /// Function called when the padding value for the material needs to be re-calculated.
  401. /// </summary>
  402. /// <returns></returns>
  403. public float GetPaddingForMaterial()
  404. {
  405. float padding = ShaderUtilities.GetPadding(m_sharedMaterial, m_TextComponent.extraPadding, m_TextComponent.isUsingBold);
  406. return padding;
  407. }
  408. /// <summary>
  409. /// Function called when the padding value for the material needs to be re-calculated.
  410. /// </summary>
  411. /// <returns></returns>
  412. public float GetPaddingForMaterial(Material mat)
  413. {
  414. float padding = ShaderUtilities.GetPadding(mat, m_TextComponent.extraPadding, m_TextComponent.isUsingBold);
  415. return padding;
  416. }
  417. /// <summary>
  418. ///
  419. /// </summary>
  420. /// <param name="isExtraPadding"></param>
  421. /// <param name="isBold"></param>
  422. public void UpdateMeshPadding(bool isExtraPadding, bool isUsingBold)
  423. {
  424. m_padding = ShaderUtilities.GetPadding(m_sharedMaterial, isExtraPadding, isUsingBold);
  425. }
  426. /// <summary>
  427. ///
  428. /// </summary>
  429. public override void SetAllDirty()
  430. {
  431. //SetLayoutDirty();
  432. //SetVerticesDirty();
  433. //SetMaterialDirty();
  434. }
  435. /// <summary>
  436. ///
  437. /// </summary>
  438. public override void SetVerticesDirty()
  439. {
  440. if (!this.IsActive())
  441. return;
  442. // This is called on the parent TextMeshPro component.
  443. if (m_TextComponent != null)
  444. {
  445. m_TextComponent.havePropertiesChanged = true;
  446. m_TextComponent.SetVerticesDirty();
  447. }
  448. }
  449. /// <summary>
  450. ///
  451. /// </summary>
  452. public override void SetLayoutDirty()
  453. {
  454. }
  455. /// <summary>
  456. ///
  457. /// </summary>
  458. public override void SetMaterialDirty()
  459. {
  460. //Debug.Log("*** STO-UI - SetMaterialDirty() *** FRAME (" + Time.frameCount + ")");
  461. //if (!this.IsActive())
  462. // return;
  463. m_materialDirty = true;
  464. UpdateMaterial();
  465. if (m_OnDirtyMaterialCallback != null)
  466. m_OnDirtyMaterialCallback();
  467. //TMP_ITextElementUpdateManager.RegisterTextElementForGraphicRebuild(this);
  468. //TMP_UpdateRegistry.RegisterCanvasElementForGraphicRebuild((ICanvasElement)this);
  469. //m_TextComponent.SetMaterialDirty();
  470. }
  471. /// <summary>
  472. ///
  473. /// </summary>
  474. public void SetPivotDirty()
  475. {
  476. if (!this.IsActive())
  477. return;
  478. this.rectTransform.pivot = m_TextComponent.rectTransform.pivot;
  479. }
  480. /// <summary>
  481. /// Override to Cull function of MaskableGraphic to prevent Culling.
  482. /// </summary>
  483. /// <param name="clipRect"></param>
  484. /// <param name="validRect"></param>
  485. public override void Cull(Rect clipRect, bool validRect)
  486. {
  487. if (m_TextComponent.ignoreRectMaskCulling) return;
  488. base.Cull(clipRect, validRect);
  489. }
  490. /// <summary>
  491. ///
  492. /// </summary>
  493. protected override void UpdateGeometry()
  494. {
  495. // Need to override to prevent Unity from changing the geometry of the object.
  496. Debug.Log("UpdateGeometry()");
  497. }
  498. /// <summary>
  499. ///
  500. /// </summary>
  501. /// <param name="update"></param>
  502. public override void Rebuild(CanvasUpdate update)
  503. {
  504. if (update == CanvasUpdate.PreRender)
  505. {
  506. if (!m_materialDirty) return;
  507. UpdateMaterial();
  508. m_materialDirty = false;
  509. }
  510. }
  511. /// <summary>
  512. /// Function to update the material from the parent text object.
  513. /// </summary>
  514. public void RefreshMaterial()
  515. {
  516. UpdateMaterial();
  517. }
  518. /// <summary>
  519. ///
  520. /// </summary>
  521. protected override void UpdateMaterial()
  522. {
  523. //Debug.Log("*** STO-UI - UpdateMaterial() *** FRAME (" + Time.frameCount + ")");
  524. //if (!this.IsActive())
  525. // return;
  526. if (m_canvasRenderer == null) m_canvasRenderer = this.canvasRenderer;
  527. m_canvasRenderer.materialCount = 1;
  528. m_canvasRenderer.SetMaterial(materialForRendering, 0);
  529. m_canvasRenderer.SetTexture(mainTexture);
  530. #if UNITY_EDITOR
  531. if (m_sharedMaterial != null && gameObject.name != "TMP SubMeshUI [" + m_sharedMaterial.name + "]")
  532. gameObject.name = "TMP SubMeshUI [" + m_sharedMaterial.name + "]";
  533. #endif
  534. }
  535. // IClippable implementation
  536. /// <summary>
  537. /// Method called when the state of a parent changes.
  538. /// </summary>
  539. public override void RecalculateClipping()
  540. {
  541. //Debug.Log("*** RecalculateClipping() ***");
  542. base.RecalculateClipping();
  543. }
  544. /// <summary>
  545. ///
  546. /// </summary>
  547. public override void RecalculateMasking()
  548. {
  549. //Debug.Log("RecalculateMasking()");
  550. this.m_ShouldRecalculateStencil = true;
  551. SetMaterialDirty();
  552. }
  553. /// <summary>
  554. /// Method which returns an instance of the shared material
  555. /// </summary>
  556. /// <returns></returns>
  557. Material GetMaterial()
  558. {
  559. // Make sure we have a valid reference to the renderer.
  560. //if (m_renderer == null) m_renderer = GetComponent<Renderer>();
  561. //if (m_material == null || m_isNewSharedMaterial)
  562. //{
  563. // m_renderer.material = m_sharedMaterial;
  564. // m_material = m_renderer.material;
  565. // m_sharedMaterial = m_material;
  566. // m_padding = ShaderUtilities.GetPadding(m_sharedMaterial, m_TextMeshPro.extraPadding, false);
  567. // m_isNewSharedMaterial = false;
  568. //}
  569. return m_sharedMaterial;
  570. }
  571. // Function called internally when a new material is assigned via the fontMaterial property.
  572. Material GetMaterial(Material mat)
  573. {
  574. // Check in case Object is disabled. If so, we don't have a valid reference to the Renderer.
  575. // This can occur when the Duplicate Material Context menu is used on an inactive object.
  576. //if (m_renderer == null)
  577. // m_renderer = GetComponent<Renderer>();
  578. // Create Instance Material only if the new material is not the same instance previously used.
  579. if (m_material == null || m_material.GetInstanceID() != mat.GetInstanceID())
  580. m_material = CreateMaterialInstance(mat);
  581. m_sharedMaterial = m_material;
  582. // Compute and Set new padding values for this new material.
  583. m_padding = GetPaddingForMaterial();
  584. SetVerticesDirty();
  585. SetMaterialDirty();
  586. return m_sharedMaterial;
  587. }
  588. /// <summary>
  589. /// Method used to create an instance of the material
  590. /// </summary>
  591. /// <param name="source"></param>
  592. /// <returns></returns>
  593. Material CreateMaterialInstance(Material source)
  594. {
  595. Material mat = new Material(source);
  596. mat.shaderKeywords = source.shaderKeywords;
  597. mat.name += " (Instance)";
  598. return mat;
  599. }
  600. /// <summary>
  601. /// Method returning the shared material assigned to the text object.
  602. /// </summary>
  603. /// <returns></returns>
  604. Material GetSharedMaterial()
  605. {
  606. if (m_canvasRenderer == null)
  607. m_canvasRenderer = GetComponent<CanvasRenderer>();
  608. return m_canvasRenderer.GetMaterial();
  609. }
  610. /// <summary>
  611. /// Method to set the shared material.
  612. /// </summary>
  613. /// <param name="mat"></param>
  614. void SetSharedMaterial(Material mat)
  615. {
  616. //Debug.Log("*** SetSharedMaterial UI() *** FRAME (" + Time.frameCount + ")");
  617. // Assign new material.
  618. m_sharedMaterial = mat;
  619. m_Material = m_sharedMaterial;
  620. //m_isDefaultMaterial = false;
  621. //if (mat.GetInstanceID() == m_fontAsset.material.GetInstanceID())
  622. // m_isDefaultMaterial = true;
  623. // Compute and Set new padding values for this new material.
  624. m_padding = GetPaddingForMaterial();
  625. //SetVerticesDirty();
  626. SetMaterialDirty();
  627. #if UNITY_EDITOR
  628. //if (m_sharedMaterial != null)
  629. // gameObject.name = "TMP SubMesh [" + m_sharedMaterial.name + "]";
  630. #endif
  631. }
  632. }
  633. }