Кнопка Показать пароль (Unity NGUI / C #) - PullRequest
0 голосов
/ 14 декабря 2018

Мне нужно сделать кнопку, которая покажет пароль, который вводит пользователь.Я попытался изменить поле InputType с помощью кнопки, но это работает только для Password-> Standard no для Standrd-> Password.

это мой скрипт для кнопки

{
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();

        }
 }
}

1 Ответ

0 голосов
/ 14 декабря 2018

используйте if-else!В настоящее время оба оператора выполняются

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();
    }
}

, поэтому результат всегда равен passwordInput.inputType = UIInput.InputType.Standard независимо от того, что было раньше.


Вместо этого используйте

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();
}

Иличтобы было еще проще читать, я бы сделал

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

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

    passwordInput.UpdateLabel();
}
...