Skip to content

Commit 5fb4e05

Browse files
committed
add: add fill font into TextToTMPConverterEditorWindow
1 parent 85f1970 commit 5fb4e05

File tree

2 files changed

+509
-330
lines changed

2 files changed

+509
-330
lines changed

Assets/Code/Editor/TextToTMPConverterEditorWindow.cs

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ namespace Code.Editor
77
{
88
public class TextToTMPConverterEditorWindow : EditorWindow
99
{
10-
private GameObject targetRoot;
10+
private GameObject _targetRoot;
11+
12+
private TMP_FontAsset _tmpFont;
13+
private Font _unityFont;
1114

1215
[MenuItem("Tools/Text <-> TMP Converter")]
1316
public static void ShowWindow()
@@ -18,39 +21,59 @@ public static void ShowWindow()
1821
private void OnGUI()
1922
{
2023
GUILayout.Label("Select Prefab or GameObject to Convert", EditorStyles.boldLabel);
24+
25+
DrawObjectFieldPrefab();
26+
27+
GUILayout.Space(10);
2128

22-
DrawObjectField();
29+
DrawFillFontTextToTMP_Pro();
2330

2431
DrawButtonConvertTextToTMP_Pro();
2532

26-
DrawButtonConvertTMP_ProToText();
33+
GUILayout.Space(10);
34+
35+
DrawFillFontTMP_ProToText();
36+
37+
DrawButtonConventTMP_ProToText();
2738
}
2839

29-
private void DrawObjectField()
40+
private void DrawFillFontTextToTMP_Pro()
3041
{
31-
targetRoot = (GameObject)EditorGUILayout.ObjectField("Target Root", targetRoot, typeof(GameObject), true);
42+
GUILayout.Label("Text ➜ TextMeshProUGUI", EditorStyles.boldLabel);
43+
_tmpFont = (TMP_FontAsset)EditorGUILayout.ObjectField("TMP Font", _tmpFont, typeof(TMP_FontAsset), false);
3244
}
3345

34-
private void DrawButtonConvertTMP_ProToText()
46+
private void DrawButtonConvertTextToTMP_Pro()
3547
{
36-
if (!GUILayout.Button("Convert TextMeshPro -> Text"))
37-
return;
38-
39-
if (targetRoot != null)
40-
ConvertTMPToText(targetRoot);
41-
else
42-
Debug.LogWarning("Please assign a target GameObject");
48+
if (GUILayout.Button("Convert Text -> TextMeshPro"))
49+
{
50+
if (_targetRoot == null)
51+
Debug.LogWarning("Please assign a target GameObject");
52+
else
53+
ConvertTextToTMP(_targetRoot);
54+
}
4355
}
4456

45-
private void DrawButtonConvertTextToTMP_Pro()
57+
private void DrawFillFontTMP_ProToText()
4658
{
47-
if (!GUILayout.Button("Convert Text -> TextMeshPro"))
48-
return;
49-
50-
if (targetRoot != null)
51-
ConvertTextToTMP(targetRoot);
52-
else
53-
Debug.LogWarning("Please assign a target GameObject");
59+
GUILayout.Label("TextMeshProUGUI ➜ Text", EditorStyles.boldLabel);
60+
_unityFont = (Font)EditorGUILayout.ObjectField("Unity Font", _unityFont, typeof(Font), false);
61+
}
62+
63+
private void DrawButtonConventTMP_ProToText()
64+
{
65+
if (GUILayout.Button("Convert TextMeshPro -> Text"))
66+
{
67+
if (_targetRoot == null)
68+
Debug.LogWarning("Please assign a target GameObject");
69+
else
70+
ConvertTMPToText(_targetRoot);
71+
}
72+
}
73+
74+
private void DrawObjectFieldPrefab()
75+
{
76+
_targetRoot = (GameObject)EditorGUILayout.ObjectField("Target Root", _targetRoot, typeof(GameObject), true);
5477
}
5578

5679
private void ConvertTextToTMP(GameObject root)
@@ -67,11 +90,14 @@ private void ConvertTextToTMP(GameObject root)
6790
GameObject go = oldText.gameObject;
6891
DestroyImmediate(oldText);
6992

70-
var tmp = go.AddComponent<TextMeshProUGUI>();
93+
TextMeshProUGUI tmp = go.AddComponent<TextMeshProUGUI>();
7194
tmp.text = textValue;
7295
tmp.fontSize = fontSize;
7396
tmp.color = color;
7497
tmp.alignment = ConvertAlignment(alignment);
98+
99+
if (_tmpFont != null)
100+
tmp.font = _tmpFont;
75101
}
76102
}
77103

@@ -85,14 +111,17 @@ private void ConvertTMPToText(GameObject root)
85111
Color color = oldTMP.color;
86112
TextAlignmentOptions alignment = oldTMP.alignment;
87113

88-
GameObject gameObject = oldTMP.gameObject;
114+
GameObject go = oldTMP.gameObject;
89115
DestroyImmediate(oldTMP);
90116

91-
Text text = gameObject.AddComponent<Text>();
117+
Text text = go.AddComponent<Text>();
92118
text.text = textValue;
93119
text.fontSize = Mathf.RoundToInt(fontSize);
94120
text.color = color;
95121
text.alignment = ConvertAlignment(alignment);
122+
123+
if (_unityFont != null)
124+
text.font = _unityFont;
96125
}
97126
}
98127

@@ -126,4 +155,4 @@ private TextAnchor ConvertAlignment(TextAlignmentOptions options) =>
126155
_ => TextAnchor.MiddleCenter,
127156
};
128157
}
129-
}
158+
}

0 commit comments

Comments
 (0)