Search Unity

TextMesh Pro Replacing Text with TextMesh Pro

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by newcoder17, Feb 3, 2018.

  1. newcoder17

    newcoder17

    Joined:
    Dec 9, 2017
    Posts:
    103
    I have a page of text that I have quite happily scrolling up and down.
    The layout in the hierarchy is:
    Canvas
    >Panel
    >>Image - not sure how I ended up with this but it has a Scroll Rect on it
    >>>GameObject - with just a rect transform
    >>>>Text - with rect transform, Text, Content Size Fitter, Mesh Renderer

    All I want to do is replace the text with textmesh pro, purely do I can use tags for formatting headings etc and also allowing links within the text.

    I've tried adding text mesh to the Text object above but that didn't show anything.
    I've then added another text mesh object under GameObject and that again doesn't show anything.

    I've tried downloading a font and using that instead of arial, changing colours etc but it shows nothing.
    I've checked colours, font size etc.

    Daft as it sounds the object even seems invisible in my scene too.
    Am I doing something wrong?
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,066
    If you are using UI.Text objects (Create - UI - Text), replace those by the TextMeshPro - Text for the UI which is located in "Create - UI - TextMeshPro Text".
     
  3. newcoder17

    newcoder17

    Joined:
    Dec 9, 2017
    Posts:
    103
    I dont seem to have that option.
    From the dropdown I just have UI > Text
    Under 3D Object I have 3D text
    If I go to Add Component on a gameobject and search for text all I see is Text, Text Mesh and GUI text

    Am I missing something?

    Edit: OK, now I realise I have to add it in asset store. I thought it was integral to Unity now. I have it now.
    Thanks
     
  4. newcoder17

    newcoder17

    Joined:
    Dec 9, 2017
    Posts:
    103
    Thanks
    I still dont quite know how to get links to work.
    I've looked at the video tutorial, but when I add the text there is no link.
    eg <link="name">Click Here</link>

    On top of that, I dont undertand how to send it elsewhere in the text?

    I basically want to have a menu, so when you click on it you get taken to whatever part of the document.
    Is this possible?
     
  5. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    3,066
    The <link> tag simply provides a way to define a portion / block of text that you want to track and possibly interact with. The <link> tag doesn't add any logic or visual treatment as users might want to do different things based on interacting with that block of text.

    Take a look at Example 12 and 12a included with TextMesh Pro. More specifically take a look at how example 12a handles interactions with different portions of the text including characters, words, lines and links. Look at the event handlers and how those work.
     
  6. danielheadbang

    danielheadbang

    Joined:
    Mar 24, 2015
    Posts:
    34
    Hello, I'm having an issue that seems to be related to text update with textmeshpro, I have a text with a link, and hovering it prints me the link, as specified, but when I change the text, it no longer seems to trigger the link events, am I doing something wrong?

    I made the change on the 12a example and it seemed to happen the same

    Is there a way to "update" the events so they trigger on text changes? It seemed to only happen with "onlinkselected" though

    If I can get a help on this it would be much appreciated, thanks in advance
     
  7. danielheadbang

    danielheadbang

    Joined:
    Mar 24, 2015
    Posts:
    34
    It happens on the "new" links it seems, adding a link to the text seems to trigger the old ones, but not the new ones
     
  8. danielheadbang

    danielheadbang

    Joined:
    Mar 24, 2015
    Posts:
    34
    I seem to have fixed it by "resetting" the m_selectedlink to -1 after altering the text, as that seems to be the issue, I don't know if this is the correct way to do it but it worked for me, if there's an "official" way to do it I'll be happy to know

    Regards
     
  9. davem123us

    davem123us

    Joined:
    Jul 17, 2012
    Posts:
    7
    Here is an Editor script that works:

    Setup: Project Settings > TextMeshPro > Settings > Default Font Asset (Set to the TMP font you want to use)

    Usage: 1) Select Text(s) in Hierarchy 2) Select: Tools > Text To TextMeshPro

    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 TextMeshPro", false, 4000)]
    27.     static void DoIt()
    28.     {
    29.         if(TMPro.TMP_Settings.defaultFontAsset == null)
    30.         {
    31.             EditorUtility.DisplayDialog("ERROR!", "Assign a default font asset in project settings!", "OK", "");
    32.             return;
    33.         }
    34.  
    35.         foreach(GameObject gameObject in Selection.gameObjects)
    36.         {
    37.             ConvertTextToTextMeshPro(gameObject);
    38.         }
    39.     }
    40.  
    41.     static void ConvertTextToTextMeshPro(GameObject target)
    42.     {
    43.         TextMeshProSettings settings = GetTextMeshProSettings(target);
    44.  
    45.         DestroyImmediate(target.GetComponent<Text>());
    46.  
    47.         TextMeshProUGUI tmp = target.AddComponent<TextMeshProUGUI>();
    48.         tmp.enabled = settings.Enabled;
    49.         tmp.fontStyle = settings.FontStyle;
    50.         tmp.fontSize = settings.FontSize;
    51.         tmp.fontSizeMin = settings.FontSizeMin;
    52.         tmp.fontSizeMax = settings.FontSizeMax;
    53.         tmp.lineSpacing = settings.LineSpacing;
    54.         tmp.richText = settings.EnableRichText;
    55.         tmp.enableAutoSizing = settings.EnableAutoSizing;
    56.         tmp.alignment = settings.TextAlignmentOptions;
    57.         tmp.enableWordWrapping = settings.WrappingEnabled;
    58.         tmp.overflowMode = settings.TextOverflowModes;
    59.         tmp.text = settings.Text;
    60.         tmp.color = settings.Color;
    61.         tmp.raycastTarget = settings.RayCastTarget;
    62.     }
    63.  
    64.     static TextMeshProSettings GetTextMeshProSettings(GameObject gameObject)
    65.     {
    66.         Text uiText = gameObject.GetComponent<Text>();
    67.         if (uiText == null)
    68.         {
    69.             EditorUtility.DisplayDialog("ERROR!", "You must select a Unity UI Text Object to convert.", "OK", "");
    70.             return null;
    71.         }
    72.  
    73.         return new TextMeshProSettings
    74.         {
    75.             Enabled = uiText.enabled,
    76.             FontStyle = FontStyleToFontStyles(uiText.fontStyle),
    77.             FontSize = uiText.fontSize,
    78.             FontSizeMin = uiText.resizeTextMinSize,
    79.             FontSizeMax = uiText.resizeTextMaxSize,
    80.             LineSpacing = uiText.lineSpacing,
    81.             EnableRichText = uiText.supportRichText,
    82.             EnableAutoSizing = uiText.resizeTextForBestFit,
    83.             TextAlignmentOptions = TextAnchorToTextAlignmentOptions(uiText.alignment),
    84.             WrappingEnabled = HorizontalWrapModeToBool(uiText.horizontalOverflow),
    85.             TextOverflowModes = VerticalWrapModeToTextOverflowModes(uiText.verticalOverflow),
    86.             Text = uiText.text,
    87.             Color = uiText.color,
    88.             RayCastTarget = uiText.raycastTarget
    89.         };
    90.     }
    91.  
    92.     static bool HorizontalWrapModeToBool(HorizontalWrapMode overflow)
    93.     {
    94.         return overflow == HorizontalWrapMode.Wrap;
    95.     }
    96.  
    97.     static TextOverflowModes VerticalWrapModeToTextOverflowModes(VerticalWrapMode verticalOverflow)
    98.     {
    99.         return verticalOverflow == VerticalWrapMode.Truncate ? TextOverflowModes.Truncate : TextOverflowModes.Overflow;
    100.     }
    101.  
    102.     static FontStyles FontStyleToFontStyles(FontStyle fontStyle)
    103.     {
    104.         switch (fontStyle)
    105.         {
    106.             case FontStyle.Normal:
    107.                 return FontStyles.Normal;
    108.  
    109.             case FontStyle.Bold:
    110.                 return FontStyles.Bold;
    111.  
    112.             case FontStyle.Italic:
    113.                 return  FontStyles.Italic;
    114.  
    115.             case FontStyle.BoldAndItalic:
    116.                 return FontStyles.Bold | FontStyles.Italic;
    117.         }
    118.  
    119.         Debug.LogWarning("Unhandled font style " + fontStyle);
    120.         return FontStyles.Normal;
    121.     }
    122.  
    123.     static TextAlignmentOptions TextAnchorToTextAlignmentOptions(TextAnchor textAnchor)
    124.     {
    125.         switch (textAnchor)
    126.         {
    127.             case TextAnchor.UpperLeft:
    128.                 return TextAlignmentOptions.TopLeft;
    129.  
    130.             case TextAnchor.UpperCenter:
    131.                 return TextAlignmentOptions.Top;
    132.  
    133.             case TextAnchor.UpperRight:
    134.                 return TextAlignmentOptions.TopRight;
    135.  
    136.             case TextAnchor.MiddleLeft:
    137.                 return TextAlignmentOptions.Left;
    138.  
    139.             case TextAnchor.MiddleCenter:
    140.                 return TextAlignmentOptions.Center;
    141.  
    142.             case TextAnchor.MiddleRight:
    143.                 return TextAlignmentOptions.Right;
    144.  
    145.             case TextAnchor.LowerLeft:
    146.                 return TextAlignmentOptions.BottomLeft;
    147.  
    148.             case TextAnchor.LowerCenter:
    149.                 return TextAlignmentOptions.Bottom;
    150.  
    151.             case TextAnchor.LowerRight:
    152.                 return TextAlignmentOptions.BottomRight;
    153.         }
    154.  
    155.         Debug.LogWarning("Unhandled text anchor " + textAnchor);
    156.         return TextAlignmentOptions.TopLeft;
    157.     }
    158. }