Last active
March 4, 2025 16:54
-
-
Save NicholasSheehan/0c5b690e246e72b7f5558c64b83c2150 to your computer and use it in GitHub Desktop.
Convert Unity Text Component to Text Mesh Pro Component
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
using TMPro; | |
using UnityEditor; | |
using UnityEngine; | |
using UnityEngine.UI; | |
namespace NicholasSheehan | |
{ | |
//Place in an editor folder | |
class TextToTextMeshPro : MonoBehaviour | |
{ | |
[MenuItem("Tools/Replace Text Component With Text Mesh Pro", true)] | |
static bool TextSelectedValidation() | |
{ | |
var selectedObjects = Selection.gameObjects; | |
if (selectedObjects.Length == 0) return false; | |
foreach (var selectedObject in selectedObjects) | |
{ | |
var text = selectedObject.GetComponent<Text>(); | |
if (!text) return false; | |
} | |
return true; | |
} | |
[MenuItem("Tools/Replace Text Component With Text Mesh Pro")] | |
static void ReplaceSelectedObjects() | |
{ | |
var selectedObjects = Selection.gameObjects; | |
Undo.RecordObjects(selectedObjects, "Replace Text Component with Text Mesh Pro Component"); | |
foreach (var selectedObject in selectedObjects) | |
{ | |
var textComp = selectedObject.GetComponent<Text>(); | |
var textSizeDelta = textComp.rectTransform.sizeDelta; | |
//text component is still alive in memory, so the settings are still intact | |
Undo.DestroyObjectImmediate(textComp); | |
var tmp = Undo.AddComponent<TextMeshProUGUI>(selectedObject); | |
tmp.text = textComp.text; | |
tmp.fontSize = textComp.fontSize; | |
var fontStyle = textComp.fontStyle; | |
switch (fontStyle) | |
{ | |
case FontStyle.Normal: | |
tmp.fontStyle = FontStyles.Normal; | |
break; | |
case FontStyle.Bold: | |
tmp.fontStyle = FontStyles.Bold; | |
break; | |
case FontStyle.Italic: | |
tmp.fontStyle = FontStyles.Italic; | |
break; | |
case FontStyle.BoldAndItalic: | |
tmp.fontStyle = FontStyles.Bold | FontStyles.Italic; | |
break; | |
} | |
tmp.enableAutoSizing = textComp.resizeTextForBestFit; | |
tmp.fontSizeMin = textComp.resizeTextMinSize; | |
tmp.fontSizeMax = textComp.resizeTextMaxSize; | |
var alignment = textComp.alignment; | |
switch (alignment) | |
{ | |
case TextAnchor.UpperLeft: | |
tmp.alignment = TextAlignmentOptions.TopLeft; | |
break; | |
case TextAnchor.UpperCenter: | |
tmp.alignment = TextAlignmentOptions.Top; | |
break; | |
case TextAnchor.UpperRight: | |
tmp.alignment = TextAlignmentOptions.TopRight; | |
break; | |
case TextAnchor.MiddleLeft: | |
tmp.alignment = TextAlignmentOptions.MidlineLeft; | |
break; | |
case TextAnchor.MiddleCenter: | |
tmp.alignment = TextAlignmentOptions.Midline; | |
break; | |
case TextAnchor.MiddleRight: | |
tmp.alignment = TextAlignmentOptions.MidlineRight; | |
break; | |
case TextAnchor.LowerLeft: | |
tmp.alignment = TextAlignmentOptions.BottomLeft; | |
break; | |
case TextAnchor.LowerCenter: | |
tmp.alignment = TextAlignmentOptions.Bottom; | |
break; | |
case TextAnchor.LowerRight: | |
tmp.alignment = TextAlignmentOptions.BottomRight; | |
break; | |
} | |
tmp.enableWordWrapping = textComp.horizontalOverflow == HorizontalWrapMode.Wrap; | |
tmp.color = textComp.color; | |
tmp.raycastTarget = textComp.raycastTarget; | |
tmp.richText = textComp.supportRichText; | |
tmp.rectTransform.sizeDelta = textSizeDelta; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment