-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextMeshProFormatter.cs
More file actions
214 lines (191 loc) · 6.44 KB
/
Copy pathTextMeshProFormatter.cs
File metadata and controls
214 lines (191 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System.Globalization;
using System.Text;
/// <summary>
/// A formatter class for building rich text strings for TextMeshPro.
/// </summary>
public class TextMeshProFormatter
{
private StringBuilder formattedText;
/// <summary>
/// Specifies the alignment of text.
/// </summary>
public enum Alignment
{
Left,
Right,
Center,
Justified,
}
/// <summary>
/// Initializes a new instance of the TextMeshProFormatter class.
/// </summary>
public TextMeshProFormatter()
{
formattedText = new StringBuilder();
}
/// <summary>
/// Appends text to the formatter.
/// </summary>
/// <param name="text">The text to append.</param>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter AppendText(string text)
{
formattedText.Append(text);
return this;
}
/// <summary>
/// Applies bold formatting to the text.
/// </summary>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter Bold()
{
formattedText.Insert(0, "<b>").Append("</b>");
return this;
}
/// <summary>
/// Applies italic formatting to the text.
/// </summary>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter Italicize()
{
formattedText.Insert(0, "<i>").Append("</i>");
return this;
}
/// <summary>
/// Applies underline formatting to the text.
/// </summary>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter Underline()
{
formattedText.Insert(0, "<u>").Append("</u>");
return this;
}
/// <summary>
/// Applies strikethrough formatting to the text.
/// </summary>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter Strikethrough()
{
formattedText.Insert(0, "<s>").Append("</s>");
return this;
}
/// <summary>
/// Sets the color of the text.
/// </summary>
/// <param name="colorHexCode">The color hex code to apply to the text.</param>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter SetColor(string colorHexCode)
{
formattedText.Insert(0, $"<color=#{colorHexCode}>").Append("</color>");
return this;
}
/// <summary>
/// Sets the size of the text.
/// </summary>
/// <param name="size">The font size to apply to the text.</param>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter SetSize(int size)
{
formattedText.Insert(0, $"<size={size}>").Append("</size>");
return this;
}
/// <summary>
/// Sets the font of the text.
/// </summary>
/// <param name="fontAssetName">The name of the font asset to apply to the text.</param>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter SetFont(string fontAssetName)
{
formattedText.Insert(0, $"<font={fontAssetName}>").Append("</font>");
return this;
}
/// <summary>
/// Inserts a sprite into the text.
/// </summary>
/// <param name="spriteIndex">The index of the sprite to insert.</param>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter InsertSprite(int spriteIndex)
{
formattedText.Append($"<sprite={spriteIndex}>");
return this;
}
/// <summary>
/// Sets the opacity of the text.
/// WARNING: Following a SetOpacity call, text alpha is set to FF i.e. 1.0f
/// </summary>
/// <param name="opacity">The opacity value to apply to the text (0.0 - 1.0).</param>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter SetOpacity(float opacity)
{
// Convert the opacity value from float (0.0 - 1.0) to hexadecimal (00 - FF)
byte opacityByte = (byte)(opacity * 255);
string opacityHex = opacityByte.ToString("X2");
// Insert the <alpha> tag with the hexadecimal opacity value
// <alpha> tags do not have a closing tag
formattedText.Insert(0, $"<alpha=#{opacityHex}>").Append("<alpha=#FF>");
return this;
}
/// <summary>
/// Aligns the text according to the specified alignment.
/// </summary>
/// <param name="alignment">The alignment to apply to the text.</param>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter Align(Alignment alignment)
{
string alignTag = alignment switch
{
Alignment.Left => "left",
Alignment.Right => "right",
Alignment.Center => "center",
Alignment.Justified => "justified",
_ => "left" // Default to left if an unknown value is passed
};
formattedText.Insert(0, $"<align={alignTag}>").Append("</align>");
return this;
}
/// <summary>
/// Rotates the text by a specified angle.
/// </summary>
/// <param name="angle">The angle to rotate the text.</param>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter Rotate(float angle)
{
formattedText.Insert(0, $"<rotate={angle}>").Append("</rotate>");
return this;
}
/// <summary>
/// Converts the entire text to uppercase.
/// </summary>
/// <returns>The current instance of the formatter.</returns>
public TextMeshProFormatter Uppercase()
{
string upperText = formattedText.ToString().ToUpper(CultureInfo.InvariantCulture);
formattedText.Clear().Append(upperText);
return this;
}
/// <summary>
/// Returns the formatted text as a string.
/// </summary>
/// <returns>The formatted text.</returns>
public override string ToString()
{
return formattedText.ToString();
}
/// <summary>
/// Clears the formatted text, removing any previously appended text or formatting.
/// </summary>
public void Clear()
{
formattedText.Clear();
}
/// <summary>
/// Gets the formatted text and clears the internal state, preparing for future formatting.
/// </summary>
/// <returns>The formatted text.</returns>
public string GetFormattedText()
{
string text = formattedText.ToString();
Clear();
return text;
}
}