-
-
Save BlindingSun/915fd5b68c11515f9ce6dbcd472fefc7 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 hidden or 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
/// Revision info: | |
/// Updated initial project from Naphier to make it work for Unity 2019.4 | |
/// | |
/// Changes: | |
/// 1) Include change from levilansing because TMP is now part of Unity. | |
/// 2) Added using needed for BindingFlags. | |
using System.Reflection; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEngine.UI; | |
using TMPro; | |
using TMPro.EditorUtilities; | |
public class UGuiTextToTextMeshPro : Editor | |
{ | |
[MenuItem("GameObject/UI/Convert To Text Mesh Pro", false, 4000)] | |
static void DoIt() | |
{ | |
Text uiText = Selection.activeGameObject.GetComponent<Text>(); | |
if (uiText == null) | |
{ | |
EditorUtility.DisplayDialog( | |
"ERROR!", "You must select a Unity UI Text Object to convert.", "OK", ""); | |
return; | |
} | |
MenuCommand command = new MenuCommand(uiText); | |
var method = typeof(TMPro_CreateObjectMenu).GetMethod("CreateTextMeshProGuiObjectPerform", BindingFlags.Static | BindingFlags.NonPublic); | |
method.Invoke(null, new object[] { command }); | |
TextMeshProUGUI 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 | |
Component[] components = uiText.GetComponents<Component>(); | |
int componentsCopied = 0; | |
for (int i = 0; i < components.Length; i++) | |
{ | |
var thisType = components[i].GetType(); | |
if (thisType == typeof(Text) || | |
thisType == typeof(RectTransform) || | |
thisType == typeof(Transform) || | |
thisType == typeof(CanvasRenderer)) | |
continue; | |
UnityEditorInternal.ComponentUtility.CopyComponent(components[i]); | |
UnityEditorInternal.ComponentUtility.PasteComponentAsNew(tmp.gameObject); | |
componentsCopied++; | |
} | |
if (componentsCopied == 0) | |
Undo.DestroyObjectImmediate((Object)uiText.gameObject); | |
else | |
{ | |
EditorUtility.DisplayDialog( | |
"uGUI to TextMesh Pro", | |
string.Format( | |
"{0} components copied. Please check for accuracy as some references may not transfer properly.", | |
componentsCopied), | |
"OK", | |
""); | |
uiText.name += " OLD"; | |
uiText.gameObject.SetActive(false); | |
} | |
} | |
private static FontStyles GetTmpFontStyle(FontStyle uGuiFontStyle) | |
{ | |
FontStyles tmp = FontStyles.Normal; | |
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 = TextAlignmentOptions.TopLeft; | |
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; | |
} | |
} |
Added this at line 68 to keep child transforms attached to the transform with the TextMeshPro component.
Transform[] allChildren = uiText.gameObject.GetComponentsInChildren<Transform>();
for (int i = 0; i < allChildren.Length; i++)
{
if (allChildren[i].parent == uiText.transform)
{
allChildren[i].parent = tmp.transform;
}
}`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! I used it in a project upgrade situation I had. I also added multiple selection option, to convert all Text to TMP in the scene.
GameObject[] allSelectedGos = Selection.gameObjects;
foreach (var go in allSelectedGos)
{
Text uiText = go.GetComponent();
etc..
Then in the Hierarchy search filter search for t:Text, this will show all gameobjects with Text Component, select them all GameObject>UI>Convert To TMP