Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

TextMeshPro problem with TextAlignmentOptions

Discussion in 'UGUI & TextMesh Pro' started by unity_QX6KJ3XmfDXG1A, Sep 25, 2019.

  1. unity_QX6KJ3XmfDXG1A

    unity_QX6KJ3XmfDXG1A

    Joined:
    Aug 30, 2018
    Posts:
    7
    Hello,

    I am trying to set Alignement to TextMeshPro component and nothing happens. Whatever I put for alignment text always has midle center alignment.

    Code

    Code (CSharp):
    1.             //This is text entity
    2.             Niveleta.Text_3D Text3D = Niveleta.ListaTekst[i];
    3.  
    4.             //TextMesh Pro Implementation
    5.             GameObject go = new GameObject();
    6.  
    7.             //Position
    8.             go.transform.position = new Vector3(Text3D.txt_x, Text3D.txt_z, Text3D.txt_y);
    9.             //Transformation
    10.             go.transform.Rotate(new Vector3(1, 0, 0), 90);
    11.             go.transform.Rotate(new Vector3(0, 0, 1), Text3D.txt_angle * 180 / Mathf.PI);
    12.             //go.renderer.castShadows = false;
    13.             //go.renderer.receiveShadows = false;
    14.  
    15.             //Text
    16.             TextMeshPro textMeshPro = go.AddComponent<TextMeshPro>();
    17.  
    18.             //Properties
    19.             textMeshPro.richText = true;
    20.             textMeshPro.enableWordWrapping = false;
    21.             textMeshPro.enableKerning = false;
    22.             textMeshPro.fontSize = Text3D.txt_height * 14.4f;
    23.             textMeshPro.SetText(Text3D.txt_name);
    24.             [B]textMeshPro.alignment = GetAlignment(Text3D.Align); //Whatever I put here nothing changes.[/B]
    25.  
    26.  
    Here is screenshot of 3 texts to which is applied different alignment but always are drawn as middle center alignment. If alignment is good than text should fit inside red box.
     

    Attached Files:

    Last edited: Sep 25, 2019
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Most likely the return value from your GetAlignment(Text3D.Align); function do not match valid values for the TMP text component which are as follows.

    Code (csharp):
    1.  
    2. public enum TextAlignmentOptions
    3.     {
    4.         TopLeft = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Top,
    5.         Top = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Top,
    6.         TopRight = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Top,
    7.         TopJustified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Top,
    8.         TopFlush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Top,
    9.         TopGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Top,
    10.         Left = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Middle,
    11.         Center = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Middle,
    12.         Right = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Middle,
    13.         Justified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Middle,
    14.         Flush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Middle,
    15.         CenterGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Middle,
    16.         BottomLeft = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Bottom,
    17.         Bottom = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Bottom,
    18.         BottomRight = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Bottom,
    19.         BottomJustified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Bottom,
    20.         BottomFlush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Bottom,
    21.         BottomGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Bottom,
    22.         BaselineLeft = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Baseline,
    23.         Baseline = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Baseline,
    24.         BaselineRight = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Baseline,
    25.         BaselineJustified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Baseline,
    26.         BaselineFlush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Baseline,
    27.         BaselineGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Baseline,
    28.         MidlineLeft = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Geometry,
    29.         Midline = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Geometry,
    30.         MidlineRight = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Geometry,
    31.         MidlineJustified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Geometry,
    32.         MidlineFlush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Geometry,
    33.         MidlineGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Geometry,
    34.         CaplineLeft = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Capline,
    35.         Capline = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Capline,
    36.         CaplineRight = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Capline,
    37.         CaplineJustified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Capline,
    38.         CaplineFlush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Capline,
    39.         CaplineGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Capline,
    40.         Converted = 0xFFFF
    41.     };
    42.     /// <summary>
    43.     /// Horizontal text alignment options.
    44.     /// </summary>
    45.     public enum HorizontalAlignmentOptions
    46.     {
    47.         Left = 0x1, Center = 0x2, Right = 0x4, Justified = 0x8, Flush = 0x10, Geometry = 0x20
    48.     }
    49.     /// <summary>
    50.     /// Vertical text alignment options.
    51.     /// </summary>
    52.     public enum VerticalAlignmentOptions
    53.     {
    54.         Top = 0x100, Middle = 0x200, Bottom = 0x400, Baseline = 0x800, Geometry = 0x1000, Capline = 0x2000,
    55.     }
    56.  
    57.  
    It appears you are mixing alignment values of this Text_3D type with the alignment values that TMP uses as listed above. You will need to do some conversion between these.
     
    KimberleyC likes this.
  3. unity_QX6KJ3XmfDXG1A

    unity_QX6KJ3XmfDXG1A

    Joined:
    Aug 30, 2018
    Posts:
    7
    This is return function:

    Code (CSharp):
    1.     //Ovdje dobijam aligment
    2.     private TextAlignmentOptions GetAlignment(int Alignment)
    3.     {
    4.         switch (Alignment)
    5.         {
    6.             case 6:
    7.                 return TextAlignmentOptions.TopRight;
    8.             case 7:
    9.                 return TextAlignmentOptions.TopJustified;
    10.             case 8:
    11.                 return TextAlignmentOptions.TopLeft;
    12.             case 3:
    13.                 return TextAlignmentOptions.MidlineRight;
    14.             case 4:
    15.                 return TextAlignmentOptions.Midline;
    16.             case 5:
    17.                 return TextAlignmentOptions.Right;
    18.             case 0:
    19.                 return TextAlignmentOptions.BottomRight;
    20.             case 1:
    21.                 return TextAlignmentOptions.Bottom;
    22.             case 2:
    23.                 return TextAlignmentOptions.BottomLeft;
    24.             case 11:
    25.                 return TextAlignmentOptions.MidlineJustified;
    26.             case 12:
    27.                 return TextAlignmentOptions.MidlineGeoAligned;
    28.         }
    29.  
    30.         return TextAlignmentOptions.Midline;
    31.  
    32.         //public const int LC_TA_LEFBOT = 0;
    33.         //public const int LC_TA_CENBOT = 1;
    34.         //public const int LC_TA_RIGBOT = 2;
    35.         //public const int LC_TA_LEFCEN = 3;
    36.         //public const int LC_TA_CENTER = 4;
    37.         //public const int LC_TA_RIGCEN = 5;
    38.         //public const int LC_TA_LEFTOP = 6;
    39.         //public const int LC_TA_CENTOP = 7;
    40.         //public const int LC_TA_RIGTOP = 8;
    41.         //public const int LC_TA_ALIGNED = 11;
    42.         //public const int LC_TA_FIT = 12;
    43.     }
    Thank you on help
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Make sure the return from that function is the correct value for TMP.

    To simplify the troubleshooting, just assign something like textMeshPro.alignment = TextAlignmentOptions.TopLeft; to see if that works.
     
  5. unity_QX6KJ3XmfDXG1A

    unity_QX6KJ3XmfDXG1A

    Joined:
    Aug 30, 2018
    Posts:
    7
    Actually I already did it and find out it doesn't accept any Alignment except Midle- probably default value (MiddleCenter). Very strange, and illogical to me. Because Alignmentproperty accept set and get value...
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Create a new scene and add a new Empty GameObject to it.

    Add the following script to this GameObject.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. /// Include the name space for TextMesh Pro
    5. using TMPro;
    6.  
    7. public class SandBox_03 : MonoBehaviour
    8. {
    9.     private void Awake()
    10.     {
    11.         // Add new text component to GameObject
    12.         TMP_Text textComponent = gameObject.AddComponent<TextMeshPro>();
    13.  
    14.         // Set the text
    15.         textComponent.text = "ABC";
    16.  
    17.         // Set Alignment
    18.         textComponent.alignment = TextAlignmentOptions.BottomRight;
    19.     }
    20. }
    21.  
    Let me know if this works as expected.
     
  7. unity_QX6KJ3XmfDXG1A

    unity_QX6KJ3XmfDXG1A

    Joined:
    Aug 30, 2018
    Posts:
    7
    I will try later. Thank you. But I solved problem on other way. I can get center point for every text and its angle. So I can draw text with middle alignment and apply rotation angle. Works perfect, not need alignment.
     
  8. Hyperg

    Hyperg

    Joined:
    Jul 6, 2015
    Posts:
    19
    Hello,
    I was working on a small conversion tool for some parts of our UI when I've stumbled upon a similar issue:
    Setting the TextMeshProUGUI component's alignment property does apply the TextAlignmentOptions enum value (as observed in the debugger), but the value does not carry to serialization. Loading the converted prefab yields the default TopLeft alignment value.

    I've inspected the TMP_Text part of that component with the inspector in Debug mode and noticed a field called IsAlignmentEnumConverted, which raised my eyebrows a bit, and indeed, after modifying that through a SerializedObject session, the alignment was being saved correctly.

    Code (CSharp):
    1.  
    2. SerializedObject st = new SerializedObject(tpro);
    3. SerializedProperty aec = st.FindProperty("m_isAlignmentEnumConverted");
    4. aec .boolValue = true;
    5. st.ApplyModifiedProperties();
    So whatever that Alignment property is doing, does not work correctly with the conversion field.

    [EDIT] We're working with Unity 2017.4.25f1 and using the TextMeshPro 1.2.2 from AssetStore.
     
  9. unity_EA0E0969A8491B6069AF

    unity_EA0E0969A8491B6069AF

    Joined:
    Aug 27, 2021
    Posts:
    3
    Hi. I faced the same problem in runtime. The reason is that Graphcs.SetVerticesDirty() does not work if object is not active and enabled. So you should change text settings after OnEnable.