Skip to content

Commit 2dbe00a

Browse files
committed
release v0.2.0
2 parents e8110e6 + dbe9550 commit 2dbe00a

File tree

8 files changed

+201
-215
lines changed

8 files changed

+201
-215
lines changed

Assets/Coffee/UIExtensions/MeshEffectForTextMeshPro/Scripts/BaseMeshEffect.cs

Lines changed: 153 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
//#define NOT_USE_TMPRO // If your project does not use TMPro, uncomment this line.
32
using UnityEngine;
43
using UnityEngine.EventSystems;
54
using UnityEngine.UI;
5+
6+
#if !NOT_USE_TMPRO
7+
using System.Collections.Generic;
68
using TMPro;
9+
#endif
710

811
namespace Coffee.UIExtensions
912
{
@@ -17,6 +20,7 @@ public abstract class BaseMeshEffect : UIBehaviour, IMeshModifier
1720
//################################
1821
// Constant or Static Members.
1922
//################################
23+
#if !NOT_USE_TMPRO
2024
static readonly List<Vector2> s_Uv0 = new List<Vector2> ();
2125
static readonly List<Vector2> s_Uv1 = new List<Vector2> ();
2226
static readonly List<Vector3> s_Vertices = new List<Vector3> ();
@@ -27,6 +31,8 @@ public abstract class BaseMeshEffect : UIBehaviour, IMeshModifier
2731
static readonly VertexHelper s_VertexHelper = new VertexHelper ();
2832
static readonly List<TMP_SubMeshUI> s_SubMeshUIs = new List<TMP_SubMeshUI> ();
2933
static readonly List<Mesh> s_Meshes = new List<Mesh> ();
34+
#endif
35+
static readonly Material [] s_EmptyMaterials = new Material [0];
3036

3137

3238
//################################
@@ -35,17 +41,103 @@ public abstract class BaseMeshEffect : UIBehaviour, IMeshModifier
3541
/// <summary>
3642
/// The Graphic attached to this GameObject.
3743
/// </summary>
38-
public Graphic graphic { get { return _graphic ?? (_graphic = GetComponent<Graphic> ()); } }
44+
public Graphic graphic { get { Initialize (); return _graphic; } }
3945

4046
/// <summary>
4147
/// The CanvasRenderer attached to this GameObject.
4248
/// </summary>
43-
public CanvasRenderer canvasRenderer { get { return _canvasRenderer ?? (_canvasRenderer = GetComponent<CanvasRenderer> ()); } }
49+
public CanvasRenderer canvasRenderer { get { Initialize (); return _canvasRenderer; } }
4450

51+
#if !NOT_USE_TMPRO
4552
/// <summary>
4653
/// The TMP_Text attached to this GameObject.
4754
/// </summary>
48-
public TMP_Text textMeshPro { get { return _textMeshPro ?? (_textMeshPro = GetComponent<TMP_Text> ()); } }
55+
public TMP_Text textMeshPro { get { Initialize (); return _textMeshPro; } }
56+
#endif
57+
58+
/// <summary>
59+
/// The RectTransform attached to this GameObject.
60+
/// </summary>
61+
public RectTransform rectTransform { get { Initialize (); return _rectTransform; } }
62+
63+
/// <summary>
64+
/// Is TextMeshPro or TextMeshProUGUI attached to this GameObject?
65+
/// </summary>
66+
public bool isTMPro
67+
{
68+
get
69+
{
70+
#if !NOT_USE_TMPRO
71+
return textMeshPro != null;
72+
#else
73+
return false;
74+
#endif
75+
}
76+
}
77+
78+
/// <summary>
79+
/// The material for rendering.
80+
/// </summary>
81+
public virtual Material material
82+
{
83+
get
84+
{
85+
86+
#if !NOT_USE_TMPRO
87+
if (textMeshPro)
88+
{
89+
return textMeshPro.fontSharedMaterial;
90+
}
91+
else
92+
#endif
93+
if (graphic)
94+
{
95+
return graphic.material;
96+
}
97+
else
98+
{
99+
return null;
100+
}
101+
}
102+
set
103+
{
104+
#if !NOT_USE_TMPRO
105+
if (textMeshPro)
106+
{
107+
textMeshPro.fontSharedMaterial = value;
108+
}
109+
else
110+
#endif
111+
if (graphic)
112+
{
113+
graphic.material = value;
114+
}
115+
}
116+
}
117+
118+
public virtual Material[] materials
119+
{
120+
get
121+
{
122+
123+
#if !NOT_USE_TMPRO
124+
if (textMeshPro)
125+
{
126+
return textMeshPro.fontSharedMaterials ?? s_EmptyMaterials;
127+
}
128+
else
129+
#endif
130+
if (graphic)
131+
{
132+
_materials [0] = graphic.material;
133+
return _materials;
134+
}
135+
else
136+
{
137+
return s_EmptyMaterials;
138+
}
139+
}
140+
}
49141

50142
/// <summary>
51143
/// Call used to modify mesh. (legacy)
@@ -68,11 +160,9 @@ public virtual void ModifyMesh (VertexHelper vh)
68160
/// </summary>
69161
public virtual void SetVerticesDirty ()
70162
{
163+
#if !NOT_USE_TMPRO
71164
if (textMeshPro)
72165
{
73-
//Debug.Log ("SetVerticesDirty");
74-
//_havePropChanged = true;
75-
//textMeshPro.isRightToLeftText
76166
foreach (var info in textMeshPro.textInfo.meshInfo)
77167
{
78168
var mesh = info.mesh;
@@ -91,7 +181,7 @@ public virtual void SetVerticesDirty ()
91181

92182
if (canvasRenderer)
93183
{
94-
canvasRenderer.SetMesh (_textMeshPro.mesh);
184+
canvasRenderer.SetMesh (textMeshPro.mesh);
95185

96186
GetComponentsInChildren (false, s_SubMeshUIs);
97187
foreach (var sm in s_SubMeshUIs)
@@ -102,7 +192,9 @@ public virtual void SetVerticesDirty ()
102192
}
103193
textMeshPro.havePropertiesChanged = true;
104194
}
105-
else if (graphic)
195+
else
196+
#endif
197+
if (graphic)
106198
{
107199
graphic.SetVerticesDirty ();
108200
}
@@ -117,34 +209,59 @@ public virtual void SetVerticesDirty ()
117209
/// </summary>
118210
protected virtual bool isLegacyMeshModifier { get { return false; } }
119211

212+
213+
protected virtual void Initialize ()
214+
{
215+
if (!_initialized)
216+
{
217+
_initialized = true;
218+
_graphic = _graphic ?? GetComponent<Graphic> ();
219+
_canvasRenderer = _canvasRenderer ?? GetComponent<CanvasRenderer> ();
220+
_rectTransform = _rectTransform ?? GetComponent<RectTransform> ();
221+
#if !NOT_USE_TMPRO
222+
_textMeshPro = _textMeshPro ?? GetComponent<TMP_Text> ();
223+
#endif
224+
}
225+
}
226+
120227
/// <summary>
121228
/// This function is called when the object becomes enabled and active.
122229
/// </summary>
123230
protected override void OnEnable ()
124231
{
232+
_initialized = false;
233+
SetVerticesDirty ();
234+
#if !NOT_USE_TMPRO
125235
if (textMeshPro)
126236
{
127-
128237
TMPro_EventManager.TEXT_CHANGED_EVENT.Add (OnTextChanged);
129-
//#if UNITY_EDITOR
130-
// TMPro_EventManager.TEXTMESHPRO_PROPERTY_EVENT.Add (OnTextChanged);
131-
// TMPro_EventManager.TEXTMESHPRO_UGUI_PROPERTY_EVENT.Add (OnTextChanged);
132-
//#endif
133238
}
134-
SetVerticesDirty ();
239+
#endif
240+
241+
#if UNITY_EDITOR && !NOT_USE_TMPRO
242+
if (graphic && textMeshPro)
243+
{
244+
GraphicRebuildTracker.TrackGraphic (graphic);
245+
}
246+
#endif
135247
}
136248

137249
/// <summary>
138250
/// This function is called when the behaviour becomes disabled () or inactive.
139251
/// </summary>
140252
protected override void OnDisable ()
141253
{
254+
#if !NOT_USE_TMPRO
142255
TMPro_EventManager.TEXT_CHANGED_EVENT.Remove (OnTextChanged);
143-
//#if UNITY_EDITOR
144-
// TMPro_EventManager.TEXTMESHPRO_PROPERTY_EVENT.Remove (OnTextChanged);
145-
// TMPro_EventManager.TEXTMESHPRO_UGUI_PROPERTY_EVENT.Remove (OnTextChanged);
146-
//#endif
256+
#endif
147257
SetVerticesDirty ();
258+
259+
#if UNITY_EDITOR && !NOT_USE_TMPRO
260+
if (graphic && textMeshPro)
261+
{
262+
GraphicRebuildTracker.UnTrackGraphic (graphic);
263+
}
264+
#endif
148265
}
149266

150267

@@ -153,15 +270,16 @@ protected override void OnDisable ()
153270
/// </summary>
154271
protected virtual void LateUpdate ()
155272
{
156-
var t = _textMeshPro;
157-
if (t)
273+
#if !NOT_USE_TMPRO
274+
if (textMeshPro)
158275
{
159-
if (t.havePropertiesChanged || _isTextMeshProActive != t.isActiveAndEnabled)
276+
if (textMeshPro.havePropertiesChanged || _isTextMeshProActive != textMeshPro.isActiveAndEnabled)
160277
{
161278
SetVerticesDirty ();
162279
}
163-
_isTextMeshProActive = t.isActiveAndEnabled;
280+
_isTextMeshProActive = textMeshPro.isActiveAndEnabled;
164281
}
282+
#endif
165283
}
166284

167285
/// <summary>
@@ -186,34 +304,25 @@ protected override void OnValidate ()
186304
//################################
187305
// Private Members.
188306
//################################
189-
TMP_Text _textMeshPro;
190-
Graphic _graphic;
307+
bool _initialized;
191308
CanvasRenderer _canvasRenderer;
309+
RectTransform _rectTransform;
310+
Graphic _graphic;
311+
Material [] _materials = new Material [1];
312+
313+
#if !NOT_USE_TMPRO
192314
bool _isTextMeshProActive;
193-
bool _havePropChanged;
315+
TMP_Text _textMeshPro;
194316

195317
/// <summary>
196318
/// Called when any TextMeshPro generated the mesh.
197319
/// </summary>
198320
/// <param name="obj">TextMeshPro object.</param>
199321
void OnTextChanged (Object obj)
200322
{
201-
//if(_havePropChanged)
202-
//{
203-
// _havePropChanged = false;
204-
// Debug.Log ("OnTextChanged _havePropChanged");
205-
//}
206-
//else
207-
//{
208-
// Debug.Log ("OnTextChanged _havePropChanged Not!");
209-
// SetVerticesDirty ();
210-
// _textMeshPro.havePropertiesChanged = false;
211-
//}
212-
213-
214323
// Skip if the object is different from the current object or the text is empty.
215-
var textInfo = _textMeshPro.textInfo;
216-
if (_textMeshPro != obj || textInfo.characterCount - textInfo.spaceCount <= 0)
324+
var textInfo = textMeshPro.textInfo;
325+
if (textMeshPro != obj || textInfo.characterCount - textInfo.spaceCount <= 0)
217326
{
218327
return;
219328
}
@@ -248,7 +357,7 @@ void OnTextChanged (Object obj)
248357
// Set the modified meshes to the CanvasRenderers (for UI only).
249358
if (canvasRenderer)
250359
{
251-
canvasRenderer.SetMesh (_textMeshPro.mesh);
360+
canvasRenderer.SetMesh (textMeshPro.mesh);
252361
GetComponentsInChildren (false, s_SubMeshUIs);
253362
foreach (var sm in s_SubMeshUIs)
254363
{
@@ -259,8 +368,6 @@ void OnTextChanged (Object obj)
259368

260369
// Clear.
261370
s_Meshes.Clear ();
262-
//s_SubMeshes.Clear ();
263-
//s_SubMeshUIs.Clear ();
264371
}
265372

266373
void FillVertexHelper (VertexHelper vh, Mesh mesh)
@@ -285,5 +392,6 @@ void FillVertexHelper (VertexHelper vh, Mesh mesh)
285392
vh.AddTriangle (s_Indices [i], s_Indices [i + 1], s_Indices [i + 2]);
286393
}
287394
}
395+
#endif
288396
}
289397
}

Assets/Coffee/UIExtensions/MeshEffectForTextMeshPro/Scripts/UIFlip.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
namespace Coffee.UIExtensions
77
{
88

9-
[RequireComponent(typeof(Graphic))]
9+
//[RequireComponent(typeof(Graphic))]
1010
[DisallowMultipleComponent]
11-
[AddComponentMenu("UI/UIEffect/UIFlip",102)]
11+
[AddComponentMenu("UI/MeshEffectForTextMeshPro/UIFlip", 102)]
1212
public class UIFlip : BaseMeshEffect
1313
{
1414
//################################

Assets/Coffee/UIExtensions/MeshEffectForTextMeshPro/Scripts/UIGradient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Coffee.UIExtensions
77
/// UIGradient.
88
/// </summary>
99
[DisallowMultipleComponent]
10-
[AddComponentMenu("UI/UIEffect/UIGradient",101)]
10+
[AddComponentMenu("UI/MeshEffectForTextMeshPro/UIGradient", 101)]
1111
public class UIGradient : BaseMeshEffect
1212
{
1313
//################################

Assets/Coffee/UIExtensions/MeshEffectForTextMeshPro/Scripts/UIShadow.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace Coffee.UIExtensions
1515
/// <summary>
1616
/// UIEffect.
1717
/// </summary>
18-
[RequireComponent(typeof(Graphic))]
19-
[AddComponentMenu("UI/UIEffect/UIShadow", 100)]
18+
//[RequireComponent(typeof(Graphic))]
19+
[AddComponentMenu("UI/MeshEffectForTextMeshPro/UIShadow", 100)]
2020
public class UIShadow : BaseMeshEffect//, IParameterTexture
2121
#if UNITY_EDITOR
2222
, ISerializationCallbackReceiver

0 commit comments

Comments
 (0)