Change InputField input from standard to password text via script

I should know this but for some reason still have problem.

Could someone nice please show me how to change the input field text field from standard to password display via script?

change the .contentType

1 Like

.contentType ?? can you give me example for that ? i need pls

inp_Password.contentType = InputField.ContentType.Standard;
inp_Password.contentType = InputField.ContentType.Password;
1 Like
  • inp_Password.contentType ? what is this ? string type ? and

iand where can i put this

  • inp_Password.contentType = InputField.ContentType.Password; ? update ?Here’s my code where can i put that ?

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LogInScript : MonoBehaviour {
#region Variable
//Public Variables
public InputField inputUserName;
public InputField inputPassword;

#endregion

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
//Condition for Change Sign in or Log in
public void LogIn(int index)
{
if(index == 1)
{
//Load Login Scene
Application.LoadLevel(“LogIn”);
}

if(index == 2)
{
//Load SignUp Scene
Application.LoadLevel(“SignUp”);
}
}
public void PassworChange(int Password)
{
inputPassword.contentType = InputField.ContentType.Standard;
inputPassword.contentType = InputField.ContentType.Password;
}
}

Its not working by the way im using UI Canvas

well, the two lines in my post above is two ways of doing it: .standard = you will see the text an by using .password you will not se the password you type. In your code you will not see the characters, only asterisks. Start there.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class PasswordFields: MonoBehaviour {
public InputField password;

}

void Start () {
    password.contentType = InputField.ContentType.Password;

}

I think this makes it more clear. You have to drag your Input Field in Unity to the “public InputField” when you add the script to a game object

Step By Step
TMPro [u]InputField or Unity InputField for Password ContenctType Show and Hide.[/u]
1) Create Script InputFieldTest.cs and added to any empty gameobject and create simple UI.

public TMP_InputField PasswordIF;
public Image ShowPassImgIcon,HidePassImgIcon;

public void ShowPassBtnClick()
{
ShowPassImgIcon.enabled = false;
HidePassImgIcon.enabled = true;
SetPasswordContentType(PasswordIF,TMP_InputField.ContentType.Standard);
}
public void HidePassBtnClick()
{
HidePassImgIcon.enabled = false;
ShowPassImgIcon.enabled = true;
SetPasswordContentType(PasswordIF,TMP_InputField.ContentType.Password);
}
private void SetPasswordContentType(TMP_InputField tmp_if, TMP_InputField.ContentType contetTypePass)
{
tmp_if.contentType = contetTypePass;
tmp_if.DeactivateInputField();
tmp_if.ActivateInputField();
}


2) Button click method via inspector OnClick Method...call ShowPassBtnClick() ,HidePassBtnClick() 
or 
ShowPassBtn.OnClick.AddListener(()=>ShowPassBtnClick())
HidePassBtn.OnClick.AddListener(()=>HidePassBtnClick())
in Start Method.

ShowPassBtn.OnClick.RemoveListener(()=>ShowPassBtnClick())
HidePassBtn.OnClick.AddListener(()=>HidePassBtnClick()) 
in Destroy or Disable Method.

![5769538--608101--Capture12.PNG|1155x558](upload://9TgDoUrpNewcuby3YueAw5SxASs.png)
![5769538--608104--Capture13.PNG|1132x543](upload://15vII5lYvv0Ha0oQ8j2qhxmLZgn.png)
2 Likes
  • Must Use in Newer Veriosn ForceLabelUpdate in Lower version of unity use LabelUpdate

  • Below Example Code

public class ShowPassword : MonoBehaviour
{
    [SerializeField] private TMP_InputField userPassword;

    public void ShowUserPassword()
    {
        if (userPassword.contentType == TMP_InputField.ContentType.Password)
        {
            userPassword.contentType = TMP_InputField.ContentType.Standard;
        }
        else
        {
            userPassword.contentType = TMP_InputField.ContentType.Password;
        }
        userPassword.ForceLabelUpdate();
    }
}

Attatch any object and Click on button Call ShowUserPassword.

3 Likes

Thanks a lot of bro.

1 Like