123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections.Generic;
- #if CURVEDUI_TMP || TMP_PRESENT
- using TMPro;
- #endif
- namespace CurvedUI
- {
- [ExecuteInEditMode]
- public class CurvedUITMP : MonoBehaviour
- {
- #if CURVEDUI_TMP || TMP_PRESENT
-
- CurvedUIVertexEffect crvdVE;
- TextMeshProUGUI tmpText;
- CurvedUISettings mySettings;
- List<UIVertex> m_UIVerts = new List<UIVertex>();
- UIVertex m_tempVertex;
- CurvedUITMPSubmesh m_tempSubMsh;
- Vector2 savedSize;
- Vector3 savedUp;
- Vector3 savedPos;
- Vector3 savedLocalScale;
- Vector3 savedGlobalScale;
- List<CurvedUITMPSubmesh> subMeshes = new List<CurvedUITMPSubmesh>();
-
- public bool Dirty = false;
- bool curvingRequired = false;
- bool tesselationRequired = false;
- bool quitting = false;
-
- private Vector3[] vertices;
-
-
-
-
-
-
-
-
-
-
-
- #region LIFECYCLE
- void Start()
- {
- if (mySettings == null)
- mySettings = GetComponentInParent<CurvedUISettings>();
- }
- void OnEnable()
- {
- FindTMP();
- if (tmpText)
- {
- tmpText.RegisterDirtyMaterialCallback(TesselationRequiredCallback);
- TMPro_EventManager.TEXT_CHANGED_EVENT.Add(TMPTextChangedCallback);
- tmpText.SetText(tmpText.text);
- }
- #if UNITY_EDITOR
- if (!Application.isPlaying)
- UnityEditor.EditorApplication.update += LateUpdate;
- #endif
- }
- void OnDisable()
- {
- #if UNITY_EDITOR
- if (!Application.isPlaying)
- UnityEditor.EditorApplication.update -= LateUpdate;
- #endif
- if (tmpText)
- {
- tmpText.UnregisterDirtyMaterialCallback(TesselationRequiredCallback);
- TMPro_EventManager.TEXT_CHANGED_EVENT.Remove(TMPTextChangedCallback);
- }
- }
- void OnDestroy()
- {
- quitting = true;
- }
- void LateUpdate()
- {
-
- if (!tmpText) FindTMP();
- if (mySettings == null) return;
-
- if (tmpText && !quitting)
- {
- if (ShouldTesselate())
- tesselationRequired = true;
- if (Dirty || tesselationRequired || (curvingRequired && !Application.isPlaying))
- {
- if (mySettings == null)
- {
- enabled = false;
- return;
- }
-
-
- tmpText.renderMode = TMPro.TextRenderFlags.Render;
- tmpText.ForceMeshUpdate(true);
- CreateUIVertexList(tmpText.mesh);
-
- crvdVE.ModifyTMPMesh(ref m_UIVerts);
-
- FillMeshWithUIVertexList(tmpText.mesh, m_UIVerts);
-
- tmpText.renderMode = TMPro.TextRenderFlags.DontRender;
-
- savedLocalScale = mySettings.transform.localScale;
- savedGlobalScale = mySettings.transform.lossyScale;
- savedSize = (transform as RectTransform).rect.size;
- savedUp = mySettings.transform.worldToLocalMatrix.MultiplyVector(transform.up);
- savedPos = mySettings.transform.worldToLocalMatrix.MultiplyPoint3x4(transform.position);
-
- tesselationRequired = false;
- curvingRequired = false;
- Dirty = false;
-
- FindSubmeshes();
- foreach (CurvedUITMPSubmesh mesh in subMeshes)
- mesh.UpdateSubmesh(true, false);
- }
-
- tmpText.canvasRenderer.SetMesh(tmpText.mesh);
- }
- }
- #endregion
- #region UIVERTEX MANAGEMENT
- void CreateUIVertexList(Mesh mesh)
- {
-
- if (mesh.vertexCount < m_UIVerts.Count)
- m_UIVerts.RemoveRange(mesh.vertexCount, m_UIVerts.Count - mesh.vertexCount);
-
- vertices = mesh.vertices;
-
-
-
-
-
-
-
- for (int i = 0; i < mesh.vertexCount; i++)
- {
-
- if (m_UIVerts.Count <= i)
- {
- m_tempVertex = new UIVertex();
- GetUIVertexFromMesh(ref m_tempVertex, i);
- m_UIVerts.Add(m_tempVertex);
- }
- else
- {
- m_tempVertex = m_UIVerts[i];
- GetUIVertexFromMesh(ref m_tempVertex, i);
- m_UIVerts[i] = m_tempVertex;
- }
- }
-
- }
- void GetUIVertexFromMesh(ref UIVertex vert, int i)
- {
- vert.position = vertices[i];
-
-
-
-
-
-
-
- }
- void FillMeshWithUIVertexList(Mesh mesh, List<UIVertex> list)
- {
- if (list.Count >= 65536)
- {
- Debug.LogError("CURVEDUI: Unity UI Mesh can not have more than 65536 vertices. Remove some UI elements or lower quality.");
- return;
- }
- for (int i = 0; i < list.Count; i++)
- {
- vertices[i] = list[i].position;
-
-
-
-
- {
- tmpText = this.gameObject.GetComponent<TextMeshProUGUI>();
- crvdVE = this.gameObject.GetComponent<CurvedUIVertexEffect>();
- mySettings = GetComponentInParent<CurvedUISettings>();
- transform.hasChanged = false;
- FindSubmeshes();
- }
- }
- void FindSubmeshes()
- {
- foreach (TMP_SubMeshUI sub in GetComponentsInChildren<TMP_SubMeshUI>())
- {
- m_tempSubMsh = sub.gameObject.AddComponentIfMissing<CurvedUITMPSubmesh>();
- if (!subMeshes.Contains(m_tempSubMsh))
- subMeshes.Add(m_tempSubMsh);
- }
- }
- bool ShouldTesselate()
- {
- if (savedSize != (transform as RectTransform).rect.size)
- {
-
- return true;
- }
- else if (savedLocalScale != mySettings.transform.localScale)
- {
-
- return true;
- }
- else if (savedGlobalScale != mySettings.transform.lossyScale)
- {
-
- return true;
- }
- else if (!savedUp.AlmostEqual(mySettings.transform.worldToLocalMatrix.MultiplyVector(transform.up)))
- {
-
- return true;
- }
- Vector3 testedPos = mySettings.transform.worldToLocalMatrix.MultiplyPoint3x4(transform.position);
- if (!savedPos.AlmostEqual(testedPos))
- {
-
- if (mySettings.Shape != CurvedUISettings.CurvedUIShape.CYLINDER || Mathf.Pow(testedPos.x - savedPos.x, 2) > 0.00001 || Mathf.Pow(testedPos.z - savedPos.z, 2) > 0.00001)
- {
-
- return true;
- }
- }
- return false;
- }
- #endregion
- #region EVENTS AND CALLBACKS
- void TMPTextChangedCallback(object obj)
- {
- if (obj != (object)tmpText) return;
- tesselationRequired = true;
-
- }
- void TesselationRequiredCallback()
- {
- tesselationRequired = true;
- curvingRequired = true;
- }
- #endregion
- #endif
- }
- }
|