Search Unity

TextMesh Pro Changing InputField ContentType via script does not update text

Discussion in 'UGUI & TextMesh Pro' started by RhinocerosGamesProduction, Jul 21, 2020.

  1. RhinocerosGamesProduction

    RhinocerosGamesProduction

    Joined:
    Dec 19, 2017
    Posts:
    4
    Hey guys I came across an interesting problem with TMPro Input fields and their content type. I created a "register profile" screen where the user is prompted to enter and verify a password (content type for input field is therefore set to "password"). Additionally I added a button next to the input fields that is supposed to hide/unhide the password. I wrote a simple script that has both inputfields referenced and on the button click, this script changes the content types to default for both fields. However, the passwords are only unhidden when I click on them (which updates this input field I assume). I also managed to create a hacky solution where the script selects the fields after changing the content type, which worked, but is just not acceptable for mobile plattform since each time you select a input field, the mobile keyboard opens itself which is of course annoying if you just want to hide/unhide your password.

    TL;DR: Changing content type for TMPro input fields via script does not automatically update the text that is in the input field, e.g. hide/unhide password

    Any ideas?
     
    ShantiB95 and AlanMattano like this.
  2. ShantiB95

    ShantiB95

    Joined:
    Feb 8, 2017
    Posts:
    17
    Any proper solution for this yet?

    EDIT:

    I just used this and it works fine:

    Code (CSharp):
    1. passText.textComponent.SetAllDirty();
     
    Last edited: Mar 3, 2021
  3. nugiesatriya

    nugiesatriya

    Joined:
    Sep 13, 2020
    Posts:
    1
    this my script for visibility password on textmeshpro, you can copy this code or just download it

    //put this script on button, and put the button on the child of TMP_InputField password
    public Button visibilityPassword;
    public TMP_InputField password;
    private void Awake()
    {
    visibilityPassword = GetComponent<Button>();
    password = GetComponentInParent<TMP_InputField>();
    visibilityPassword.onClick.AddListener(ShowHidePassword);
    }

    private void ShowHidePassword()
    {
    if (password.contentType == TMP_InputField.ContentType.Standard)
    password.contentType = TMP_InputField.ContentType.Password;
    else
    password.contentType = TMP_InputField.ContentType.Standard;

    password.Select();
    }
     

    Attached Files:

  4. SweatIRL

    SweatIRL

    Joined:
    Jun 12, 2021
    Posts:
    1
    Since there is still no viable answer here, I will attempt to answer the question swiftly:

    All you need to do in order to "Update" the Input Field is to use the following Method:
    Code (CSharp):
    1. InputField.ForceLabelUpdate()
    You would want to use this after changing the Content Type, i.e.
    Code (CSharp):
    1. inputField.contentType = InputField.ContentType.Password;
    2. inputField.ForceLabelUpdate();
    An example of the full implementation of the Method could therefore be:
    Code (CSharp):
    1. [SerializeField] InputField inputField;
    2.  
    3. void TogglePassword() {
    4.     if (inputField.contentType == InputField.ContentType.Password) {
    5.         inputField.contentType = InputField.ContentType.Standard;
    6.     } else {
    7.         inputField.contentType = InputField.ContentType.Password;
    8.     }
    9.     inputField.ForceLabelUpdate();
    10. }
    Using my implementation, you would of course have to drag the InputField onto the Script from within the Editor.
    Another way you could do it is by using
    Code (CSharp):
    1. GetComponent<>();
    But that way, the Script would have to be on the same GameObject. You could also get the Parent or Child and such, but a lot of the time I will just prioritize dragging and dropping using the Editor as it could be much less of a hassle.

    Hope this helped ^^
     
  5. tugrulsubekci

    tugrulsubekci

    Joined:
    May 30, 2022
    Posts:
    5
    it worked thanks!
     
  6. Harshid123

    Harshid123

    Joined:
    Jan 27, 2023
    Posts:
    3
    It worked for me thanks for posting this!