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

Change InputField input from standard to password text via script

Discussion in 'UGUI & TextMesh Pro' started by pKallv, Jan 18, 2015.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    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?
     
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    change the .contentType
     
  3. NightStalker08

    NightStalker08

    Joined:
    Jan 26, 2015
    Posts:
    3
    .contentType ?? can you give me example for that ? i need pls
     
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Code (csharp):
    1.  
    2. inp_Password.contentType = InputField.ContentType.Standard;
    3. inp_Password.contentType = InputField.ContentType.Password;
    4.  
     
  5. NightStalker08

    NightStalker08

    Joined:
    Jan 26, 2015
    Posts:
    3
    1. inp_Password.contentType ? what is this ? string type ? and
    iand where can i put this
    1. 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
     
    Last edited: Jan 27, 2015
  6. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    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.
     
  7. orihq

    orihq

    Joined:
    May 25, 2016
    Posts:
    7
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class PasswordFields: MonoBehaviour {
    6. public InputField password;
    7.  
    8. }
    9.  
    10. void Start () {
    11.     password.contentType = InputField.ContentType.Password;
    12.  
    13. }
    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
     
  8. atulpateltmspl

    atulpateltmspl

    Joined:
    Feb 3, 2020
    Posts:
    7
    Step By Step
    TMPro InputField or Unity InputField for Password ContenctType Show and Hide.


    1
    ) Create Script InputFieldTest.cs and added to any empty gameobject and create simple UI.
    • Code (CSharp):
      1.  
      2. public TMP_InputField PasswordIF;
      3. public Image ShowPassImgIcon,HidePassImgIcon;
      4.  
      5.  
      6. public void ShowPassBtnClick()
      7.     {
      8.         ShowPassImgIcon.enabled = false;
      9.         HidePassImgIcon.enabled = true;
      10.         SetPasswordContentType(PasswordIF,TMP_InputField.ContentType.Standard);
      11.     }
      12.     public void HidePassBtnClick()
      13.     {
      14.         HidePassImgIcon.enabled = false;
      15.         ShowPassImgIcon.enabled = true;
      16.         SetPasswordContentType(PasswordIF,TMP_InputField.ContentType.Password);
      17.     }
      18.     private void SetPasswordContentType(TMP_InputField tmp_if, TMP_InputField.ContentType contetTypePass)
      19.     {
      20.         tmp_if.contentType = contetTypePass;
      21.         tmp_if.DeactivateInputField();
      22.         tmp_if.ActivateInputField();
      23.     }
    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.
     

    Attached Files:

  9. SanjayChauhan

    SanjayChauhan

    Joined:
    Oct 12, 2019
    Posts:
    2
    - Must Use in Newer Veriosn ForceLabelUpdate in Lower version of unity use LabelUpdate

    - Below Example Code
    Code (CSharp):
    1. public class ShowPassword : MonoBehaviour
    2. {
    3.     [SerializeField] private TMP_InputField userPassword;
    4.  
    5.     public void ShowUserPassword()
    6.     {
    7.         if (userPassword.contentType == TMP_InputField.ContentType.Password)
    8.         {
    9.             userPassword.contentType = TMP_InputField.ContentType.Standard;
    10.         }
    11.         else
    12.         {
    13.             userPassword.contentType = TMP_InputField.ContentType.Password;
    14.         }
    15.         userPassword.ForceLabelUpdate();
    16.     }
    17. }
    Attatch any object and Click on button Call ShowUserPassword.
     
    Last edited: Jan 11, 2021
    BV0176 and Kujo87 like this.
  10. furkan6kus

    furkan6kus

    Joined:
    Nov 5, 2020
    Posts:
    1
    Thanks a lot of bro.
     
    SanjayChauhan likes this.