Created
March 11, 2020 08:38
-
-
Save stilllisisi/4c49ed17bb7b3d2e90705a94157a4a8b to your computer and use it in GitHub Desktop.
【游戏-unity】Unity TextMeshPro 字库的自动生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//在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() | |
{ | |
GUI.enabled = !this.isProcessing; | |
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(); | |
info.toggle = EditorGUILayout.ToggleLeft(info.fontName, info.toggle); | |
GUILayout.Space(10f); | |
EditorGUILayout.LabelField(EditorGUIUtil.TempContent($"({info.genPercent}%)")); | |
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--; | |
GUI.enabled = true; | |
} | |
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 (!info.toggle) | |
{ | |
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/chinese_3500.txt"); | |
this.characterSequence = characterList.text; | |
} | |
DoCreate(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gameinstitute.qq.com/community/detail/128725