显示密码按钮(Unity NGUI / C#)

时间:2018-12-14 08:02:29

标签: c# unity3d passwords ngui

我需要执行显示用户输入密码的按钮。 我尝试用按钮更改InputType字段,但这仅适用于密码->标准否适用于Standrd->密码。

这是我的按钮脚本

{
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 :(得分:2)

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