44using System.Collections.Generic;
48 [AddComponentMenu(
"UI/Effects/Extensions/Mono Spacing")]
49 [RequireComponent(typeof(Text))]
50 [RequireComponent(typeof(RectTransform))]
56 private float m_spacing = 0f;
60 private RectTransform rectTransform;
67 text = GetComponent<Text>();
70 Debug.LogWarning(
"MonoSpacing: Missing Text component");
73 rectTransform = text.GetComponent<RectTransform>();
77 protected override void OnValidate()
86 get {
return m_spacing; }
89 if (m_spacing == value)
return;
91 if (graphic !=
null) graphic.SetVerticesDirty();
97 if (! IsActive())
return;
99 List<UIVertex> verts =
new List<UIVertex>();
100 vh.GetUIVertexStream(verts);
102 string[] lines = text.text.Split(
'\n');
104 float letterOffset =
Spacing * (float)text.fontSize / 100f;
105 float alignmentFactor = 0;
108 switch (text.alignment)
110 case TextAnchor.LowerLeft:
111 case TextAnchor.MiddleLeft:
112 case TextAnchor.UpperLeft:
113 alignmentFactor = 0f;
116 case TextAnchor.LowerCenter:
117 case TextAnchor.MiddleCenter:
118 case TextAnchor.UpperCenter:
119 alignmentFactor = 0.5f;
122 case TextAnchor.LowerRight:
123 case TextAnchor.MiddleRight:
124 case TextAnchor.UpperRight:
125 alignmentFactor = 1f;
129 for (
int lineIdx=0; lineIdx < lines.Length; lineIdx++)
131 string line = lines[lineIdx];
132 float lineOffset = (line.Length - 1) * letterOffset * (alignmentFactor) - (alignmentFactor - 0.5f) * rectTransform.rect.width;
134 var offsetX = -lineOffset + letterOffset / 2 * (1 - alignmentFactor * 2);
136 for (
int charIdx = 0; charIdx < line.Length; charIdx++)
138 int idx1 = glyphIdx * 6 + 0;
139 int idx2 = glyphIdx * 6 + 1;
140 int idx3 = glyphIdx * 6 + 2;
141 int idx4 = glyphIdx * 6 + 3;
142 int idx5 = glyphIdx * 6 + 4;
143 int idx6 = glyphIdx * 6 + 5;
146 if (idx6 > verts.Count - 1)
return;
148 UIVertex vert1 = verts[idx1];
149 UIVertex vert2 = verts[idx2];
150 UIVertex vert3 = verts[idx3];
151 UIVertex vert4 = verts[idx4];
152 UIVertex vert5 = verts[idx5];
153 UIVertex vert6 = verts[idx6];
156 float charWidth = (vert2.position - vert1.position).x;
159 var smallCharOffset = smallChar ? -letterOffset/4 : 0;
161 vert1.position +=
new Vector3(-vert1.position.x + offsetX + -.5f * charWidth + smallCharOffset, 0, 0);
162 vert2.position +=
new Vector3(-vert2.position.x + offsetX + .5f * charWidth + smallCharOffset, 0, 0);
163 vert3.position +=
new Vector3(-vert3.position.x + offsetX + .5f * charWidth + smallCharOffset, 0, 0);
164 vert4.position +=
new Vector3(-vert4.position.x + offsetX + .5f * charWidth + smallCharOffset, 0, 0);
165 vert5.position +=
new Vector3(-vert5.position.x + offsetX + -.5f * charWidth + smallCharOffset, 0, 0);
166 vert6.position +=
new Vector3(-vert6.position.x + offsetX + -.5f * charWidth + smallCharOffset, 0, 0);
169 offsetX += letterOffset / 2;
171 offsetX += letterOffset;
187 vh.AddUIVertexTriangleStream(verts);
override void ModifyMesh(VertexHelper vh)
Credit Erdener Gonenc - @PixelEnvision.