CurvedUITMPSubmesh.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. #if CURVEDUI_TMP || TMP_PRESENT
  6. using TMPro;
  7. #endif
  8. namespace CurvedUI
  9. {
  10. [ExecuteInEditMode]
  11. public class CurvedUITMPSubmesh : MonoBehaviour
  12. {
  13. #if CURVEDUI_TMP || TMP_PRESENT
  14. //saved references
  15. private VertexHelper vh;
  16. private Mesh straightMesh;
  17. private Mesh curvedMesh;
  18. private CurvedUIVertexEffect crvdVE;
  19. private TMP_SubMeshUI TMPsub;
  20. private TextMeshProUGUI TMPtext;
  21. public void UpdateSubmesh(bool tesselate, bool curve)
  22. {
  23. //find required components
  24. if (TMPsub == null) TMPsub = gameObject.GetComponent<TMP_SubMeshUI>();
  25. if (TMPsub == null) return;
  26. if (TMPtext == null)TMPtext = GetComponentInParent<TextMeshProUGUI>();
  27. if (crvdVE == null)crvdVE = gameObject.AddComponentIfMissing<CurvedUIVertexEffect>();
  28. //perform tesselatio and curving
  29. if (tesselate || straightMesh == null || vh == null || (!Application.isPlaying))
  30. {
  31. vh = new VertexHelper(TMPsub.mesh);
  32. //save straight mesh - it will be curved then every time the object moves on the canvas.
  33. straightMesh = new Mesh();
  34. vh.FillMesh(straightMesh);
  35. curve = true;
  36. }
  37. if (curve)
  38. {
  39. //Debug.Log("Submesh: Curve", this.gameObject);
  40. vh = new VertexHelper(straightMesh);
  41. crvdVE.ModifyMesh(vh);
  42. curvedMesh = new Mesh();
  43. vh.FillMesh(curvedMesh);
  44. crvdVE.CurvingRequired = true;
  45. }
  46. //upload mesh to TMP object's renderer
  47. TMPsub.canvasRenderer.SetMesh(curvedMesh);
  48. //cleanup for not needed submeshes.
  49. if (TMPtext != null && TMPtext.textInfo.materialCount < 2)
  50. {
  51. //Each submesh uses 1 additional material.
  52. //If materialCount is 1, this means Submesh is not needed. Bounce it to toggle cleanup.
  53. TMPsub.enabled = false;
  54. TMPsub.enabled = true;
  55. }
  56. }
  57. #endif
  58. }
  59. }