TMP_FontAsset_CreationMenu.cs
10.2 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
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.TextCore;
using UnityEngine.TextCore.LowLevel;
using TMPro;
namespace TMPro
{
public static class TMP_FontAsset_CreationMenu
{
[MenuItem("Assets/Create/TextMeshPro/Font Asset Variant", false, 105)]
public static void CreateFontAssetVariant()
{
Object target = Selection.activeObject;
// Make sure the selection is a font file
if (target == null || target.GetType() != typeof(TMP_FontAsset))
{
Debug.LogWarning("A Font file must first be selected in order to create a Font Asset.");
return;
}
TMP_FontAsset sourceFontAsset = (TMP_FontAsset)target;
string sourceFontFilePath = AssetDatabase.GetAssetPath(target);
string folderPath = Path.GetDirectoryName(sourceFontFilePath);
string assetName = Path.GetFileNameWithoutExtension(sourceFontFilePath);
string newAssetFilePathWithName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + assetName + " - Variant.asset");
// Set Texture and Material reference to the source font asset.
TMP_FontAsset fontAsset = ScriptableObject.Instantiate<TMP_FontAsset>(sourceFontAsset);
AssetDatabase.CreateAsset(fontAsset, newAssetFilePathWithName);
fontAsset.atlasPopulationMode = AtlasPopulationMode.Static;
// Initialize array for the font atlas textures.
fontAsset.atlasTextures = sourceFontAsset.atlasTextures;
fontAsset.material = sourceFontAsset.material;
// Not sure if this is still necessary in newer versions of Unity.
EditorUtility.SetDirty(fontAsset);
AssetDatabase.SaveAssets();
}
/*
[MenuItem("Assets/Create/TextMeshPro/Font Asset Fallback", false, 105)]
public static void CreateFallbackFontAsset()
{
Object target = Selection.activeObject;
// Make sure the selection is a font file
if (target == null || target.GetType() != typeof(TMP_FontAsset))
{
Debug.LogWarning("A Font file must first be selected in order to create a Font Asset.");
return;
}
TMP_FontAsset sourceFontAsset = (TMP_FontAsset)target;
string sourceFontFilePath = AssetDatabase.GetAssetPath(target);
string folderPath = Path.GetDirectoryName(sourceFontFilePath);
string assetName = Path.GetFileNameWithoutExtension(sourceFontFilePath);
string newAssetFilePathWithName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + assetName + " - Fallback.asset");
//// Create new TM Font Asset.
TMP_FontAsset fontAsset = ScriptableObject.CreateInstance<TMP_FontAsset>();
AssetDatabase.CreateAsset(fontAsset, newAssetFilePathWithName);
fontAsset.version = "1.1.0";
fontAsset.faceInfo = sourceFontAsset.faceInfo;
fontAsset.m_SourceFontFileGUID = sourceFontAsset.m_SourceFontFileGUID;
fontAsset.m_SourceFontFile_EditorRef = sourceFontAsset.m_SourceFontFile_EditorRef;
fontAsset.atlasPopulationMode = TMP_FontAsset.AtlasPopulationMode.Dynamic;
int atlasWidth = fontAsset.atlasWidth = sourceFontAsset.atlasWidth;
int atlasHeight = fontAsset.atlasHeight = sourceFontAsset.atlasHeight;
int atlasPadding = fontAsset.atlasPadding = sourceFontAsset.atlasPadding;
fontAsset.atlasRenderMode = sourceFontAsset.atlasRenderMode;
// Initialize array for the font atlas textures.
fontAsset.atlasTextures = new Texture2D[1];
// Create and add font atlas texture
Texture2D texture = new Texture2D(atlasWidth, atlasHeight, TextureFormat.Alpha8, false);
Color32[] colors = new Color32[atlasWidth * atlasHeight];
texture.SetPixels32(colors);
texture.name = assetName + " Atlas";
fontAsset.atlasTextures[0] = texture;
AssetDatabase.AddObjectToAsset(texture, fontAsset);
// Add free rectangle of the size of the texture.
int packingModifier = ((GlyphRasterModes)fontAsset.atlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP ? 0 : 1;
fontAsset.m_FreeGlyphRects = new List<GlyphRect>() { new GlyphRect(0, 0, atlasWidth - packingModifier, atlasHeight - packingModifier) };
fontAsset.m_UsedGlyphRects = new List<GlyphRect>();
// Create new Material and Add it as Sub-Asset
Material tmp_material = new Material(sourceFontAsset.material);
tmp_material.name = texture.name + " Material";
tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, atlasPadding + packingModifier);
tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
fontAsset.material = tmp_material;
AssetDatabase.AddObjectToAsset(tmp_material, fontAsset);
// Add Font Asset Creation Settings
// TODO
// Not sure if this is still necessary in newer versions of Unity.
EditorUtility.SetDirty(fontAsset);
AssetDatabase.SaveAssets();
}
*/
//[MenuItem("Assets/Create/TextMeshPro/Font Asset #%F12", true)]
//public static bool CreateFontAssetMenuValidation()
//{
// return false;
//}
[MenuItem("Assets/Create/TextMeshPro/Font Asset #%F12", false, 100)]
public static void CreateFontAsset()
{
Object target = Selection.activeObject;
// Make sure the selection is a font file
if (target == null || target.GetType() != typeof(Font))
{
Debug.LogWarning("A Font file must first be selected in order to create a Font Asset.");
return;
}
Font sourceFont = (Font)target;
string sourceFontFilePath = AssetDatabase.GetAssetPath(target);
string folderPath = Path.GetDirectoryName(sourceFontFilePath);
string assetName = Path.GetFileNameWithoutExtension(sourceFontFilePath);
string newAssetFilePathWithName = AssetDatabase.GenerateUniqueAssetPath(folderPath + "/" + assetName + " SDF.asset");
// Initialize FontEngine
FontEngine.InitializeFontEngine();
// Load Font Face
if (FontEngine.LoadFontFace(sourceFont, 90) != FontEngineError.Success)
{
Debug.LogWarning("Unable to load font face for [" + sourceFont.name + "]. Make sure \"Include Font Data\" is enabled in the Font Import Settings.", sourceFont);
return;
}
// Create new Font Asset
TMP_FontAsset fontAsset = ScriptableObject.CreateInstance<TMP_FontAsset>();
AssetDatabase.CreateAsset(fontAsset, newAssetFilePathWithName);
fontAsset.version = "1.1.0";
fontAsset.faceInfo = FontEngine.GetFaceInfo();
// Set font reference and GUID
fontAsset.m_SourceFontFileGUID = AssetDatabase.AssetPathToGUID(sourceFontFilePath);
fontAsset.m_SourceFontFile_EditorRef = sourceFont;
fontAsset.atlasPopulationMode = AtlasPopulationMode.Dynamic;
// Default atlas resolution is 1024 x 1024.
int atlasWidth = fontAsset.atlasWidth = 1024;
int atlasHeight = fontAsset.atlasHeight = 1024;
int atlasPadding = fontAsset.atlasPadding = 9;
fontAsset.atlasRenderMode = GlyphRenderMode.SDFAA;
// Initialize array for the font atlas textures.
fontAsset.atlasTextures = new Texture2D[1];
// Create atlas texture of size zero.
Texture2D texture = new Texture2D(0, 0, TextureFormat.Alpha8, false);
texture.name = assetName + " Atlas";
fontAsset.atlasTextures[0] = texture;
AssetDatabase.AddObjectToAsset(texture, fontAsset);
// Add free rectangle of the size of the texture.
int packingModifier = ((GlyphRasterModes)fontAsset.atlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP ? 0 : 1;
fontAsset.freeGlyphRects = new List<GlyphRect>() { new GlyphRect(0, 0, atlasWidth - packingModifier, atlasHeight - packingModifier) };
fontAsset.usedGlyphRects = new List<GlyphRect>();
// Create new Material and Add it as Sub-Asset
Shader default_Shader = Shader.Find("TextMeshPro/Distance Field");
Material tmp_material = new Material(default_Shader);
tmp_material.name = texture.name + " Material";
tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, atlasPadding + packingModifier);
tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle);
tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle);
fontAsset.material = tmp_material;
AssetDatabase.AddObjectToAsset(tmp_material, fontAsset);
// Add Font Asset Creation Settings
fontAsset.creationSettings = new FontAssetCreationSettings(fontAsset.m_SourceFontFileGUID, fontAsset.faceInfo.pointSize, 0, atlasPadding, 0, 1024, 1024, 7, string.Empty, (int)GlyphRenderMode.SDFAA);
// Not sure if this is still necessary in newer versions of Unity.
EditorUtility.SetDirty(fontAsset);
AssetDatabase.SaveAssets();
}
}
}