TMP_EditorUtility.cs
16.1 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
using UnityEngine;
using UnityEditor;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace TMPro.EditorUtilities
{
public static class TMP_EditorUtility
{
/// <summary>
/// Returns the relative path of the package.
/// </summary>
public static string packageRelativePath
{
get
{
if (string.IsNullOrEmpty(m_PackagePath))
m_PackagePath = GetPackageRelativePath();
return m_PackagePath;
}
}
[SerializeField]
private static string m_PackagePath;
/// <summary>
/// Returns the fully qualified path of the package.
/// </summary>
public static string packageFullPath
{
get
{
if (string.IsNullOrEmpty(m_PackageFullPath))
m_PackageFullPath = GetPackageFullPath();
return m_PackageFullPath;
}
}
[SerializeField]
private static string m_PackageFullPath;
// Static Fields Related to locating the TextMesh Pro Asset
private static string folderPath = "Not Found";
private static EditorWindow Gameview;
private static bool isInitialized = false;
private static void GetGameview()
{
System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
System.Type type = assembly.GetType("UnityEditor.GameView");
Gameview = EditorWindow.GetWindow(type);
}
public static void RepaintAll()
{
if (isInitialized == false)
{
GetGameview();
isInitialized = true;
}
SceneView.RepaintAll();
Gameview.Repaint();
}
/// <summary>
/// Create and return a new asset in a smart location based on the current selection and then select it.
/// </summary>
/// <param name="name">
/// Name of the new asset. Do not include the .asset extension.
/// </param>
/// <returns>
/// The new asset.
/// </returns>
public static T CreateAsset<T>(string name) where T : ScriptableObject
{
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (path.Length == 0)
{
// no asset selected, place in asset root
path = "Assets/" + name + ".asset";
}
else if (Directory.Exists(path))
{
// place in currently selected directory
path += "/" + name + ".asset";
}
else {
// place in current selection's containing directory
path = Path.GetDirectoryName(path) + "/" + name + ".asset";
}
T asset = ScriptableObject.CreateInstance<T>();
AssetDatabase.CreateAsset(asset, AssetDatabase.GenerateUniqueAssetPath(path));
EditorUtility.FocusProjectWindow();
Selection.activeObject = asset;
return asset;
}
// Function used to find all materials which reference a font atlas so we can update all their references.
public static Material[] FindMaterialReferences(TMP_FontAsset fontAsset)
{
List<Material> refs = new List<Material>();
Material mat = fontAsset.material;
refs.Add(mat);
// Get materials matching the search pattern.
string searchPattern = "t:Material" + " " + fontAsset.name.Split(new char[] { ' ' })[0];
string[] materialAssetGUIDs = AssetDatabase.FindAssets(searchPattern);
for (int i = 0; i < materialAssetGUIDs.Length; i++)
{
string materialPath = AssetDatabase.GUIDToAssetPath(materialAssetGUIDs[i]);
Material targetMaterial = AssetDatabase.LoadAssetAtPath<Material>(materialPath);
if (targetMaterial.HasProperty(ShaderUtilities.ID_MainTex) && targetMaterial.GetTexture(ShaderUtilities.ID_MainTex) != null && mat.GetTexture(ShaderUtilities.ID_MainTex) != null && targetMaterial.GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID() == mat.GetTexture(ShaderUtilities.ID_MainTex).GetInstanceID())
{
if (!refs.Contains(targetMaterial))
refs.Add(targetMaterial);
}
else
{
// TODO: Find a more efficient method to unload resources.
//Resources.UnloadAsset(targetMaterial.GetTexture(ShaderUtilities.ID_MainTex));
}
}
return refs.ToArray();
}
// Function used to find the Font Asset which matches the given Material Preset and Font Atlas Texture.
public static TMP_FontAsset FindMatchingFontAsset(Material mat)
{
if (mat.GetTexture(ShaderUtilities.ID_MainTex) == null) return null;
// Find the dependent assets of this material.
string[] dependentAssets = AssetDatabase.GetDependencies(AssetDatabase.GetAssetPath(mat), false);
for (int i = 0; i < dependentAssets.Length; i++)
{
TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath<TMP_FontAsset>(dependentAssets[i]);
if (fontAsset != null)
return fontAsset;
}
return null;
}
private static string GetPackageRelativePath()
{
// Check for potential UPM package
string packagePath = Path.GetFullPath("Packages/com.unity.textmeshpro");
if (Directory.Exists(packagePath))
{
return "Packages/com.unity.textmeshpro";
}
packagePath = Path.GetFullPath("Assets/..");
if (Directory.Exists(packagePath))
{
// Search default location for development package
if (Directory.Exists(packagePath + "/Assets/Packages/com.unity.TextMeshPro/Editor Resources"))
{
return "Assets/Packages/com.unity.TextMeshPro";
}
// Search for default location of normal TextMesh Pro AssetStore package
if (Directory.Exists(packagePath + "/Assets/TextMesh Pro/Editor Resources"))
{
return "Assets/TextMesh Pro";
}
// Search for potential alternative locations in the user project
string[] matchingPaths = Directory.GetDirectories(packagePath, "TextMesh Pro", SearchOption.AllDirectories);
packagePath = ValidateLocation(matchingPaths, packagePath);
if (packagePath != null) return packagePath;
}
return null;
}
private static string GetPackageFullPath()
{
// Check for potential UPM package
string packagePath = Path.GetFullPath("Packages/com.unity.textmeshpro");
if (Directory.Exists(packagePath))
{
return packagePath;
}
packagePath = Path.GetFullPath("Assets/..");
if (Directory.Exists(packagePath))
{
// Search default location for development package
if (Directory.Exists(packagePath + "/Assets/Packages/com.unity.TextMeshPro/Editor Resources"))
{
return packagePath + "/Assets/Packages/com.unity.TextMeshPro";
}
// Search for default location of normal TextMesh Pro AssetStore package
if (Directory.Exists(packagePath + "/Assets/TextMesh Pro/Editor Resources"))
{
return packagePath + "/Assets/TextMesh Pro";
}
// Search for potential alternative locations in the user project
string[] matchingPaths = Directory.GetDirectories(packagePath, "TextMesh Pro", SearchOption.AllDirectories);
string path = ValidateLocation(matchingPaths, packagePath);
if (path != null) return packagePath + path;
}
return null;
}
/// <summary>
/// Method to validate the location of the asset folder by making sure the GUISkins folder exists.
/// </summary>
/// <param name="paths"></param>
/// <returns></returns>
private static string ValidateLocation(string[] paths, string projectPath)
{
for (int i = 0; i < paths.Length; i++)
{
// Check if any of the matching directories contain a GUISkins directory.
if (Directory.Exists(paths[i] + "/Editor Resources"))
{
folderPath = paths[i].Replace(projectPath, "");
folderPath = folderPath.TrimStart('\\', '/');
return folderPath;
}
}
return null;
}
/// <summary>
/// Function which returns a string containing a sequence of Decimal character ranges.
/// </summary>
/// <param name="characterSet"></param>
/// <returns></returns>
public static string GetDecimalCharacterSequence(int[] characterSet)
{
if (characterSet == null || characterSet.Length == 0)
return string.Empty;
string characterSequence = string.Empty;
int count = characterSet.Length;
int first = characterSet[0];
int last = first;
for (int i = 1; i < count; i++)
{
if (characterSet[i - 1] + 1 == characterSet[i])
{
last = characterSet[i];
}
else
{
if (first == last)
characterSequence += first + ",";
else
characterSequence += first + "-" + last + ",";
first = last = characterSet[i];
}
}
// handle the final group
if (first == last)
characterSequence += first;
else
characterSequence += first + "-" + last;
return characterSequence;
}
/// <summary>
/// Function which returns a string containing a sequence of Unicode (Hex) character ranges.
/// </summary>
/// <param name="characterSet"></param>
/// <returns></returns>
public static string GetUnicodeCharacterSequence(int[] characterSet)
{
if (characterSet == null || characterSet.Length == 0)
return string.Empty;
string characterSequence = string.Empty;
int count = characterSet.Length;
int first = characterSet[0];
int last = first;
for (int i = 1; i < count; i++)
{
if (characterSet[i - 1] + 1 == characterSet[i])
{
last = characterSet[i];
}
else
{
if (first == last)
characterSequence += first.ToString("X2") + ",";
else
characterSequence += first.ToString("X2") + "-" + last.ToString("X2") + ",";
first = last = characterSet[i];
}
}
// handle the final group
if (first == last)
characterSequence += first.ToString("X2");
else
characterSequence += first.ToString("X2") + "-" + last.ToString("X2");
return characterSequence;
}
/// <summary>
///
/// </summary>
/// <param name="rect"></param>
/// <param name="thickness"></param>
/// <param name="color"></param>
public static void DrawBox(Rect rect, float thickness, Color color)
{
EditorGUI.DrawRect(new Rect(rect.x - thickness, rect.y + thickness, rect.width + thickness * 2, thickness), color);
EditorGUI.DrawRect(new Rect(rect.x - thickness, rect.y + thickness, thickness, rect.height - thickness * 2), color);
EditorGUI.DrawRect(new Rect(rect.x - thickness, rect.y + rect.height - thickness * 2, rect.width + thickness * 2, thickness), color);
EditorGUI.DrawRect(new Rect(rect.x + rect.width, rect.y + thickness, thickness, rect.height - thickness * 2), color);
}
/// <summary>
/// Function to return the horizontal alignment grid value.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int GetHorizontalAlignmentGridValue(int value)
{
if ((value & 0x1) == 0x1)
return 0;
else if ((value & 0x2) == 0x2)
return 1;
else if ((value & 0x4) == 0x4)
return 2;
else if ((value & 0x8) == 0x8)
return 3;
else if ((value & 0x10) == 0x10)
return 4;
else if ((value & 0x20) == 0x20)
return 5;
return 0;
}
/// <summary>
/// Function to return the vertical alignment grid value.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int GetVerticalAlignmentGridValue(int value)
{
if ((value & 0x100) == 0x100)
return 0;
if ((value & 0x200) == 0x200)
return 1;
if ((value & 0x400) == 0x400)
return 2;
if ((value & 0x800) == 0x800)
return 3;
if ((value & 0x1000) == 0x1000)
return 4;
if ((value & 0x2000) == 0x2000)
return 5;
return 0;
}
public static void DrawColorProperty(Rect rect, SerializedProperty property)
{
int oldIndent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
if (EditorGUIUtility.wideMode)
{
EditorGUI.PropertyField(new Rect(rect.x, rect.y, 50f, rect.height), property, GUIContent.none);
rect.x += 50f;
rect.width = Mathf.Min(100f, rect.width - 55f);
}
else
{
rect.height /= 2f;
rect.width = Mathf.Min(100f, rect.width - 5f);
EditorGUI.PropertyField(rect, property, GUIContent.none);
rect.y += rect.height;
}
EditorGUI.BeginChangeCheck();
string colorString = EditorGUI.TextField(rect, string.Format("#{0}", ColorUtility.ToHtmlStringRGBA(property.colorValue)));
if (EditorGUI.EndChangeCheck())
{
Color color;
if (ColorUtility.TryParseHtmlString(colorString, out color))
{
property.colorValue = color;
}
}
EditorGUI.indentLevel = oldIndent;
}
public static bool EditorToggle(Rect position, bool value, GUIContent content, GUIStyle style)
{
var id = GUIUtility.GetControlID(content, FocusType.Keyboard, position);
var evt = Event.current;
// Toggle selected toggle on space or return key
if (GUIUtility.keyboardControl == id && evt.type == EventType.KeyDown && (evt.keyCode == KeyCode.Space || evt.keyCode == KeyCode.Return || evt.keyCode == KeyCode.KeypadEnter))
{
value = !value;
evt.Use();
GUI.changed = true;
}
if (evt.type == EventType.MouseDown && position.Contains(Event.current.mousePosition))
{
GUIUtility.keyboardControl = id;
EditorGUIUtility.editingTextField = false;
HandleUtility.Repaint();
}
return GUI.Toggle(position, id, value, content, style);
}
}
}