├── Assets └── TMProUpdater │ ├── RectTransformReset.cs │ ├── RectTransformReset.cs.meta │ ├── TMProUpgrader.cs │ └── TMProUpgrader.cs.meta ├── LICENSE ├── README.md └── preview.png /Assets/TMProUpdater/RectTransformReset.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using TMPro; 4 | using UnityEngine; 5 | 6 | [ExecuteInEditMode] 7 | public class RectTransformReset : MonoBehaviour 8 | { 9 | private Vector2 offsetMin; 10 | private Vector2 offsetMax; 11 | private RectTransform rectTransform; 12 | private int frameCounter = 0; 13 | 14 | public void Init(RectTransform rectTransform, Vector2 offsetMin, Vector2 offsetMax) 15 | { 16 | this.rectTransform = rectTransform; 17 | this.offsetMin = offsetMin; 18 | this.offsetMax = offsetMax; 19 | } 20 | 21 | 22 | void Update() 23 | { 24 | frameCounter++; 25 | if (frameCounter >= 1) 26 | { 27 | rectTransform.offsetMin = this.offsetMin; 28 | rectTransform.offsetMax = this.offsetMax; 29 | DestroyImmediate(this.gameObject, false); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Assets/TMProUpdater/RectTransformReset.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 055e6791831e6024db9b3f4c2c5f09f3 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /Assets/TMProUpdater/TMProUpgrader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using TMPro; 5 | using UnityEditor; 6 | using UnityEngine; 7 | using UnityEngine.UI; 8 | 9 | public class TMProUpgrader : MonoBehaviour 10 | { 11 | #if UNITY_EDITOR 12 | static bool ConvertText(GameObject textObj) 13 | { 14 | var text = textObj.GetComponent(); 15 | if (text == null) 16 | { 17 | Debug.Log("No text found to upgrade."); 18 | return false; 19 | } 20 | 21 | //search for font asset 22 | string[] fontNames = text.font.fontNames; 23 | var results = AssetDatabase.FindAssets("t:TMP_FontAsset"); 24 | TMP_FontAsset chosenFontAsset = null; 25 | 26 | foreach (var result in results) 27 | { 28 | string path = AssetDatabase.GUIDToAssetPath(result); 29 | var asset = AssetDatabase.LoadMainAssetAtPath(path); 30 | if (asset is TMP_FontAsset fontAsset) 31 | { 32 | if(Array.IndexOf(fontNames, fontAsset.faceInfo.familyName) >= 0) 33 | { 34 | chosenFontAsset = fontAsset; 35 | break; 36 | } 37 | } 38 | } 39 | 40 | //can't find font asset' 41 | if (chosenFontAsset == null) 42 | { 43 | Debug.LogError($"Can't find suitable font for upgrade from ({string.Join(",", fontNames)})."); 44 | return false; 45 | } 46 | 47 | //preform upgrade! 48 | //store recttransform since TMP insists on changing it 49 | var rectTransform = textObj.transform as RectTransform; 50 | var rt_offMin = rectTransform.offsetMin; 51 | var rt_offMax = rectTransform.offsetMax; 52 | 53 | //store text properties 54 | int textSize = text.fontSize; 55 | int textResizeMaxSize = text.resizeTextMaxSize; 56 | int textResizeMinSize = text.resizeTextMinSize; 57 | bool textResize = text.resizeTextForBestFit; 58 | TextAnchor textAlign = text.alignment; 59 | FontStyle textStyle = text.fontStyle; 60 | string textText = text.text; 61 | Color textColor = text.color; 62 | bool textAllowRich = text.supportRichText; 63 | HorizontalWrapMode textHorizontalOverflow = text.horizontalOverflow; 64 | VerticalWrapMode textVerticalOverflow = text.verticalOverflow; 65 | float textLineSpacing = text.lineSpacing; 66 | bool textRaycastTarget = text.raycastTarget; 67 | 68 | //delete text 69 | DestroyImmediate(text, false); 70 | 71 | //add TMP 72 | var textTMP = textObj.AddComponent(); 73 | textTMP.font = chosenFontAsset; 74 | textTMP.richText = textAllowRich; 75 | textTMP.enableWordWrapping = (textVerticalOverflow == VerticalWrapMode.Overflow) && (textHorizontalOverflow == HorizontalWrapMode.Wrap); 76 | textTMP.overflowMode = (textHorizontalOverflow == HorizontalWrapMode.Wrap) && (textVerticalOverflow == VerticalWrapMode.Overflow ) 77 | ? TextOverflowModes.Overflow 78 | : TextOverflowModes.Truncate; 79 | textTMP.lineSpacing = textLineSpacing; //TODO: finish this! 80 | textTMP.raycastTarget = textRaycastTarget; 81 | 82 | //setup TMP overflow 83 | if (textStyle == FontStyle.Bold || textStyle == FontStyle.BoldAndItalic) 84 | textTMP.fontStyle |= FontStyles.Bold; 85 | if (textStyle == FontStyle.Italic || textStyle == FontStyle.BoldAndItalic) 86 | textTMP.fontStyle |= FontStyles.Italic; 87 | 88 | //setup TMP alignment 89 | switch (textAlign) 90 | { 91 | case TextAnchor.LowerCenter: 92 | textTMP.alignment = TextAlignmentOptions.Bottom; 93 | break; 94 | case TextAnchor.LowerLeft: 95 | textTMP.alignment = TextAlignmentOptions.BottomLeft; 96 | break; 97 | case TextAnchor.LowerRight: 98 | textTMP.alignment = TextAlignmentOptions.BottomRight; 99 | break; 100 | case TextAnchor.MiddleCenter: 101 | textTMP.alignment = TextAlignmentOptions.Center; 102 | break; 103 | case TextAnchor.MiddleLeft: 104 | textTMP.alignment = TextAlignmentOptions.Left; 105 | break; 106 | case TextAnchor.MiddleRight: 107 | textTMP.alignment = TextAlignmentOptions.Right; 108 | break; 109 | case TextAnchor.UpperCenter: 110 | textTMP.alignment = TextAlignmentOptions.Top; 111 | break; 112 | case TextAnchor.UpperLeft: 113 | textTMP.alignment = TextAlignmentOptions.TopLeft; 114 | break; 115 | case TextAnchor.UpperRight: 116 | textTMP.alignment = TextAlignmentOptions.TopRight; 117 | break; 118 | } 119 | 120 | //setup resize parameters 121 | if (textResize) 122 | { 123 | textTMP.enableAutoSizing = true; 124 | textTMP.fontSizeMax = textResizeMaxSize; 125 | textTMP.fontSizeMin = textResizeMinSize; 126 | } 127 | else 128 | { 129 | textTMP.fontSize = textSize; 130 | } 131 | 132 | //replace rich text stuff 133 | if (textAllowRich) 134 | { 135 | textText = textText.Replace("(); 148 | resetter.Init(rectTransform, rt_offMin, rt_offMax); 149 | 150 | //DONE! 151 | return true; 152 | } 153 | 154 | [MenuItem("Dummiesmans Tools/Upgrade Scene To TMP")] 155 | static void UpgradeScene() 156 | { 157 | bool shouldProceed = EditorUtility.DisplayDialog("Upgrade entire scene?", 158 | "Are you sure you want to upgrade the entire scene? Note that no undo history will be created, and you should save any unsaved work before running this.", "Convert Entire Scene", "Cancel"); 159 | if (!shouldProceed) 160 | return; 161 | 162 | var allTextObjs = Resources.FindObjectsOfTypeAll(); 163 | var scene = UnityEngine.SceneManagement.SceneManager.GetActiveScene(); 164 | 165 | int numConverted = 0; 166 | int numFailed = 0; 167 | foreach (var textObj in allTextObjs) 168 | { 169 | var go = textObj.gameObject; 170 | if (go == null || go.scene != scene) 171 | continue; 172 | if (ConvertText(go)) 173 | { 174 | numConverted++; 175 | } 176 | else 177 | { 178 | numFailed++; 179 | } 180 | } 181 | 182 | if(numConverted > 0) 183 | Debug.Log($"Successfully converted {numConverted} texts."); 184 | if(numFailed > 0) 185 | Debug.Log($"Failed to convert {numFailed} texts."); 186 | if(numConverted == 0 && numFailed == 0) 187 | Debug.Log("No texts found to convert."); 188 | } 189 | 190 | [MenuItem("Dummiesmans Tools/Upgrade Selected To TMP")] 191 | static void UpgradeSelected() 192 | { 193 | var activeTransforms = Selection.transforms; 194 | foreach (var transform in activeTransforms) 195 | { 196 | ConvertText(transform.gameObject); 197 | } 198 | } 199 | #endif 200 | } 201 | -------------------------------------------------------------------------------- /Assets/TMProUpdater/TMProUpgrader.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cf34c5b9ae44c804e8d0156628727a5c 3 | MonoImporter: 4 | externalObjects: {} 5 | serializedVersion: 2 6 | defaultReferences: [] 7 | executionOrder: 0 8 | icon: {instanceID: 0} 9 | userData: 10 | assetBundleName: 11 | assetBundleVariant: 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Dummiesman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UnityTextMeshProUpgrader 2 | Upgrades standard UGUI text to TextMeshPro 3 | 4 | ![Preview](preview.png?raw=true) 5 | 6 | **Usage** 7 | Per-text: Select desired text objects, click "Dummiesmans Tools" up top and then "Upgrade Selected to TMP" 8 | 9 | Entire scene: click "Dummiesmans Tools" up top and then "Upgrade Scene to TMP" 10 | 11 | 12 | **Notes** 13 | - Does not create undo history, save before using if you're worried! 14 | 15 | - Does not update scripts or references, that's left to you. 16 | 17 | - Does not create font assets, that's also left to you. 18 | -------------------------------------------------------------------------------- /preview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Dummiesman/UnityTextMeshProUpgrader/8c3358b7a6b91af6f595935210bdae15a54eb295/preview.png --------------------------------------------------------------------------------