TMPro_ContextMenus.cs
13.4 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
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
namespace TMPro.EditorUtilities
{
public class TMP_ContextMenus : Editor
{
private static Texture m_copiedTexture;
private static Material m_copiedProperties;
private static Material m_copiedAtlasProperties;
// Add a Context Menu to the Texture Editor Panel to allow Copy / Paste of Texture.
[MenuItem("CONTEXT/Texture/Copy", false, 2000)]
static void CopyTexture(MenuCommand command)
{
m_copiedTexture = command.context as Texture;
}
// Select the currently assigned material or material preset.
[MenuItem("CONTEXT/Material/Select Material", false, 500)]
static void SelectMaterial(MenuCommand command)
{
Material mat = command.context as Material;
// Select current material
EditorUtility.FocusProjectWindow();
EditorGUIUtility.PingObject(mat);
}
// Add a Context Menu to allow easy duplication of the Material.
[MenuItem("CONTEXT/Material/Create Material Preset", false)]
static void DuplicateMaterial(MenuCommand command)
{
// Get the type of text object
// If material is not a base material, we get material leaks...
Material source_Mat = (Material)command.context;
if (!EditorUtility.IsPersistent(source_Mat))
{
Debug.LogWarning("Material is an instance and cannot be converted into a permanent asset.");
return;
}
string assetPath = AssetDatabase.GetAssetPath(source_Mat).Split('.')[0];
Material duplicate = new Material(source_Mat);
// Need to manually copy the shader keywords
duplicate.shaderKeywords = source_Mat.shaderKeywords;
AssetDatabase.CreateAsset(duplicate, AssetDatabase.GenerateUniqueAssetPath(assetPath + ".mat"));
// Assign duplicate Material to selected object (if one is)
if (Selection.activeGameObject != null)
{
TMP_Text textObject = Selection.activeGameObject.GetComponent<TMP_Text>();
if (textObject != null)
{
textObject.fontSharedMaterial = duplicate;
}
else
{
TMP_SubMesh subMeshObject = Selection.activeGameObject.GetComponent<TMP_SubMesh>();
if (subMeshObject != null)
subMeshObject.sharedMaterial = duplicate;
else
{
TMP_SubMeshUI subMeshUIObject = Selection.activeGameObject.GetComponent<TMP_SubMeshUI>();
if (subMeshUIObject != null)
subMeshUIObject.sharedMaterial = duplicate;
}
}
}
// Ping newly created Material Preset.
EditorUtility.FocusProjectWindow();
EditorGUIUtility.PingObject(duplicate);
}
//[MenuItem("CONTEXT/MaterialComponent/Copy Material Properties", false)]
[MenuItem("CONTEXT/Material/Copy Material Properties", false)]
static void CopyMaterialProperties(MenuCommand command)
{
Material mat = null;
if (command.context.GetType() == typeof(Material))
mat = (Material)command.context;
else
{
mat = Selection.activeGameObject.GetComponent<CanvasRenderer>().GetMaterial();
}
m_copiedProperties = new Material(mat);
m_copiedProperties.shaderKeywords = mat.shaderKeywords;
m_copiedProperties.hideFlags = HideFlags.DontSave;
}
// PASTE MATERIAL
//[MenuItem("CONTEXT/MaterialComponent/Paste Material Properties", false)]
[MenuItem("CONTEXT/Material/Paste Material Properties", false)]
static void PasteMaterialProperties(MenuCommand command)
{
if (m_copiedProperties == null)
{
Debug.LogWarning("No Material Properties to Paste. Use Copy Material Properties first.");
return;
}
Material mat = null;
if (command.context.GetType() == typeof(Material))
mat = (Material)command.context;
else
{
mat = Selection.activeGameObject.GetComponent<CanvasRenderer>().GetMaterial();
}
Undo.RecordObject(mat, "Paste Material");
ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
if (mat.HasProperty(ShaderUtilities.ID_GradientScale))
{
// Preserve unique SDF properties from destination material.
m_copiedProperties.SetTexture(ShaderUtilities.ID_MainTex, mat.GetTexture(ShaderUtilities.ID_MainTex));
m_copiedProperties.SetFloat(ShaderUtilities.ID_GradientScale, mat.GetFloat(ShaderUtilities.ID_GradientScale));
m_copiedProperties.SetFloat(ShaderUtilities.ID_TextureWidth, mat.GetFloat(ShaderUtilities.ID_TextureWidth));
m_copiedProperties.SetFloat(ShaderUtilities.ID_TextureHeight, mat.GetFloat(ShaderUtilities.ID_TextureHeight));
}
EditorShaderUtilities.CopyMaterialProperties(m_copiedProperties, mat);
// Copy ShaderKeywords from one material to the other.
mat.shaderKeywords = m_copiedProperties.shaderKeywords;
// Let TextMeshPro Objects that this mat has changed.
TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, mat);
}
// Enable Resetting of Material properties without losing unique properties of the font atlas.
[MenuItem("CONTEXT/Material/Reset", false, 2100)]
static void ResetSettings(MenuCommand command)
{
Material mat = null;
if (command.context.GetType() == typeof(Material))
mat = (Material)command.context;
else
{
mat = Selection.activeGameObject.GetComponent<CanvasRenderer>().GetMaterial();
}
Undo.RecordObject(mat, "Reset Material");
ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
if (mat.HasProperty(ShaderUtilities.ID_GradientScale))
{
// Copy unique properties of the SDF Material
var texture = mat.GetTexture(ShaderUtilities.ID_MainTex);
var gradientScale = mat.GetFloat(ShaderUtilities.ID_GradientScale);
var texWidth = mat.GetFloat(ShaderUtilities.ID_TextureWidth);
var texHeight = mat.GetFloat(ShaderUtilities.ID_TextureHeight);
var stencilId = 0.0f;
var stencilComp = 0.0f;
if (mat.HasProperty(ShaderUtilities.ID_StencilID))
{
stencilId = mat.GetFloat(ShaderUtilities.ID_StencilID);
stencilComp = mat.GetFloat(ShaderUtilities.ID_StencilComp);
}
var normalWeight = mat.GetFloat(ShaderUtilities.ID_WeightNormal);
var boldWeight = mat.GetFloat(ShaderUtilities.ID_WeightBold);
// Reset the material
Unsupported.SmartReset(mat);
// Reset ShaderKeywords
mat.shaderKeywords = new string[0]; // { "BEVEL_OFF", "GLOW_OFF", "UNDERLAY_OFF" };
// Copy unique material properties back to the material.
mat.SetTexture(ShaderUtilities.ID_MainTex, texture);
mat.SetFloat(ShaderUtilities.ID_GradientScale, gradientScale);
mat.SetFloat(ShaderUtilities.ID_TextureWidth, texWidth);
mat.SetFloat(ShaderUtilities.ID_TextureHeight, texHeight);
if (mat.HasProperty(ShaderUtilities.ID_StencilID))
{
mat.SetFloat(ShaderUtilities.ID_StencilID, stencilId);
mat.SetFloat(ShaderUtilities.ID_StencilComp, stencilComp);
}
mat.SetFloat(ShaderUtilities.ID_WeightNormal, normalWeight);
mat.SetFloat(ShaderUtilities.ID_WeightBold, boldWeight);
}
else
{
Unsupported.SmartReset(mat);
}
TMPro_EventManager.ON_MATERIAL_PROPERTY_CHANGED(true, mat);
}
//This function is used for debugging and fixing potentially broken font atlas links.
[MenuItem("CONTEXT/Material/Copy Atlas", false, 2000)]
static void CopyAtlas(MenuCommand command)
{
Material mat = command.context as Material;
m_copiedAtlasProperties = new Material(mat);
m_copiedAtlasProperties.hideFlags = HideFlags.DontSave;
}
// This function is used for debugging and fixing potentially broken font atlas links
[MenuItem("CONTEXT/Material/Paste Atlas", false, 2001)]
static void PasteAtlas(MenuCommand command)
{
Material mat = command.context as Material;
if (m_copiedAtlasProperties != null)
{
Undo.RecordObject(mat, "Paste Texture");
ShaderUtilities.GetShaderPropertyIDs(); // Make sure we have valid Property IDs
mat.SetTexture(ShaderUtilities.ID_MainTex, m_copiedAtlasProperties.GetTexture(ShaderUtilities.ID_MainTex));
mat.SetFloat(ShaderUtilities.ID_GradientScale, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_GradientScale));
mat.SetFloat(ShaderUtilities.ID_TextureWidth, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_TextureWidth));
mat.SetFloat(ShaderUtilities.ID_TextureHeight, m_copiedAtlasProperties.GetFloat(ShaderUtilities.ID_TextureHeight));
}
else if (m_copiedTexture != null)
{
Undo.RecordObject(mat, "Paste Texture");
mat.SetTexture(ShaderUtilities.ID_MainTex, m_copiedTexture);
}
//DestroyImmediate(m_copiedAtlasProperties);
}
// Context Menus for TMPro Font Assets
//This function is used for debugging and fixing potentially broken font atlas links.
[MenuItem("CONTEXT/TMP_FontAsset/Extract Atlas", false, 2100)]
static void ExtractAtlas(MenuCommand command)
{
TMP_FontAsset font = command.context as TMP_FontAsset;
string fontPath = AssetDatabase.GetAssetPath(font);
string texPath = Path.GetDirectoryName(fontPath) + "/" + Path.GetFileNameWithoutExtension(fontPath) + " Atlas.png";
// Create a Serialized Object of the texture to allow us to make it readable.
SerializedObject texprop = new SerializedObject(font.material.GetTexture(ShaderUtilities.ID_MainTex));
texprop.FindProperty("m_IsReadable").boolValue = true;
texprop.ApplyModifiedProperties();
// Create a copy of the texture.
Texture2D tex = Instantiate(font.material.GetTexture(ShaderUtilities.ID_MainTex)) as Texture2D;
// Set the texture to not readable again.
texprop.FindProperty("m_IsReadable").boolValue = false;
texprop.ApplyModifiedProperties();
Debug.Log(texPath);
// Saving File for Debug
var pngData = tex.EncodeToPNG();
File.WriteAllBytes(texPath, pngData);
AssetDatabase.Refresh();
DestroyImmediate(tex);
}
/// <summary>
///
/// </summary>
/// <param name="command"></param>
[MenuItem("CONTEXT/TMP_FontAsset/Update Atlas Texture...", false, 2000)]
static void RegenerateFontAsset(MenuCommand command)
{
TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
if (fontAsset != null)
{
TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(fontAsset);
}
}
/// <summary>
/// Clear Font Asset Data
/// </summary>
/// <param name="command"></param>
[MenuItem("CONTEXT/TMP_FontAsset/Reset", false, 100)]
static void ClearFontAssetData(MenuCommand command)
{
TMP_FontAsset fontAsset = command.context as TMP_FontAsset;
if (fontAsset != null && Selection.activeObject != fontAsset)
{
Selection.activeObject = fontAsset;
}
fontAsset.ClearFontAssetData(true);
TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset);
}
[MenuItem("CONTEXT/TrueTypeFontImporter/Create TMP Font Asset...", false, 200)]
static void CreateFontAsset(MenuCommand command)
{
TrueTypeFontImporter importer = command.context as TrueTypeFontImporter;
if (importer != null)
{
Font sourceFontFile = AssetDatabase.LoadAssetAtPath<Font>(importer.assetPath);
if (sourceFontFile)
TMPro_FontAssetCreatorWindow.ShowFontAtlasCreatorWindow(sourceFontFile);
}
}
}
}