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

TextMesh Pro TMPro Backspace Handling Incorrect With Offset

Discussion in 'UGUI & TextMesh Pro' started by looytroop, Jan 15, 2021.

  1. looytroop

    looytroop

    Joined:
    Jun 22, 2017
    Posts:
    69
    Hello, @Stephan_B

    I am having an issue where I have a TMPro Input field with a minOffset and maxOffset where left is set to 180, and right is set to 0. I am having an issue where when pressing the backspace key, it gets offset dramatically (by -180) so that it becomes left = 0, and right = 90.

    I seem to have tracked down this bug to the TMP_InputField.cs script from lines 3866. I can see that it is detecting where the first character and last character are, and then it is using these values against the anchored position to determine if it should make an offset. I am not sure how to fix this bug properly, so I thought you may have some ideas, also I think it is good for you to know about this issue.

    The code is here for convenience:
    Code (CSharp):
    1. // Special handling of backspace
    2. if (m_isLastKeyBackspace)
    3. {
    4.     float anchoredPositionX = m_TextComponent.rectTransform.anchoredPosition.x;
    5.  
    6.     float firstCharPosition = localPosition.x + textViewportLocalPosition.x + textComponentLocalPosition.x + m_TextComponent.textInfo.characterInfo[0].origin - m_TextComponent.margin.x;
    7.     float lastCharPosition = localPosition.x + textViewportLocalPosition.x + textComponentLocalPosition.x + m_TextComponent.textInfo.characterInfo[m_TextComponent.textInfo.characterCount - 1].origin + m_TextComponent.margin.z + m_CaretWidth;
    8.  
    9.     if (anchoredPositionX > 0.0001f && firstCharPosition > viewportWSRect.xMin)
    10.     {
    11.         float offset = viewportWSRect.xMin - firstCharPosition;
    12.  
    13.         if (anchoredPositionX < -offset)
    14.             offset = -anchoredPositionX;
    15.  
    16.         m_TextComponent.rectTransform.anchoredPosition += new Vector2(offset, 0);
    17.         AssignPositioningIfNeeded();
    18.     }
    19.     else if (anchoredPositionX < -0.0001f && lastCharPosition < viewportWSRect.xMax)
    20.     {
    21.         float offset = viewportWSRect.xMax - lastCharPosition;
    22.  
    23.         if (-anchoredPositionX < offset)
    24.             offset = -anchoredPositionX;
    25.  
    26.         m_TextComponent.rectTransform.anchoredPosition += new Vector2(offset, 0);
    27.         AssignPositioningIfNeeded();
    28.     }
    29.  
    30.     m_isLastKeyBackspace = false;
    31. }

    Please let me know what you think.
     

    Attached Files:

    Last edited: Feb 3, 2021
  2. looytroop

    looytroop

    Joined:
    Jun 22, 2017
    Posts:
    69
    I seem to have fixed the bug by updating the firstCharacter position computation. I just added in the factor of the minOffset. Please let me know if this will actually cause other issues and if this is not the way to solve this issue.

    OLD:
     float firstCharPosition = localPosition.x + textViewportLocalPosition.x + textComponentLocalPosition.x + m_TextComponent.textInfo.characterInfo[0].origin - m_TextComponent.margin.x;


    NEW:
     float firstCharPosition = localPosition.x + textViewportLocalPosition.x + textComponentLocalPosition.x + m_TextComponent.textInfo.characterInfo[0].origin - m_TextComponent.margin.x - m_TextComponent.rectTransform.offsetMin.x;