Skip to content

Instantly share code, notes, and snippets.

@Naphier
Created January 4, 2017 15:23
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save Naphier/df8b56b8b879b6f33ec4eea8e98840b9 to your computer and use it in GitHub Desktop.
Save Naphier/df8b56b8b879b6f33ec4eea8e98840b9 to your computer and use it in GitHub Desktop.
Unity3D Editor Tool to convert Unity GUI Text objects to Text Mesh Pro Text Objects
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);
// If you have an error due to this line then go to TMPro_CreateObjectMenu.cs
// and make this method public.
TMPro_CreateObjectMenu.CreateTextMeshProGuiObjectPerform(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;
}
}
@levilansing
Copy link

Thanks! Now that Unity has integrated TMP, you can't just make the private method CreateTextMeshProObjectPerform public. I changed line 24 to this to get it working:

    var method = typeof(TMPro_CreateObjectMenu).GetMethod("CreateTextMeshProGuiObjectPerform", BindingFlags.Static | BindingFlags.NonPublic);
    method.Invoke(null, new object[] { command });

@BlindingSun
Copy link

Thanks to Naphier it was very helpful!

I forked a new version which includes levilansing's update and added a missing Using that was needed when compiling under VS 2017 for Unity 2019.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment