Forked from FairlySadPanda/UGuiTextToTextMeshPro.cs
Last active
June 25, 2022 12:37
-
-
Save yuna0x0/8d09ce0df98d45993a29babd66a9a47f to your computer and use it in GitHub Desktop.
Unity3D Editor Tool to convert Unity GUI Text objects to Text Mesh Pro Text Objects
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
using TMPro; | |
using TMPro.EditorUtilities; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class UGuiTextToTextMeshPro : Editor | |
{ | |
[MenuItem("GameObject/UI/Convert To Text Mesh Pro", false, 4000)] | |
private static void DoIt() | |
{ | |
foreach (var obj in Selection.gameObjects) | |
{ | |
var uiText = obj.GetComponent<Text>(); | |
if (uiText == null) | |
continue; | |
var command = new MenuCommand(uiText); | |
var method = typeof(TMPro_CreateObjectMenu).GetMethod("CreateTextMeshProGuiObjectPerform", | |
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic); | |
if (method != null) method.Invoke(null, new object[] { command }); | |
var tmp = Selection.activeGameObject.GetComponent<TextMeshProUGUI>(); | |
if (tmp == null) | |
{ | |
EditorUtility.DisplayDialog( | |
"ERROR!", | |
"Something went wrong! Text Mesh Pro did not select the newly created object.", | |
"OK", | |
""); | |
return; | |
} | |
tmp.fontStyle = GetTmpFontStyle(uiText.fontStyle); | |
tmp.fontSize = uiText.fontSize; | |
tmp.fontSizeMin = uiText.resizeTextMinSize; | |
tmp.fontSizeMax = uiText.resizeTextMaxSize; | |
tmp.enableAutoSizing = uiText.resizeTextForBestFit; | |
tmp.alignment = GetTmpAlignment(uiText.alignment); | |
tmp.text = uiText.text; | |
tmp.color = uiText.color; | |
tmp.transform.SetParent(uiText.transform.parent); | |
tmp.name = uiText.name; | |
tmp.rectTransform.anchoredPosition3D = uiText.rectTransform.anchoredPosition3D; | |
tmp.rectTransform.anchorMax = uiText.rectTransform.anchorMax; | |
tmp.rectTransform.anchorMin = uiText.rectTransform.anchorMin; | |
tmp.rectTransform.localPosition = uiText.rectTransform.localPosition; | |
tmp.rectTransform.localRotation = uiText.rectTransform.localRotation; | |
tmp.rectTransform.localScale = uiText.rectTransform.localScale; | |
tmp.rectTransform.pivot = uiText.rectTransform.pivot; | |
tmp.rectTransform.sizeDelta = uiText.rectTransform.sizeDelta; | |
tmp.transform.SetSiblingIndex(uiText.transform.GetSiblingIndex()); | |
// Copy all other components | |
var components = uiText.GetComponents<Component>(); | |
var componentsCopied = 0; | |
foreach (var t in components) | |
{ | |
var thisType = t.GetType(); | |
if (thisType == typeof(Text) || | |
thisType == typeof(RectTransform) || | |
thisType == typeof(Transform) || | |
thisType == typeof(CanvasRenderer)) | |
continue; | |
UnityEditorInternal.ComponentUtility.CopyComponent(t); | |
UnityEditorInternal.ComponentUtility.PasteComponentAsNew(tmp.gameObject); | |
componentsCopied++; | |
} | |
if (componentsCopied == 0) | |
Undo.DestroyObjectImmediate(uiText.gameObject); | |
else | |
{ | |
EditorUtility.DisplayDialog( | |
"uGUI to TextMesh Pro", | |
$"{componentsCopied} components copied. Please check for accuracy as some references may not transfer properly.", | |
"OK", | |
""); | |
uiText.name += " OLD"; | |
uiText.gameObject.SetActive(false); | |
} | |
} | |
} | |
private static FontStyles GetTmpFontStyle(FontStyle uGuiFontStyle) | |
{ | |
FontStyles tmp; | |
switch (uGuiFontStyle) | |
{ | |
case FontStyle.Normal: | |
default: | |
tmp = FontStyles.Normal; | |
break; | |
case FontStyle.Bold: | |
tmp = FontStyles.Bold; | |
break; | |
case FontStyle.Italic: | |
tmp = FontStyles.Italic; | |
break; | |
case FontStyle.BoldAndItalic: | |
tmp = FontStyles.Bold | FontStyles.Italic; | |
break; | |
} | |
return tmp; | |
} | |
private static TextAlignmentOptions GetTmpAlignment(TextAnchor uGuiAlignment) | |
{ | |
TextAlignmentOptions alignment; | |
switch (uGuiAlignment) | |
{ | |
default: | |
case TextAnchor.UpperLeft: | |
alignment = TextAlignmentOptions.TopLeft; | |
break; | |
case TextAnchor.UpperCenter: | |
alignment = TextAlignmentOptions.Top; | |
break; | |
case TextAnchor.UpperRight: | |
alignment = TextAlignmentOptions.TopRight; | |
break; | |
case TextAnchor.MiddleLeft: | |
alignment = TextAlignmentOptions.MidlineLeft; | |
break; | |
case TextAnchor.MiddleCenter: | |
alignment = TextAlignmentOptions.Midline; | |
break; | |
case TextAnchor.MiddleRight: | |
alignment = TextAlignmentOptions.MidlineRight; | |
break; | |
case TextAnchor.LowerLeft: | |
alignment = TextAlignmentOptions.BottomLeft; | |
break; | |
case TextAnchor.LowerCenter: | |
alignment = TextAlignmentOptions.Bottom; | |
break; | |
case TextAnchor.LowerRight: | |
alignment = TextAlignmentOptions.BottomRight; | |
break; | |
} | |
return alignment; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment