Giter Site home page Giter Site logo

rlzicar / unitytexttotextmeshproupgradetool Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yasirkula/unitytexttotextmeshproupgradetool

0.0 0.0 0.0 140 KB

Upgrade Text, InputField, Dropdown and TextMesh objects to their TextMesh Pro variants in Unity

License: MIT License

C# 100.00%

unitytexttotextmeshproupgradetool's Introduction

Text To TextMesh Pro Upgrade Tool for Unity

screenshot

Available on Asset Store: https://assetstore.unity.com/packages/tools/utilities/text-to-textmesh-pro-upgrade-tool-176732

Forum Thread: https://forum.unity.com/threads/text-to-textmesh-pro-upgrade-tool-open-source.947948/

Discord: https://discord.gg/UJJt549AaV

GitHub Sponsors โ˜•

This asset helps you upgrade the Text, InputField, Dropdown and TextMesh objects in your projects to their TextMesh Pro variants. It also upgrades the scripts so that e.g. Text variables in those scripts become TMP_Text variables. Then, it reconnects the references to the upgraded components (e.g. if a public variable was referencing an upgraded Text component, it will now reference the corresponding TextMeshProUGUI component).

INSTALLATION

There are 5 ways to install this plugin:

  • import TextToTMP.unitypackage via Assets-Import Package
  • clone/download this repository and move the Plugins folder to your Unity project's Assets folder
  • import it from Asset Store
  • (via Package Manager) add the following line to Packages/manifest.json:
    • "com.yasirkula.texttotmp": "https://github.com/yasirkula/UnityTextToTextMeshProUpgradeTool.git",
  • (via OpenUPM) after installing openupm-cli, run the following command:
    • openupm add com.yasirkula.texttotmp

HOW TO

Before proceeding, you are strongly recommended to backup your project; just in case.

  • Open the Window-Upgrade Text to TMP window
  • Add the prefabs, Scenes, scripts and ScriptableObjects to upgrade to the Assets & Scenes To Upgrade list (if you add a folder there, its whole contents will be upgraded)(ScriptableObjects' Font variables will be upgraded). If an Object wasn't added to that list but it had references to the upgraded components, those references will be lost
  • To determine which Unity Fonts will be upgraded to which TextMesh Pro FontAssets, use the Font Upgrades list
  • Hit START and then follow the presented instructions. To summarize:
    • Step 1/3: Upgrading Scripts: Decide which scripts should be upgraded; e.g. Text variables in those scripts will become TMP_Text variables
    • Step 2/3: Upgrading Components: Decide which prefabs/Scenes should be upgraded; e.g. Text components in those prefabs/Scenes will be upgraded to TextMeshProUGUI components
    • Step 3/3: Reconnecting References: Decide whether or not the references to the upgraded components should be restored; e.g. if a public variable was referencing an upgraded Text component, it will now reference the corresponding TextMeshProUGUI component

KNOWN LIMITATIONS

  • If an InputField or Dropdown prefab instance's UnityEvent is modified (i.e. it's different from the prefab asset), that modification can't be restored after the upgrade process on Unity 2019.2 or earlier if a script was modified during the upgrade process

EXAMPLES

Add Plugins/RuntimeInspector folder to the Assets & Scenes To Upgrade list and then upgrade everything (scripts, components, references). No manual intervention necessary.

Add Plugins/IngameDebugConsole folder to the Assets & Scenes To Upgrade list and then upgrade everything. No manual intervention necessary.

Upgrading DynamicPanels

Add Plugins/DynamicPanels folder to the Assets & Scenes To Upgrade list and then upgrade everything. While upgrading the scripts, DynamicPanelsCanvasEditor.cs will throw a compiler error. Fix it as follows before proceeding to upgrade the components:

anchoredPanelGUIStyle = new GUIStyle( "box" )
{
	alignment = TextAnchor.MiddleCenter, // <-- Changed here to TextAnchor.MiddleCenter
	clipping = TextClipping.Clip
};

What happened: While UI system uses TextAnchor, TMP uses TextAlignmentOptions. The plugin assumed that it was upgrading a Text's alignment property here but it was actually breaking a GUIStyle's alignment property. While upgrading the scripts, the plugin doesn't use advanced stuff like lexers in order to avoid any 3rd party dependencies. However, this can result in rare false positives like this.

Add Plugins/SimpleFileBrowser folder to the Assets & Scenes To Upgrade list and then upgrade everything. While upgrading the scripts, FileBrowser.cs will throw a compiler error. Fix it as follows before proceeding to upgrade the components:

// Credit: https://forum.unity.com/threads/calculate-width-of-a-text-before-without-assigning-it-to-a-tmp-object.758867/#post-5057900
private int CalculateLengthOfDropdownText( string str )
{
	float totalLength = 0;

	TMPro.TMP_FontAsset myFont = filterItemTemplate.font;

	float pointSizeScale = filterItemTemplate.fontSize / ( myFont.faceInfo.pointSize * myFont.faceInfo.scale );
	float emScale = filterItemTemplate.fontSize * 0.01f;

	float styleSpacingAdjustment = ( filterItemTemplate.fontStyle & TMPro.FontStyles.Bold ) == TMPro.FontStyles.Bold ? myFont.boldSpacing : 0;
	float normalSpacingAdjustment = myFont.normalSpacingOffset;

	for( int i = 0; i < str.Length; i++ )
	{
		TMPro.TMP_Character character;
		if( filterItemTemplate.font.characterLookupTable.TryGetValue( str[i], out character ) )
			totalLength += character.glyph.metrics.horizontalAdvance * pointSizeScale + ( styleSpacingAdjustment + normalSpacingAdjustment ) * emScale;
		else
			totalLength += 5;
	}

	return (int) totalLength + 10;
}

What happened: Previously, we were using Font.RequestCharactersInTexture and Font.GetCharacterInfo but TMP_FontAsset doesn't have these functions, so we had to upgrade this function manually.

Upgrading ImageCropper

Add Plugins/ImageCropper folder to the Assets & Scenes To Upgrade list and then upgrade everything. While upgrading the scripts, FontSizeSynchronizer.cs will throw a compiler error. Fix it as follows before proceeding to upgrade the components:

private void Awake()
{
	if( texts.Length == 0 )
		return;

	canvas = texts[0].canvas;

	initialBestFitSizes = new int[texts.Length];
	for( int i = 0; i < texts.Length; i++ )
		initialBestFitSizes[i] = (int) texts[i].fontSizeMax; // <-- Added (int) typecast here
}

// Credit: https://forum.unity.com/threads/textmeshpro-precull-dorebuilds-performance.762968/#post-5083490
public void Synchronize()
{
	if( canvas == null || !gameObject.activeInHierarchy )
		return;

	int minSize = int.MaxValue;
	for( int i = 0; i < texts.Length; i++ )
	{
		TMPro.TMP_Text text = texts[i];

		text.fontSizeMax = initialBestFitSizes[i];
		text.enableAutoSizing = true;
		text.ForceMeshUpdate();

		int fontSize = (int) text.fontSize;
		if( fontSize < minSize )
			minSize = fontSize;
	}

	for( int i = 0; i < texts.Length; i++ )
	{
		texts[i].fontSize = minSize;
		texts[i].enableAutoSizing = false;
	}
}

What happened: Previously, we were using TextGenerator to calculate the Best Fit font size but TextMesh Pro doesn't use TextGenerator, so we had to upgrade this function manually.

unitytexttotextmeshproupgradetool's People

Contributors

yasirkula avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.