Search Unity

Textmesh Pro component changing RectTransform values when added and general impressions

Discussion in 'UGUI & TextMesh Pro' started by Nieles_GH, Jul 13, 2018.

  1. Nieles_GH

    Nieles_GH

    Joined:
    Jun 26, 2017
    Posts:
    57
    I'm currently in the process of replacing the standard Text components with the Text Mesh Pro ones and it's quite annoying that the Text Mesh Pro component changes the Rect Transform settings, this makes replacing the text components a lot more painstaking.
    Any specific reason for this?

    Some other impressions:

    Why is the default setting of Raycast Target true?
    I only want this to be true in 5% of the cases and from what I've heard it can be quite a heavy option.
    The default value of the standard Unity text raycast target was already true but in text mesh pro it's little more hidden away in the extra's options. Anyway, I would think false would be the most common use case so I would think this should be the default.

    Overall Text Mesh Pro is a cool package but I would think that if Unity adopts it in their engine they would integrate it a little better. Because even though it comes with the engine now it still feels like a separate package. If this happens more with other packages Unity will become quite messy over time.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    TextMesh Pro should not be changing the RectTransform settings. What version of Unity are you using? Can you provide more insight on how to reproduce this behavior?

    As you stated, this is enabled by default to mirror the behavior of UI.Text which also has Raycast Target enabled by default. I can certainly consider adding an options in the TMP Settings to control the default behavior of this option.

    Do you mean just from a visual standpoint? If so take a look at the 1.3.0-preview version of the TextMesh Pro package.
     
  3. Nieles_GH

    Nieles_GH

    Joined:
    Jun 26, 2017
    Posts:
    57
    I'm using Text Mesh Pro 1.2.4 in Unity 2018.2.
    It's happening every time I add the Text Mesh Pro UGUI component to a GameObject:


    And what I mean with that it feels like a different package comes from multiple things. It does indeed look a little different. But also that it's not part of the standard UnityEngine.UI namespace. And the naming to me is a little weird. TextMeshProUGUI just is a long name that I could expect from an asset store package but in the Unity engine, I would expect a shorter more straightforward name. Also how to add materials for texts with different outlines was a little unclear to me, it doesn't work as straightforward as adding an outline component to the gameobject.

    I do get where it's coming from and I also think it's good Unity works with the developers bring the great packages into Unity by default but then the package should also feel like something standard in Unity.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The ability to control the default size of a RectTransform is a feature that was requested by users a long time ago. The default size can be defined in the TMP Settings for both the <TextMeshPro> component and for the <TextMeshProUGUI> component. I could add an option to disable this feature is needed.

    BTW: I already added the ability to control the Raycast Target in the TMP Settings.
    upload_2018-7-16_14-28-44.png

    In terms of the ability to dynamically change the visual appearance of the text to add an Outline, Shadow, Glow, etc. This functionality is made possible by the use of Signed Distance Field and are material properties which are manged via Material Presets. This provides for the best flexibility and performance. By contrast the Outline and Shadow component used in the UI is super inefficient as results in increasing the geometry count by 5x to 10x. It also looks horrible and will kill performance on mobile devices. See the following old post about this.

    As I continue to work / improve TextMesh Pro and work on the new text system (a.k.a. integrated version), that the product features and workflows (where it makes sense) mirror those of Unity. At the same time, I also had to be mindful of the hundreds of thousands of TMP users and make sure I provide as smooth as possible transition / migration to the new system.
     
  5. Nieles_GH

    Nieles_GH

    Joined:
    Jun 26, 2017
    Posts:
    57
    For the default size maybe it's an option to use the default size when creating a new gameobject with a Text Mesh Pro component from the new GameObject menu. But to not use it when adding the Text Mesh Component yourself?
    But maybe I only had issues now because I was replacing all Text components with the Text Mesh Pro ones.

    I didn't know about the TextMeshPro settings, looking forward for the raycast option :)
    Overall I'm very pleased with Text Mesh Pro and it does look a lot better than the standard Unity Text!
    So keep up the good work!
     
    oana_hia and Stephan_B like this.
  6. menderbug

    menderbug

    Joined:
    May 14, 2018
    Posts:
    26
    FWIW, I'm also currently in the process of switching from built-in UI.Text components to TextMeshProUGUI and it feels like I spend the majority of the time fixing RectTransforms. An option to disable the default values, or to just ignore them when adding a component would be very helpful. :)
     
  7. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    This will be addressed in the next release where if the RectTransform has a value other than the initial value, it will retain the old value.
     
    menderbug likes this.
  8. davemeta

    davemeta

    Joined:
    Jul 17, 2012
    Posts:
    17
    Hope this helps someone with current nightmare of converting Text > TextMeshPro

    Just select the object to convert and pick from the Tools dropdown menu.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEngine.UI;
    4. using TMPro;
    5.  
    6. public class TextToTextMeshPro : Editor
    7. {
    8.     public class TextMeshProSettings
    9.     {
    10.         public bool Enabled;
    11.         public FontStyles FontStyle;
    12.         public float FontSize;
    13.         public float FontSizeMin;
    14.         public float FontSizeMax;
    15.         public float LineSpacing;
    16.         public bool EnableRichText;
    17.         public bool EnableAutoSizing;
    18.         public TextAlignmentOptions TextAlignmentOptions;
    19.         public bool WrappingEnabled;
    20.         public TextOverflowModes TextOverflowModes;
    21.         public string Text;
    22.         public Color Color;
    23.         public bool RayCastTarget;
    24.     }
    25.  
    26.     [MenuItem("Tools/Text To TMP (Single)", false)]
    27.     static void SelectionToTextMeshPro()
    28.     {
    29.         if (IsDefaultFontMissing())
    30.         {
    31.             return;
    32.         }
    33.  
    34.         foreach (GameObject gameObject in Selection.gameObjects)
    35.         {
    36.             Text text = gameObject.GetComponent<Text>();
    37.             if (text != null)
    38.             {
    39.                 ConvertTextToTextMeshPro(text);
    40.             }
    41.         }
    42.     }
    43.  
    44.     [MenuItem("Tools/Text To TMP (Recursive)", false)]
    45.     static void SelectionToTextMeshProRecursive()
    46.     {
    47.         if (IsDefaultFontMissing())
    48.         {
    49.             return;
    50.         }
    51.  
    52.         SelectionToTextMeshPro();
    53.         foreach (GameObject gameObject in Selection.gameObjects)
    54.         {
    55.             foreach(Text text in gameObject.GetComponentsInChildren<Text>())
    56.             {
    57.                 ConvertTextToTextMeshPro(text);
    58.             }
    59.         }
    60.     }
    61.  
    62.     static bool IsDefaultFontMissing()
    63.     {
    64.         if (TMPro.TMP_Settings.defaultFontAsset == null)
    65.         {
    66.             EditorUtility.DisplayDialog("Default Font Missing", "Assign a default font asset in project settings!", "Ok", "");
    67.             return true;
    68.         }
    69.         return false;
    70.     }
    71.  
    72.     static void ConvertTextToTextMeshPro(Text text)
    73.     {
    74.         GameObject parent = text.gameObject;
    75.         Vector2 sizeDelta = parent.GetComponent<RectTransform>().sizeDelta;
    76.         TextMeshProSettings settings = GetTextMeshProSettings(text);
    77.         Object.DestroyImmediate(text as Object, false);
    78.  
    79.         TextMeshProUGUI tmp = parent.AddComponent<TextMeshProUGUI>();
    80.         tmp.enabled = settings.Enabled;
    81.         tmp.fontStyle = settings.FontStyle;
    82.         tmp.fontSize = settings.FontSize;
    83.         tmp.fontSizeMin = settings.FontSizeMin;
    84.         tmp.fontSizeMax = settings.FontSizeMax;
    85.         tmp.lineSpacing = settings.LineSpacing;
    86.         tmp.richText = settings.EnableRichText;
    87.         tmp.enableAutoSizing = settings.EnableAutoSizing;
    88.         tmp.alignment = settings.TextAlignmentOptions;
    89.         tmp.enableWordWrapping = settings.WrappingEnabled;
    90.         tmp.overflowMode = settings.TextOverflowModes;
    91.         tmp.text = settings.Text;
    92.         tmp.color = settings.Color;
    93.         tmp.raycastTarget = settings.RayCastTarget;
    94.  
    95.         parent.GetComponent<RectTransform>().sizeDelta = sizeDelta;
    96.         Debug.Log("Converted Text to TextMeshPro on " + parent.name);
    97.     }
    98.  
    99.     static TextMeshProSettings GetTextMeshProSettings(Text text)
    100.     {
    101.         return new TextMeshProSettings
    102.         {
    103.             Enabled = text.enabled,
    104.             FontStyle = FontStyleToFontStyles(text.fontStyle),
    105.             FontSize = text.fontSize,
    106.             FontSizeMin = text.resizeTextMinSize,
    107.             FontSizeMax = text.resizeTextMaxSize,
    108.             LineSpacing = text.lineSpacing,
    109.             EnableRichText = text.supportRichText,
    110.             EnableAutoSizing = text.resizeTextForBestFit,
    111.             TextAlignmentOptions = TextAnchorToTextAlignmentOptions(text.alignment),
    112.             WrappingEnabled = HorizontalWrapModeToBool(text.horizontalOverflow),
    113.             TextOverflowModes = VerticalWrapModeToTextOverflowModes(text.verticalOverflow),
    114.             Text = text.text,
    115.             Color = text.color,
    116.             RayCastTarget = text.raycastTarget
    117.         };
    118.     }
    119.  
    120.     static bool HorizontalWrapModeToBool(HorizontalWrapMode overflow)
    121.     {
    122.         return overflow == HorizontalWrapMode.Wrap;
    123.     }
    124.  
    125.     static TextOverflowModes VerticalWrapModeToTextOverflowModes(VerticalWrapMode verticalOverflow)
    126.     {
    127.         return verticalOverflow == VerticalWrapMode.Truncate ? TextOverflowModes.Truncate : TextOverflowModes.Overflow;
    128.     }
    129.  
    130.     static FontStyles FontStyleToFontStyles(FontStyle fontStyle)
    131.     {
    132.         switch (fontStyle)
    133.         {
    134.             case FontStyle.Normal:
    135.                 return FontStyles.Normal;
    136.  
    137.             case FontStyle.Bold:
    138.                 return FontStyles.Bold;
    139.  
    140.             case FontStyle.Italic:
    141.                 return  FontStyles.Italic;
    142.  
    143.             case FontStyle.BoldAndItalic:
    144.                 return FontStyles.Bold | FontStyles.Italic;
    145.         }
    146.  
    147.         Debug.LogWarning("Unhandled font style " + fontStyle);
    148.         return FontStyles.Normal;
    149.     }
    150.  
    151.     static TextAlignmentOptions TextAnchorToTextAlignmentOptions(TextAnchor textAnchor)
    152.     {
    153.         switch (textAnchor)
    154.         {
    155.             case TextAnchor.UpperLeft:
    156.                 return TextAlignmentOptions.TopLeft;
    157.  
    158.             case TextAnchor.UpperCenter:
    159.                 return TextAlignmentOptions.Top;
    160.  
    161.             case TextAnchor.UpperRight:
    162.                 return TextAlignmentOptions.TopRight;
    163.  
    164.             case TextAnchor.MiddleLeft:
    165.                 return TextAlignmentOptions.Left;
    166.  
    167.             case TextAnchor.MiddleCenter:
    168.                 return TextAlignmentOptions.Center;
    169.  
    170.             case TextAnchor.MiddleRight:
    171.                 return TextAlignmentOptions.Right;
    172.  
    173.             case TextAnchor.LowerLeft:
    174.                 return TextAlignmentOptions.BottomLeft;
    175.  
    176.             case TextAnchor.LowerCenter:
    177.                 return TextAlignmentOptions.Bottom;
    178.  
    179.             case TextAnchor.LowerRight:
    180.                 return TextAlignmentOptions.BottomRight;
    181.         }
    182.  
    183.         Debug.LogWarning("Unhandled text anchor " + textAnchor);
    184.         return TextAlignmentOptions.TopLeft;
    185.     }
    186. }
     
    menderbug and Nieles_GH like this.