You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

581 lines
19 KiB

  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. #pragma warning disable 0109 // Disable warning due to conflict between Unity Editor DLL and Runtime DLL related to .renderer property being available in one but not the other.
  5. namespace TMPro
  6. {
  7. [ExecuteInEditMode]
  8. [RequireComponent(typeof(MeshRenderer))]
  9. [RequireComponent(typeof(MeshFilter))]
  10. public class TMP_SubMesh : MonoBehaviour
  11. {
  12. /// <summary>
  13. /// The TMP Font Asset assigned to this sub text object.
  14. /// </summary>
  15. public TMP_FontAsset fontAsset
  16. {
  17. get { return m_fontAsset; }
  18. set { m_fontAsset = value; }
  19. }
  20. [SerializeField]
  21. private TMP_FontAsset m_fontAsset;
  22. /// <summary>
  23. /// The TMP Sprite Asset assigned to this sub text object.
  24. /// </summary>
  25. public TMP_SpriteAsset spriteAsset
  26. {
  27. get { return m_spriteAsset; }
  28. set { m_spriteAsset = value; }
  29. }
  30. [SerializeField]
  31. private TMP_SpriteAsset m_spriteAsset;
  32. /// <summary>
  33. /// The material to be assigned to this object. Returns an instance of the material.
  34. /// </summary>
  35. public Material material
  36. {
  37. // Return a new Instance of the Material if none exists. Otherwise return the current Material Instance.
  38. get { return GetMaterial(m_sharedMaterial); }
  39. // Assign new font material
  40. set
  41. {
  42. if (m_sharedMaterial.GetInstanceID() == value.GetInstanceID())
  43. return;
  44. m_sharedMaterial = m_material = value;
  45. m_padding = GetPaddingForMaterial();
  46. SetVerticesDirty();
  47. SetMaterialDirty();
  48. }
  49. }
  50. [SerializeField]
  51. private Material m_material;
  52. /// <summary>
  53. /// The material to be assigned to this text object.
  54. /// </summary>
  55. public Material sharedMaterial
  56. {
  57. get { return m_sharedMaterial; }
  58. set { SetSharedMaterial(value); }
  59. }
  60. [SerializeField]
  61. private Material m_sharedMaterial;
  62. /// <summary>
  63. /// The fallback material created from the properties of the fallback source material.
  64. /// </summary>
  65. public Material fallbackMaterial
  66. {
  67. get { return m_fallbackMaterial; }
  68. set
  69. {
  70. if (m_fallbackMaterial == value) return;
  71. if (m_fallbackMaterial != null && m_fallbackMaterial != value)
  72. TMP_MaterialManager.ReleaseFallbackMaterial(m_fallbackMaterial);
  73. m_fallbackMaterial = value;
  74. TMP_MaterialManager.AddFallbackMaterialReference(m_fallbackMaterial);
  75. SetSharedMaterial(m_fallbackMaterial);
  76. }
  77. }
  78. private Material m_fallbackMaterial;
  79. /// <summary>
  80. /// The source material used by the fallback font
  81. /// </summary>
  82. public Material fallbackSourceMaterial
  83. {
  84. get { return m_fallbackSourceMaterial; }
  85. set { m_fallbackSourceMaterial = value; }
  86. }
  87. private Material m_fallbackSourceMaterial;
  88. /// <summary>
  89. /// Is the text object using the default font asset material.
  90. /// </summary>
  91. public bool isDefaultMaterial
  92. {
  93. get { return m_isDefaultMaterial; }
  94. set { m_isDefaultMaterial = value; }
  95. }
  96. [SerializeField]
  97. private bool m_isDefaultMaterial;
  98. /// <summary>
  99. /// Padding value resulting for the property settings on the material.
  100. /// </summary>
  101. public float padding
  102. {
  103. get { return m_padding; }
  104. set { m_padding = value; }
  105. }
  106. [SerializeField]
  107. private float m_padding;
  108. /// <summary>
  109. /// The Mesh Renderer of this text sub object.
  110. /// </summary>
  111. public new Renderer renderer
  112. {
  113. get { if (m_renderer == null) m_renderer = GetComponent<Renderer>();
  114. return m_renderer;
  115. }
  116. }
  117. [SerializeField]
  118. private Renderer m_renderer;
  119. /// <summary>
  120. /// The MeshFilter of this text sub object.
  121. /// </summary>
  122. public MeshFilter meshFilter
  123. {
  124. get { if (m_meshFilter == null) m_meshFilter = GetComponent<MeshFilter>();
  125. return m_meshFilter;
  126. }
  127. }
  128. [SerializeField]
  129. private MeshFilter m_meshFilter;
  130. /// <summary>
  131. /// The Mesh of this text sub object.
  132. /// </summary>
  133. public Mesh mesh
  134. {
  135. get
  136. {
  137. if (m_mesh == null)
  138. {
  139. m_mesh = new Mesh();
  140. m_mesh.hideFlags = HideFlags.HideAndDontSave;
  141. this.meshFilter.mesh = m_mesh;
  142. }
  143. return m_mesh;
  144. }
  145. set { m_mesh = value; }
  146. }
  147. private Mesh m_mesh;
  148. /// <summary>
  149. ///
  150. /// </summary>
  151. //public BoxCollider boxCollider
  152. //{
  153. // get
  154. // {
  155. // if (m_boxCollider == null)
  156. // {
  157. // //
  158. // m_boxCollider = GetComponent<BoxCollider>();
  159. // if (m_boxCollider == null)
  160. // {
  161. // m_boxCollider = gameObject.AddComponent<BoxCollider>();
  162. // gameObject.AddComponent<Rigidbody>();
  163. // }
  164. // }
  165. // return m_boxCollider;
  166. // }
  167. //}
  168. //[SerializeField]
  169. //private BoxCollider m_boxCollider;
  170. [SerializeField]
  171. private TextMeshPro m_TextComponent;
  172. [NonSerialized]
  173. private bool m_isRegisteredForEvents;
  174. void OnEnable()
  175. {
  176. // Register Callbacks for various events.
  177. if (!m_isRegisteredForEvents)
  178. {
  179. #if UNITY_EDITOR
  180. TMPro_EventManager.MATERIAL_PROPERTY_EVENT.Add(ON_MATERIAL_PROPERTY_CHANGED);
  181. TMPro_EventManager.FONT_PROPERTY_EVENT.Add(ON_FONT_PROPERTY_CHANGED);
  182. //TMPro_EventManager.TEXTMESHPRO_PROPERTY_EVENT.Add(ON_TEXTMESHPRO_PROPERTY_CHANGED);
  183. TMPro_EventManager.DRAG_AND_DROP_MATERIAL_EVENT.Add(ON_DRAG_AND_DROP_MATERIAL);
  184. //TMPro_EventManager.TEXT_STYLE_PROPERTY_EVENT.Add(ON_TEXT_STYLE_CHANGED);
  185. TMPro_EventManager.SPRITE_ASSET_PROPERTY_EVENT.Add(ON_SPRITE_ASSET_PROPERTY_CHANGED);
  186. //TMPro_EventManager.TMP_SETTINGS_PROPERTY_EVENT.Add(ON_TMP_SETTINGS_CHANGED);
  187. #endif
  188. m_isRegisteredForEvents = true;
  189. }
  190. // Make the geometry visible when the object is enabled.
  191. meshFilter.sharedMesh = mesh;
  192. // Update _ClipRect values
  193. if (m_sharedMaterial != null)
  194. m_sharedMaterial.SetVector(ShaderUtilities.ID_ClipRect, new Vector4(-32767, -32767, 32767, 32767));
  195. }
  196. void OnDisable()
  197. {
  198. // Hide the geometry when the object is disabled.
  199. m_meshFilter.sharedMesh = null;
  200. if (m_fallbackMaterial != null)
  201. {
  202. TMP_MaterialManager.ReleaseFallbackMaterial(m_fallbackMaterial);
  203. m_fallbackMaterial = null;
  204. }
  205. }
  206. void OnDestroy()
  207. {
  208. // Destroy Mesh
  209. if (m_mesh != null) DestroyImmediate(m_mesh);
  210. if (m_fallbackMaterial != null)
  211. {
  212. TMP_MaterialManager.ReleaseFallbackMaterial(m_fallbackMaterial);
  213. m_fallbackMaterial = null;
  214. }
  215. #if UNITY_EDITOR
  216. // Unregister the event this object was listening to
  217. TMPro_EventManager.MATERIAL_PROPERTY_EVENT.Remove(ON_MATERIAL_PROPERTY_CHANGED);
  218. TMPro_EventManager.FONT_PROPERTY_EVENT.Remove(ON_FONT_PROPERTY_CHANGED);
  219. //TMPro_EventManager.TEXTMESHPRO_PROPERTY_EVENT.Remove(ON_TEXTMESHPRO_PROPERTY_CHANGED);
  220. TMPro_EventManager.DRAG_AND_DROP_MATERIAL_EVENT.Remove(ON_DRAG_AND_DROP_MATERIAL);
  221. //TMPro_EventManager.TEXT_STYLE_PROPERTY_EVENT.Remove(ON_TEXT_STYLE_CHANGED);
  222. TMPro_EventManager.SPRITE_ASSET_PROPERTY_EVENT.Remove(ON_SPRITE_ASSET_PROPERTY_CHANGED);
  223. //TMPro_EventManager.TMP_SETTINGS_PROPERTY_EVENT.Remove(ON_TMP_SETTINGS_CHANGED);
  224. #endif
  225. m_isRegisteredForEvents = false;
  226. }
  227. #if UNITY_EDITOR
  228. // Event received when custom material editor properties are changed.
  229. void ON_MATERIAL_PROPERTY_CHANGED(bool isChanged, Material mat)
  230. {
  231. //Debug.Log("*** ON_MATERIAL_PROPERTY_CHANGED ***");
  232. int targetMaterialID = mat.GetInstanceID();
  233. int sharedMaterialID = m_sharedMaterial.GetInstanceID();
  234. int fallbackSourceMaterialID = m_fallbackSourceMaterial == null ? 0 : m_fallbackSourceMaterial.GetInstanceID();
  235. // Filter events and return if the affected material is not this object's material.
  236. if (targetMaterialID != sharedMaterialID)
  237. {
  238. // Check if event applies to the source fallback material
  239. if (m_fallbackMaterial != null && fallbackSourceMaterialID == targetMaterialID)
  240. TMP_MaterialManager.CopyMaterialPresetProperties(mat, m_fallbackMaterial);
  241. else
  242. return;
  243. }
  244. if (m_TextComponent == null) m_TextComponent = GetComponentInParent<TextMeshPro>();
  245. m_padding = GetPaddingForMaterial();
  246. m_TextComponent.havePropertiesChanged = true;
  247. m_TextComponent.SetVerticesDirty();
  248. }
  249. // Event to Track Material Changed resulting from Drag-n-drop.
  250. void ON_DRAG_AND_DROP_MATERIAL(GameObject obj, Material currentMaterial, Material newMaterial)
  251. {
  252. // Check if event applies to this current object
  253. #if UNITY_2018_2_OR_NEWER
  254. if (obj == gameObject || UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(gameObject) == obj)
  255. #else
  256. if (obj == gameObject || UnityEditor.PrefabUtility.GetPrefabParent(gameObject) == obj)
  257. #endif
  258. {
  259. if (!m_isDefaultMaterial) return;
  260. // Make sure we have a valid reference to the renderer.
  261. if (m_renderer == null) m_renderer = GetComponent<Renderer>();
  262. UnityEditor.Undo.RecordObject(this, "Material Assignment");
  263. UnityEditor.Undo.RecordObject(m_renderer, "Material Assignment");
  264. SetSharedMaterial(newMaterial);
  265. m_TextComponent.havePropertiesChanged = true;
  266. }
  267. }
  268. // Event received when font asset properties are changed in Font Inspector
  269. void ON_SPRITE_ASSET_PROPERTY_CHANGED(bool isChanged, UnityEngine.Object obj)
  270. {
  271. //if (spriteSheet != null && (obj as TMP_SpriteAsset == m_spriteAsset || obj as Texture2D == m_spriteAsset.spriteSheet))
  272. //{
  273. if (m_TextComponent != null)
  274. {
  275. m_TextComponent.havePropertiesChanged = true;
  276. //m_TextComponent.SetVerticesDirty();
  277. }
  278. //}
  279. }
  280. // Event received when font asset properties are changed in Font Inspector
  281. void ON_FONT_PROPERTY_CHANGED(bool isChanged, TMP_FontAsset font)
  282. {
  283. if (m_fontAsset != null && font.GetInstanceID() == m_fontAsset.GetInstanceID())
  284. {
  285. // Copy Normal and Bold Weight
  286. if (m_fallbackMaterial != null)
  287. {
  288. m_fallbackMaterial.SetFloat(ShaderUtilities.ID_WeightNormal, m_fontAsset.normalStyle);
  289. m_fallbackMaterial.SetFloat(ShaderUtilities.ID_WeightBold, m_fontAsset.boldStyle);
  290. }
  291. }
  292. }
  293. /// <summary>
  294. /// Event received when the TMP Settings are changed.
  295. /// </summary>
  296. void ON_TMP_SETTINGS_CHANGED()
  297. {
  298. // //Debug.Log("TMP Setting have changed.");
  299. // //SetVerticesDirty();
  300. // SetMaterialDirty();
  301. }
  302. #endif
  303. public static TMP_SubMesh AddSubTextObject(TextMeshPro textComponent, MaterialReference materialReference)
  304. {
  305. GameObject go = new GameObject("TMP SubMesh [" + materialReference.material.name + "]", typeof(TMP_SubMesh));
  306. TMP_SubMesh subMesh = go.GetComponent<TMP_SubMesh>();
  307. go.transform.SetParent(textComponent.transform, false);
  308. go.transform.localPosition = Vector3.zero;
  309. go.transform.localRotation = Quaternion.identity;
  310. go.transform.localScale = Vector3.one;
  311. go.layer = textComponent.gameObject.layer;
  312. subMesh.m_meshFilter = go.GetComponent<MeshFilter>();
  313. subMesh.m_TextComponent = textComponent;
  314. subMesh.m_fontAsset = materialReference.fontAsset;
  315. subMesh.m_spriteAsset = materialReference.spriteAsset;
  316. subMesh.m_isDefaultMaterial = materialReference.isDefaultMaterial;
  317. subMesh.SetSharedMaterial(materialReference.material);
  318. subMesh.renderer.sortingLayerID = textComponent.renderer.sortingLayerID;
  319. subMesh.renderer.sortingOrder = textComponent.renderer.sortingOrder;
  320. return subMesh;
  321. }
  322. public void DestroySelf()
  323. {
  324. Destroy(this.gameObject, 1f);
  325. }
  326. // Function called internally when a new material is assigned via the fontMaterial property.
  327. Material GetMaterial(Material mat)
  328. {
  329. // Check in case Object is disabled. If so, we don't have a valid reference to the Renderer.
  330. // This can occur when the Duplicate Material Context menu is used on an inactive object.
  331. if (m_renderer == null)
  332. m_renderer = GetComponent<Renderer>();
  333. // Create Instance Material only if the new material is not the same instance previously used.
  334. if (m_material == null || m_material.GetInstanceID() != mat.GetInstanceID())
  335. m_material = CreateMaterialInstance(mat);
  336. m_sharedMaterial = m_material;
  337. // Compute and Set new padding values for this new material.
  338. m_padding = GetPaddingForMaterial();
  339. SetVerticesDirty();
  340. SetMaterialDirty();
  341. return m_sharedMaterial;
  342. }
  343. /// <summary>
  344. /// Method used to create an instance of the material
  345. /// </summary>
  346. /// <param name="source"></param>
  347. /// <returns></returns>
  348. Material CreateMaterialInstance(Material source)
  349. {
  350. Material mat = new Material(source);
  351. mat.shaderKeywords = source.shaderKeywords;
  352. mat.name += " (Instance)";
  353. return mat;
  354. }
  355. /// <summary>
  356. /// Method returning the shared material assigned to the text object.
  357. /// </summary>
  358. /// <returns></returns>
  359. Material GetSharedMaterial()
  360. {
  361. if (m_renderer == null)
  362. m_renderer = GetComponent<Renderer>();
  363. return m_renderer.sharedMaterial;
  364. }
  365. /// <summary>
  366. /// Method to set the shared material.
  367. /// </summary>
  368. /// <param name="mat"></param>
  369. void SetSharedMaterial(Material mat)
  370. {
  371. //Debug.Log("*** SetSharedMaterial() *** FRAME (" + Time.frameCount + ")");
  372. // Assign new material.
  373. m_sharedMaterial = mat;
  374. // Compute and Set new padding values for this new material.
  375. m_padding = GetPaddingForMaterial();
  376. SetMaterialDirty();
  377. #if UNITY_EDITOR
  378. if (m_sharedMaterial != null)
  379. gameObject.name = "TMP SubMesh [" + m_sharedMaterial.name + "]";
  380. #endif
  381. }
  382. /// <summary>
  383. /// Function called when the padding value for the material needs to be re-calculated.
  384. /// </summary>
  385. /// <returns></returns>
  386. public float GetPaddingForMaterial()
  387. {
  388. float padding = ShaderUtilities.GetPadding(m_sharedMaterial, m_TextComponent.extraPadding, m_TextComponent.isUsingBold);
  389. return padding;
  390. }
  391. /// <summary>
  392. /// Function to update the padding values of the object.
  393. /// </summary>
  394. /// <param name="isExtraPadding"></param>
  395. /// <param name="isBold"></param>
  396. public void UpdateMeshPadding(bool isExtraPadding, bool isUsingBold)
  397. {
  398. m_padding = ShaderUtilities.GetPadding(m_sharedMaterial, isExtraPadding, isUsingBold);
  399. }
  400. /// <summary>
  401. ///
  402. /// </summary>
  403. public void SetVerticesDirty()
  404. {
  405. if (!this.enabled)
  406. return;
  407. // This is called on the parent TextMeshPro component.
  408. if (m_TextComponent != null)
  409. {
  410. m_TextComponent.havePropertiesChanged = true;
  411. m_TextComponent.SetVerticesDirty();
  412. }
  413. }
  414. /// <summary>
  415. ///
  416. /// </summary>
  417. public void SetMaterialDirty()
  418. {
  419. //if (!this.enabled)
  420. // return;
  421. UpdateMaterial();
  422. //m_materialDirty = true;
  423. //TMP_UpdateRegistry.RegisterCanvasElementForGraphicRebuild((ICanvasElement)this);
  424. }
  425. /// <summary>
  426. ///
  427. /// </summary>
  428. protected void UpdateMaterial()
  429. {
  430. //Debug.Log("*** STO - UpdateMaterial() *** FRAME (" + Time.frameCount + ")");
  431. //if (!this.enabled)
  432. // return;
  433. if (m_renderer == null) m_renderer = this.renderer;
  434. m_renderer.sharedMaterial = m_sharedMaterial;
  435. #if UNITY_EDITOR
  436. if (m_sharedMaterial != null && gameObject.name != "TMP SubMesh [" + m_sharedMaterial.name + "]")
  437. gameObject.name = "TMP SubMesh [" + m_sharedMaterial.name + "]";
  438. #endif
  439. }
  440. /// <summary>
  441. ///
  442. /// </summary>
  443. //public void UpdateColliders(int vertexCount)
  444. //{
  445. // if (this.boxCollider == null) return;
  446. // Vector2 bl = TMP_Math.MAX_16BIT;
  447. // Vector2 tr = TMP_Math.MIN_16BIT;
  448. // // Compute the bounds of the sub text object mesh (excluding the transform position).
  449. // for (int i = 0; i < vertexCount; i++)
  450. // {
  451. // bl.x = Mathf.Min(bl.x, m_mesh.vertices[i].x);
  452. // bl.y = Mathf.Min(bl.y, m_mesh.vertices[i].y);
  453. // tr.x = Mathf.Max(tr.x, m_mesh.vertices[i].x);
  454. // tr.y = Mathf.Max(tr.y, m_mesh.vertices[i].y);
  455. // }
  456. // Vector3 center = (bl + tr) / 2;
  457. // Vector3 size = tr - bl;
  458. // size.z = .1f;
  459. // this.boxCollider.center = center;
  460. // this.boxCollider.size = size;
  461. //}
  462. }
  463. }