44using System.Collections.Generic;
48 [AddComponentMenu(
"UI/Effects/Extensions/Letter Spacing")]
54 private float m_spacing = 0f;
59 protected override void OnValidate()
68 get {
return m_spacing; }
71 if (m_spacing == value)
return;
73 if (graphic !=
null) graphic.SetVerticesDirty();
79 if (! IsActive())
return;
81 List<UIVertex> verts =
new List<UIVertex>();
82 vh.GetUIVertexStream(verts);
84 Text text = GetComponent<Text>();
87 Debug.LogWarning(
"LetterSpacing: Missing Text component");
91 string[] lines = text.text.Split(
'\n');
93 float letterOffset =
spacing * (float)text.fontSize / 100f;
94 float alignmentFactor = 0;
97 switch (text.alignment)
99 case TextAnchor.LowerLeft:
100 case TextAnchor.MiddleLeft:
101 case TextAnchor.UpperLeft:
102 alignmentFactor = 0f;
105 case TextAnchor.LowerCenter:
106 case TextAnchor.MiddleCenter:
107 case TextAnchor.UpperCenter:
108 alignmentFactor = 0.5f;
111 case TextAnchor.LowerRight:
112 case TextAnchor.MiddleRight:
113 case TextAnchor.UpperRight:
114 alignmentFactor = 1f;
118 for (
int lineIdx=0; lineIdx < lines.Length; lineIdx++)
120 string line = lines[lineIdx];
121 float lineOffset = (line.Length -1) * letterOffset * alignmentFactor;
123 for (
int charIdx = 0; charIdx < line.Length; charIdx++)
125 int idx1 = glyphIdx * 6 + 0;
126 int idx2 = glyphIdx * 6 + 1;
127 int idx3 = glyphIdx * 6 + 2;
128 int idx4 = glyphIdx * 6 + 3;
129 int idx5 = glyphIdx * 6 + 4;
130 int idx6 = glyphIdx * 6 + 5;
133 if (idx6 > verts.Count - 1)
return;
135 UIVertex vert1 = verts[idx1];
136 UIVertex vert2 = verts[idx2];
137 UIVertex vert3 = verts[idx3];
138 UIVertex vert4 = verts[idx4];
139 UIVertex vert5 = verts[idx5];
140 UIVertex vert6 = verts[idx6];
142 pos = Vector3.right * (letterOffset * charIdx - lineOffset);
144 vert1.position += pos;
145 vert2.position += pos;
146 vert3.position += pos;
147 vert4.position += pos;
148 vert5.position += pos;
149 vert6.position += pos;
165 vh.AddUIVertexTriangleStream(verts);
override void ModifyMesh(VertexHelper vh)
Credit Erdener Gonenc - @PixelEnvision.