Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Changing TMP_InputField text via script

Discussion in 'UGUI & TextMesh Pro' started by menderbug, Jul 5, 2019.

  1. menderbug

    menderbug

    Joined:
    May 14, 2018
    Posts:
    26
    I'm having a weird issue with the TMP InputField. If I change the text via script, e.g.:

    Code (CSharp):
    1. nameField.text = "New Solution";
    Then the text gets shifted left a bit:



    As soon as I click the input field it seems to recalculate the position and the positioning gets fixed. Is there some method I need to call to force this recalculation?
     
  2. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Is this with the latest releases of TMP which are version 1.4.1 for Unity 2018.3 / 2018.4 and 2.0.1 for Unity 2019.x?
     
  3. menderbug

    menderbug

    Joined:
    May 14, 2018
    Posts:
    26
    Yeah, this is on 2.0.1 for Unity 2019.1.8f1.
     
  4. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    Just tested on my end and it appears to be working fine.

    See if you get the same results creating a new TMP_InputField without modifying any of the positions / offsets of the child objects. Perhaps in your example, you modified these values which results in the incorrect positioning?
     
  5. menderbug

    menderbug

    Joined:
    May 14, 2018
    Posts:
    26
    Okay yeah it doesn't happen with a fresh TMP_InputField, but once I change the anchors of the input field itself (not the children) the problem shows up. In particular, I'm using MinX = 0, MaxX = MinY = MaxY = 1.

    That said, I haven't been able to reproduce it in an empty project yet, so I'll keep fiddling around.

    (In the empty project I noticed another issue though: the Text child of the InputField was using the default TMP font settings, which made it way too big for the input field. This only happened on the first input field though, which triggered the import of TMP Essentials.)
     
    Last edited: Jul 9, 2019
    JDeyby likes this.
  6. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    6,588
    I have noticed weirdness when creating / using a TMP object that was created prior to importing the TMP Essential Resources. When I have time, I need to track this down.

    For the time being make sure you import the TMP Essential Resources before hand.
     
  7. Jorhoto

    Jorhoto

    Joined:
    May 7, 2019
    Posts:
    99
    I'm having a similar issue in 2019.3 version doing:

    public class LoginFillEmail : MonoBehaviour
    {
    TMP_InputField inputFieldEmail;
    void Start()
    {
    inputFieldEmail = GetComponent<TMP_InputField>();
    inputFieldEmail.textComponent.SetText("a@b.com");
    inputFieldEmail.textComponent.SetAllDirty();
    inputFieldEmail.placeholder.SetAllDirty();
    }
    }

    After doing this I obtain the attached pic.
    Also the event "On End Edit" is not triggered.
    If I Click in the field, the current text is cleared.

    Hope this helps, Thanks ;)
     

    Attached Files:

  8. Babster

    Babster

    Joined:
    Jun 7, 2020
    Posts:
    20
    the next tow lines of code works only together...
    inputField.text = "some text";
    inputField.textComponent.SetText("some text");