TMP_SubMeshUI.cs 28 KB

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