Search Unity

TextMesh Pro Accessing TMP_InputField programmatically causes RectTrans to shift (2017.3.1)

Discussion in 'UGUI & TextMesh Pro' started by Sun-Dog, Mar 14, 2018.

  1. Sun-Dog

    Sun-Dog

    Joined:
    Mar 23, 2009
    Posts:
    144
    I am using 2017.3.1f1 and I have a very strange issue with the TMP_InputField.

    I have in integer validated input field used to choose the number of items in a "stack" in a split inventory item.

    I have the standard input field for the user to input data. I also have two buttons that increment and decrement that value.

    When taking manual user input, everything behaves as expected.

    When changing the value of the field programmatically using the buttons, the RectTransform shifts.

    These are the normal and then the shifted image. The image shifts when I use the buttons to access the Text Field.

    NormalElement.png ShiftedElement.png

    These are the respective inspector grabs. It's easy to see that the top and bottom shift from 2.5/5.0 to 3.75/3.75.

    NormalInspector.png
    ShiftedInspector.png

    And I can't figure out why.

    My code is as follows:
    Code (csharp):
    1.  
    2.  
    3.     public TMP_InputField desiredStackCount;
    4.  
    5.     // some other code
    6.  
    7.     public void IncrementStack(bool increment)
    8.     {
    9.         int currentStackCount = 0;
    10.         if (System.Int32.TryParse(desiredStackCount.text, out currentStackCount))
    11.         {
    12.             if (increment)
    13.             {
    14.                 if (currentStackCount >= stackToSplit.stackCount - 1)
    15.                 {
    16.                     currentStackCount = stackToSplit.stackCount - 1;
    17.                 }
    18.                 else
    19.                 {
    20.                     currentStackCount++;
    21.                 }
    22.             }
    23.             else
    24.             {
    25.                 if (currentStackCount <= 1)
    26.                 {
    27.                     currentStackCount = 1;
    28.                 }
    29.                 else
    30.                 {
    31.                     currentStackCount--;
    32.                 }
    33.             }
    34.  
    35.            desiredStackCount.text =  currentStackCount.ToString();
    36.         }
    37.     }
    38.  
    Can anyone see why, or know why, this Text field shifts?

    Also - if I then enter text as input, the RectTransform stays shifted until I hit Enter, and then it shifts back to its original values: 2.5/5.0.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Can you submit a bug report which includes the project or relevant stuff to allow me to reproduce the behavior? If you do please provide me with the case number?

    This always makes it so much quicker for me to reproduce and identify the potential issue and provide a solution for it.
     
  3. Sun-Dog

    Sun-Dog

    Joined:
    Mar 23, 2009
    Posts:
    144
    I ended up rebuilding the Input Field from scratch and this may or may not be a "bug" per se.

    What I did notice was that the margins had slipped a little and were different between the placeholder and text fields... in the end this seemed to be an interaction between all of the many different elements that make up Input element and when I rebuilt it and made sure that all of the components matched exactly, the issue went away.

    If you feel there is need for more data, I can try to get you an example.
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The setup of those margins is key and would most certainly result in the type of shift you describe.