Unity:InputField账号密码输入

using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class LoginWindow : MonoBehaviour
{
    [SerializeField] private TMP_InputField ipd_user;
    [SerializeField] private TMP_InputField ipd_pwd;
    [SerializeField] private TextMeshProUGUI txt_errorMessage;
    private string username;
    private string password;
    [SerializeField] private Button btn_Login;

    public  void Awake()
    {

        ipd_user.onValidateInput += CheckForEnter;
        ipd_pwd.onValidateInput += CheckForEnter;

        btn_Login.onClick.AddListener(() =>
        {
            Login();
        });

    }

    private char CheckForEnter(string text, int charIndex, char addedChar)
    {
        if (addedChar == '\n')
        {
            Debug.Log("检测到回车");
            Login(false);
            return '\0';
        }
        else
            return addedChar;
    }

    private async void Login(bool sound = true)
    {
        //if (sound)
        //    SoundPlay(audioClick);

        txt_errorMessage.text = "";
        username = ipd_user.text;
        password = ipd_pwd.text;

        if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
        {
            txt_errorMessage.text = "用户名或密码不能为空";
        }
        else
        {
            var result = await Entrance.instance.Login(username, password);

            if (result == null)
                txt_errorMessage.text = "请检查网络连接";
            else
            {
                if (result.success)
                    Back();
                else
                    txt_errorMessage.text = "账号密码错误";
            }
        }
    }

    public void Init()
    {
        txt_errorMessage.text = "";
        ipd_user.text = "";
        ipd_pwd.text = "";
        Show();
    }

    private void OnDestroy()
    {
        ipd_user.onValidateInput -= CheckForEnter;
        ipd_pwd.onValidateInput -= CheckForEnter;
    }

 void Back(Action finish = null)
    {
        //CG.DOFade(0, 0.15f);
        /*child1.DOScaleY(0, 0.15f).OnComplete(() =>
        {
            HideType();
            finish?.Invoke();
        });*/
    }

    public void Show()
    {
        gameObject.SetActive(true);
        //CG.alpha = 0;
        /*SoundPlay(audioShow);
        child1.localScale = new Vector3(1, 0, 1);
        //CG.DOFade(1, 0.25f);
        child1.DOScaleY(1, 0.25f);*/
    }
}

猜你喜欢

转载自blog.csdn.net/XYH_78/article/details/126405731
今日推荐