UnityTextMeshPro字库的⾃动⽣成
getsavefilename原因
在 Unity 中使⽤ TextMeshPro 的⽂本解决⽅案,当要⽣成字库图集时,需要通过插件提供的 【Font Asset Creator】⼯具,但是每次都需要重新设置选项,如下所⽰:
解决
反编译编辑器查看其代码,照着其实现步骤再实现⼀遍,注意的是升级插件的话,也要看下实现的代码是否需要改动。另外,⾃动处理多个字体的部分逻辑如下:
public void Update()
{
if (this.isProcessing)
{
var progress = TMPro_FontPlugin.Check_RenderProgress();
m_FontAssetInfos[m_CurGenerateIndex].genPercent = progress * 100;
this.Repaint();
}
if (!this.isRenderingDone)
{
return;
}
this.isProcessing = false;
this.isRenderingDone = false;
this.UpdateRenderFeedbackWindow();
foreach (var asset in m_FontAssetInfos[m_CurGenerateIndex].assets)
{
this.CreateFontTexture();
Save_SDF_FontAsset(asset);
}
GenerateNext();
}
public void OnGUI()
{
EditorGUI.indentLevel++;
GUILayout.Label(" Font Word", EditorStyles.boldLabel);
if (GUILayout.Button("⽣成字库⽂本", GUILayout.MinHeight(22f)))
{
TextMeshProFontTextGen.GenChineseText();
}
GUILayout.Space(10f);
GUILayout.Label(" Font Asset", EditorStyles.boldLabel);
if (GUILayout.Button("⽣成字库资产", GUILayout.MinHeight(22f)))
{
Generate();
}
GUILayout.Space(10f);
GUILayout.Label(" Font List", EditorStyles.boldLabel);
foreach (var info in m_FontAssetInfos)
{
EditorGUILayout.BeginHorizontal();
GUILayout.Space(10f);
EditorGUILayout.LabelField(EditorGUIUtil.TempContent($"({Percent}%)"));
EditorGUILayout.EndHorizontal();
EditorGUI.indentLevel++;
EditorGUI.indentLevel++;
foreach (var asset in info.assets)
{
EditorGUILayout.LabelField(EditorGUIUtil.TempContent(Path.GetFileNameWithoutExtension(asset)));            }
EditorGUI.indentLevel--;
EditorGUI.indentLevel--;
}
EditorGUI.indentLevel--;
}
private void FindFonts()
{
string str1 = "t:Font";
string[] fonts = AssetDatabase.FindAssets(str1, new[] { "Assets/TextMeshPro" });
m_FontAssetInfos.Clear();
foreach (var font in fonts)
{
FontAssetInfo info = new FontAssetInfo();
info.fontPath = AssetDatabase.GUIDToAssetPath(font);
info.fontName = Path.GetFileNameWithoutExtension(info.fontPath);
info.fontObj = AssetDatabase.LoadAssetAtPath<Font>(info.fontPath);
if (info.fontObj == null)
{
continue;
}
List<string> assetPaths = new List<string>();
str1 = "t:TMP_FontAsset " + info.fontName + "_SDF";
var assets = AssetDatabase.FindAssets(str1, new[] { "Assets/TextMeshPro/Resources/Fonts_Materials" });            foreach (var asset in assets)
{
assetPaths.Add(AssetDatabase.GUIDToAssetPath(asset));
}
info.assets = assetPaths.ToArray();
m_FontAssetInfos.Add(info);
}
}
private void Generate()
{
m_CurGenerateIndex = -1;
GenerateNext();
}
private void GenerateNext()
{
m_CurGenerateIndex++;
if (m_CurGenerateIndex >= m_FontAssetInfos.Count)
{
EditorUtility.DisplayDialog("提⽰", "⽣成字库资产成功!", "OK");
return;
}
var info = m_FontAssetInfos[m_CurGenerateIndex];
if (!le)
{
GenerateNext();
return;
}
font_TTF = info.fontObj;
font_size = 22;
font_atlas_width = 2048;
font_atlas_height = 2048;
if (string.IsNullOrEmpty(characterSequence))
{
var characterList = AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/TextMeshPro/");            this.characterSequence = ;
}
DoCreate();
}
完整代码:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。