TextMeshPro problem with TextAlignmentOptions

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

            //This is text entity
            Niveleta.Text_3D Text3D = Niveleta.ListaTekst[i];

            //TextMesh Pro Implementation
            GameObject go = new GameObject();

            //Position
            go.transform.position = new Vector3(Text3D.txt_x, Text3D.txt_z, Text3D.txt_y);
            //Transformation
            go.transform.Rotate(new Vector3(1, 0, 0), 90);
            go.transform.Rotate(new Vector3(0, 0, 1), Text3D.txt_angle * 180 / Mathf.PI);
            //go.renderer.castShadows = false;
            //go.renderer.receiveShadows = false;

            //Text
            TextMeshPro textMeshPro = go.AddComponent<TextMeshPro>();

            //Properties
            textMeshPro.richText = true;
            textMeshPro.enableWordWrapping = false;
            textMeshPro.enableKerning = false;
            textMeshPro.fontSize = Text3D.txt_height * 14.4f;
            textMeshPro.SetText(Text3D.txt_name);
            [B]textMeshPro.alignment = GetAlignment(Text3D.Align); //Whatever I put here nothing changes.[/B]

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.

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.

public enum TextAlignmentOptions
    {
        TopLeft = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Top,
        Top = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Top,
        TopRight = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Top,
        TopJustified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Top,
        TopFlush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Top,
        TopGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Top,
        Left = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Middle,
        Center = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Middle,
        Right = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Middle,
        Justified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Middle,
        Flush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Middle,
        CenterGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Middle,
        BottomLeft = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Bottom,
        Bottom = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Bottom,
        BottomRight = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Bottom,
        BottomJustified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Bottom,
        BottomFlush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Bottom,
        BottomGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Bottom,
        BaselineLeft = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Baseline,
        Baseline = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Baseline,
        BaselineRight = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Baseline,
        BaselineJustified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Baseline,
        BaselineFlush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Baseline,
        BaselineGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Baseline,
        MidlineLeft = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Geometry,
        Midline = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Geometry,
        MidlineRight = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Geometry,
        MidlineJustified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Geometry,
        MidlineFlush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Geometry,
        MidlineGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Geometry,
        CaplineLeft = HorizontalAlignmentOptions.Left | VerticalAlignmentOptions.Capline,
        Capline = HorizontalAlignmentOptions.Center | VerticalAlignmentOptions.Capline,
        CaplineRight = HorizontalAlignmentOptions.Right | VerticalAlignmentOptions.Capline,
        CaplineJustified = HorizontalAlignmentOptions.Justified | VerticalAlignmentOptions.Capline,
        CaplineFlush = HorizontalAlignmentOptions.Flush | VerticalAlignmentOptions.Capline,
        CaplineGeoAligned = HorizontalAlignmentOptions.Geometry | VerticalAlignmentOptions.Capline,
        Converted = 0xFFFF
    };
    /// <summary>
    /// Horizontal text alignment options.
    /// </summary>
    public enum HorizontalAlignmentOptions
    {
        Left = 0x1, Center = 0x2, Right = 0x4, Justified = 0x8, Flush = 0x10, Geometry = 0x20
    }
    /// <summary>
    /// Vertical text alignment options.
    /// </summary>
    public enum VerticalAlignmentOptions
    {
        Top = 0x100, Middle = 0x200, Bottom = 0x400, Baseline = 0x800, Geometry = 0x1000, Capline = 0x2000,
    }

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.

1 Like

This is return function:

    //Ovdje dobijam aligment
    private TextAlignmentOptions GetAlignment(int Alignment)
    {
        switch (Alignment)
        {
            case 6:
                return TextAlignmentOptions.TopRight;
            case 7:
                return TextAlignmentOptions.TopJustified;
            case 8:
                return TextAlignmentOptions.TopLeft;
            case 3:
                return TextAlignmentOptions.MidlineRight;
            case 4:
                return TextAlignmentOptions.Midline;
            case 5:
                return TextAlignmentOptions.Right;
            case 0:
                return TextAlignmentOptions.BottomRight;
            case 1:
                return TextAlignmentOptions.Bottom;
            case 2:
                return TextAlignmentOptions.BottomLeft;
            case 11:
                return TextAlignmentOptions.MidlineJustified;
            case 12:
                return TextAlignmentOptions.MidlineGeoAligned;
        }

        return TextAlignmentOptions.Midline;

        //public const int LC_TA_LEFBOT = 0;
        //public const int LC_TA_CENBOT = 1;
        //public const int LC_TA_RIGBOT = 2;
        //public const int LC_TA_LEFCEN = 3;
        //public const int LC_TA_CENTER = 4;
        //public const int LC_TA_RIGCEN = 5;
        //public const int LC_TA_LEFTOP = 6;
        //public const int LC_TA_CENTOP = 7;
        //public const int LC_TA_RIGTOP = 8;
        //public const int LC_TA_ALIGNED = 11;
        //public const int LC_TA_FIT = 12;
    }

Thank you on help

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.

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…

Create a new scene and add a new Empty GameObject to it.

Add the following script to this GameObject.

using UnityEngine;

/// Include the name space for TextMesh Pro
using TMPro;

public class SandBox_03 : MonoBehaviour
{
    private void Awake()
    {
        // Add new text component to GameObject
        TMP_Text textComponent = gameObject.AddComponent<TextMeshPro>();

        // Set the text
        textComponent.text = "ABC";

        // Set Alignment
        textComponent.alignment = TextAlignmentOptions.BottomRight;
    }
}

Let me know if this works as expected.

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.

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.

SerializedObject st = new SerializedObject(tpro);
SerializedProperty aec = st.FindProperty("m_isAlignmentEnumConverted");
aec .boolValue = true;
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.

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.