Upgrading TextMesh Pro from an old Unity project
Table of Contents
So If you are like me, you have a bazziloin projects you started, some you have finished and even some you have published. If you have made good money on your published projects, please tell me how! It’s almost the bane of a creator’s life to not get equally rewarded for their work… until you are dead I guess. Honestly, we live in an age that rewards creativity more than ever before (I think). Regardless of whether we made money or not, we still do it. We do it because it’s cool and we love it.
To that end, I have been bringing out some projects from the archives and upgrading them. These projects are mostly from the Unity 5.x time period (seems like forever ago!) The biggest issue seems to be migrating to the package manager included version of TextMesh Pro. You can spend weeks of your life rebuilding text prefabs. Thank goodness, the good folks at Unity have included a little tool to help. Im using Unity 2018.2.8f1 FYI. You can find this TextMesh Pro tool from Window -> TextMeshPro -> Project Files GUID Remapping Tool.
And this tool looks like (Yea I have Unity Pro! I do it just for the dark skin because I love my eyes!)
Now it’s tricky getting to it if you have both versions in your project. If you don't see the menu option, its reading the old TMP editor data over the new.
So what you do is #
- Delete the old version of TMP. Make sure you are all committed before you do this!
- Now the new menu option should be there. If not, close and reopen your project. (Remember, Editor scripts get compiled after game scripts, so it can take a second. Wait for the spin wheel in the bottom right hand corner to stop.)
- Open the GUID remapping tool and dock it somewhere.
- Restore your old version of TextMesh Pro from source control.
- Run the tool by pressing "Scan Project Files" and then pressing "Save Modified Project Files".
- Delete your old version of TextMesh Pro.
This method has been working really well for me. Most of the time there is little clean up. But now that I have gone back to Unity 5 projects and I have been a slow adopter of RectTransform, I found a new issue. The latest version of TeshMesh Pro is fully into RectTransforms and gives error messages. Basically, they have fully migrated from their old Text Container script to the RectTransform. Fully understandable but now that leaves me going through all my text objects and adding the RectTransform, migrating the size data and then removing the Text Container.
Thank goodness, Im a programmer and can write tools for repetitive tasks. And since I am a good citizen of the interwebs, here it is. If you dislike the way I code, feel free to keep those comments to yourself, but if you can make this better, please modify and re-share in the comments.
The Code #
//***************************************
//* TMP PRO TOOLS
//***************************************
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using TMPro;
//********************************************
//* NAMESPACE
//********************************************
namespace CollectiveMass{
//********************************************
//* CLASS
//********************************************
public class TMPTools : EditorWindow {
//********************************************
//* ENUM
//********************************************
//********************************************
//* CONSTANTS
//********************************************
//********************************************
//* VARIABLES
//********************************************
private Vector2 scrollPostion = new Vector2();
//********************************************
//* LAUNCH METHOD
//********************************************
[MenuItem("Tools/TMP Tools")]
public static void Init () {
// Get existing open window or if none, make a new one:
EditorWindow.GetWindow (typeof (TMPTools));
}
//********************************************
//* UNITY METHODS
//********************************************
private void Update(){
Repaint();
}
private void OnGUI () {
//*** Render
Render();
}
//********************************************
//* MAIN METHODS
//********************************************
private void Render(){
//*** start Scrolling
scrollPostion = EditorGUILayout.BeginScrollView(scrollPostion,GUIStyle.none);
GUILayout.Label ("Text Mesh Pro Tools", EditorStyles.boldLabel);
//*** Space
GUILayout.Space(20);
//*** Rotating
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Fix Rect Transforms", GUILayout.Height(30))) {
FixRectTransforms();
}
EditorGUILayout.EndHorizontal();
//*** End Scroll view
EditorGUILayout.EndScrollView();
}
private void FixRectTransforms(){
//*** Variables
int i;
int count = 0;
List<TextMeshPro> aTMPObjects = new List<TextMeshPro>();
//*** Get all text objects in scene
Scene oScene = SceneManager.GetActiveScene();
GameObject[] aRootGameObjects = oScene.GetRootGameObjects();
//*** Loop through roots and get Text Objects
for(i=0; i< aRootGameObjects.Length; i++){
//*** Get Text Objects in Children
TextMeshPro[] aTextObjects = aRootGameObjects[i].GetComponentsInChildren<TextMeshPro>();
//*** Add to stack
aTMPObjects.AddRange(aTextObjects);
}
//*** Iterate through Text objects and solve
for(i=0; i<aTMPObjects.Count; i++){
//*** Chgeck if it has a rect transfomr
RectTransform oRect = aTMPObjects[i].GetComponent<RectTransform>();
TextContainer oContainer = aTMPObjects[i].GetComponent<TextContainer>();
//*** If does not have
if(
(oRect == null)
&&(oContainer != null)
){
//*** Get Values
float xWidth = oContainer.width;
float xHeight = oContainer.height;
//*** Add rect
oRect = aTMPObjects[i].gameObject.AddComponent<RectTransform>();
oRect.sizeDelta = new Vector2(xWidth, xHeight);
//*** Remove container (Maybe check margins here)
DestroyImmediate(oContainer);
//*** Count successful change
count++;
}
}
//*** Tell the user that the job is done and what was done.
EditorUtility.DisplayDialog("Text Mesh Pro Rect Transform", count + " TMP objects fixed.", "Okay");
}
}
}