1+ using TMPro ;
2+ using UnityEditor ;
3+ using UnityEngine ;
4+ using UnityEngine . UI ;
5+
6+ namespace Code . Editor
7+ {
8+ public class TextToTMPConverterEditorWindow : EditorWindow
9+ {
10+ private GameObject targetRoot ;
11+
12+ [ MenuItem ( "Tools/Text <-> TMP Converter" ) ]
13+ public static void ShowWindow ( )
14+ {
15+ GetWindow < TextToTMPConverterEditorWindow > ( "Text <-> TMP Converter" ) ;
16+ }
17+
18+ private void OnGUI ( )
19+ {
20+ GUILayout . Label ( "Select Prefab or GameObject to Convert" , EditorStyles . boldLabel ) ;
21+
22+ DrawObjectField ( ) ;
23+
24+ DrawButtonConvertTextToTMP_Pro ( ) ;
25+
26+ DrawButtonConvertTMP_ProToText ( ) ;
27+ }
28+
29+ private void DrawObjectField ( )
30+ {
31+ targetRoot = ( GameObject ) EditorGUILayout . ObjectField ( "Target Root" , targetRoot , typeof ( GameObject ) , true ) ;
32+ }
33+
34+ private void DrawButtonConvertTMP_ProToText ( )
35+ {
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" ) ;
43+ }
44+
45+ private void DrawButtonConvertTextToTMP_Pro ( )
46+ {
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" ) ;
54+ }
55+
56+ private void ConvertTextToTMP ( GameObject root )
57+ {
58+ Text [ ] texts = root . GetComponentsInChildren < Text > ( true ) ;
59+ foreach ( Text oldText in texts )
60+ {
61+ string textValue = oldText . text ;
62+ Font font = oldText . font ;
63+ int fontSize = oldText . fontSize ;
64+ Color color = oldText . color ;
65+ TextAnchor alignment = oldText . alignment ;
66+
67+ GameObject go = oldText . gameObject ;
68+ DestroyImmediate ( oldText ) ;
69+
70+ var tmp = go . AddComponent < TextMeshProUGUI > ( ) ;
71+ tmp . text = textValue ;
72+ tmp . fontSize = fontSize ;
73+ tmp . color = color ;
74+ tmp . alignment = ConvertAlignment ( alignment ) ;
75+ }
76+ }
77+
78+ private void ConvertTMPToText ( GameObject root )
79+ {
80+ TextMeshProUGUI [ ] tmps = root . GetComponentsInChildren < TextMeshProUGUI > ( true ) ;
81+ foreach ( var oldTMP in tmps )
82+ {
83+ string textValue = oldTMP . text ;
84+ float fontSize = oldTMP . fontSize ;
85+ Color color = oldTMP . color ;
86+ TextAlignmentOptions alignment = oldTMP . alignment ;
87+
88+ GameObject gameObject = oldTMP . gameObject ;
89+ DestroyImmediate ( oldTMP ) ;
90+
91+ Text text = gameObject . AddComponent < Text > ( ) ;
92+ text . text = textValue ;
93+ text . fontSize = Mathf . RoundToInt ( fontSize ) ;
94+ text . color = color ;
95+ text . alignment = ConvertAlignment ( alignment ) ;
96+ }
97+ }
98+
99+ private TextAlignmentOptions ConvertAlignment ( TextAnchor anchor ) =>
100+ anchor switch
101+ {
102+ TextAnchor . UpperLeft => TextAlignmentOptions . TopLeft ,
103+ TextAnchor . UpperCenter => TextAlignmentOptions . Top ,
104+ TextAnchor . UpperRight => TextAlignmentOptions . TopRight ,
105+ TextAnchor . MiddleLeft => TextAlignmentOptions . Left ,
106+ TextAnchor . MiddleCenter => TextAlignmentOptions . Center ,
107+ TextAnchor . MiddleRight => TextAlignmentOptions . Right ,
108+ TextAnchor . LowerLeft => TextAlignmentOptions . BottomLeft ,
109+ TextAnchor . LowerCenter => TextAlignmentOptions . Bottom ,
110+ TextAnchor . LowerRight => TextAlignmentOptions . BottomRight ,
111+ _ => TextAlignmentOptions . Center ,
112+ } ;
113+
114+ private TextAnchor ConvertAlignment ( TextAlignmentOptions options ) =>
115+ options switch
116+ {
117+ TextAlignmentOptions . TopLeft => TextAnchor . UpperLeft ,
118+ TextAlignmentOptions . Top => TextAnchor . UpperCenter ,
119+ TextAlignmentOptions . TopRight => TextAnchor . UpperRight ,
120+ TextAlignmentOptions . Left => TextAnchor . MiddleLeft ,
121+ TextAlignmentOptions . Center => TextAnchor . MiddleCenter ,
122+ TextAlignmentOptions . Right => TextAnchor . MiddleRight ,
123+ TextAlignmentOptions . BottomLeft => TextAnchor . LowerLeft ,
124+ TextAlignmentOptions . Bottom => TextAnchor . LowerCenter ,
125+ TextAlignmentOptions . BottomRight => TextAnchor . LowerRight ,
126+ _ => TextAnchor . MiddleCenter ,
127+ } ;
128+ }
129+ }
0 commit comments