简体   繁体   中英

Show password button(Unity NGUI / C#)

I need to do button that show a password that user input. I tried change a InputType field with button but this only work for Password->Standard no for Standrd->Password.

this is my script for button

{
GameObject NewPasswordInput;
    private UIInput passwordInput;

    // Use this for initialization
    void Start()
    {
        NewPasswordInput = GameObject.Find("ActuallyPasswordInput");


    }
    // Update is called once per frame
    void Update () {
        passwordInput = GameObject.Find("ActuallyPasswordInput").GetComponent<UIInput>();
        passwordInput.UpdateLabel();
    }
    //
    public void Cancel()
    {
        _stateManager.ChangeScreen(ScreenStateEnum.ProfileEdit);
    }
    //
    public void Confirm()
    {
        _stateManager.ChangeScreen(ScreenStateEnum.ProfileEdit);
    }
    public void ShowPassword()
    {
        if (passwordInput.inputType == UIInput.InputType.Standard) {

                passwordInput.inputType = UIInput.InputType.Password;
            passwordInput.UpdateLabel();
        }
        if (passwordInput.inputType == UIInput.InputType.Password){

            passwordInput.inputType = UIInput.InputType.Standard;
            passwordInput.UpdateLabel();

        }
 }
}

use if-else ! Currently both statements are executed

public void ShowPassword()
{
    if (passwordInput.inputType == UIInput.InputType.Standard) 
    {
        passwordInput.inputType = UIInput.InputType.Password;
        passwordInput.UpdateLabel();
    }

    // after the first statement was executed this will allways be true 
    // and will revert the change right ahead
    if (passwordInput.inputType == UIInput.InputType.Password)
    {
        passwordInput.inputType = UIInput.InputType.Standard;
        passwordInput.UpdateLabel();
    }
}

so the result is allways passwordInput.inputType = UIInput.InputType.Standard no matter what it was before.


Instead use

if (passwordInput.inputType == UIInput.InputType.Standard) 
{
    passwordInput.inputType = UIInput.InputType.Password;
    passwordInput.UpdateLabel();
} 
// execute the second check only if the frírst condition wasn't met before
else if (passwordInput.inputType == UIInput.InputType.Password)
{
    passwordInput.inputType = UIInput.InputType.Standard;
    passwordInput.UpdateLabel();
}

Or to make it even easier to read I would do

public void TooglePasswordVisablilty()
{
    bool isCurrentlyPassword = passwordInput.inputType == UIInput.InputType.Password;

    passwordInput.inputType = isCurrentlyPassword ? UIInput.InputType.Standard : UIInput.InputType.Password;

    passwordInput.UpdateLabel();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM