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?
2 Likes
Any proper solution for this yet?
EDIT:
I just used this and it works fine:
passText.textComponent.SetAllDirty();
4 Likes
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();
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();
}
7093060–844852–VisibilityPasswordManager.cs (854 Bytes)
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:
InputField.ForceLabelUpdate()
You would want to use this after changing the Content Type, i.e.
inputField.contentType = InputField.ContentType.Password;
inputField.ForceLabelUpdate();
An example of the full implementation of the Method could therefore be:
[SerializeField] InputField inputField;
void TogglePassword() {
if (inputField.contentType == InputField.ContentType.Password) {
inputField.contentType = InputField.ContentType.Standard;
} else {
inputField.contentType = InputField.ContentType.Password;
}
inputField.ForceLabelUpdate();
}
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
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 ^^
8 Likes
It worked for me thanks for posting this!