Кнопка Показать пароль (Unity NGUI / C#)
Мне нужно сделать кнопку, которая показывает пароль, который вводит пользователь. Я попытался изменить поле 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 ответ
Использование 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();
}