Unity_UGUI_LoginPanel

小鸟游星野
小鸟游星野
发布于 2025-02-18 / 7 阅读
0
0

Unity_UGUI_LoginPanel

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LoginPanel : UIBase
{
    public InputField usernameField;
    public InputField passwordField;
    public Button sureButton;
    public Button registerButton;
    public Toggle passWordToggle;
    public Toggle autoLoginToggle;
    // Start is called before the first frame update

    protected override void Start()
    {
        base.Start();
        ShowMe();
    }

    protected override void Init()
    {
        //登录
        sureButton.onClick.AddListener(() =>
        {
            //登录执行的代码
            if (LoginMgr.Instance.CheckInfo(usernameField.text, passwordField.text))
            {
                
                
                //登录成功之后的代码
                LoginMgr.Instance.loginData.username = usernameField.text;
                LoginMgr.Instance.loginData.password = passwordField.text;
                LoginMgr.Instance.loginData.autoLoginToggle = autoLoginToggle.isOn;
                LoginMgr.Instance.loginData.passWordToggle = passWordToggle.isOn;
                
                print(LoginMgr.Instance.loginData.frontServerId);
                if (LoginMgr.Instance.loginData.frontServerId == -1)
                {
                    //-1代表第一次进入游戏进入选服务器面板
                }
                else
                {
                    //上次已经选过服务器了
                    UIManager.Instance.CreatePanel<ServerPanel>();
                }
                UIManager.Instance.DelPanel<LoginPanel>();
            }
            else
            {
                //不存在该用户名
                UIManager.Instance.CreatePanel<TipPanel>().ChangeTip("用户名或密码错误");
            }
            
        });
        //注册
        registerButton.onClick.AddListener(() =>
        {
            UIManager.Instance.CreatePanel<RegisterPanel>();
            
            UIManager.Instance.DelPanel<LoginPanel>();
        });
        //自动登录
        autoLoginToggle.onValueChanged.AddListener((bl) =>
        {
            LoginMgr.Instance.loginData.autoLoginToggle = bl;
            if (autoLoginToggle.isOn)
            {
                passWordToggle.isOn = true;
            }
        });
        //记住密码
        passWordToggle.onValueChanged.AddListener((bl) =>
        {
            LoginMgr.Instance.loginData.passWordToggle = bl;
            if (!passWordToggle.isOn)
            {
                autoLoginToggle.isOn = false;
            }
        });
        
        
    }
    
    //为了将本地化的数据实例化到ui上需要对其进行赋值
    public override void ShowMe()
    {
        base.ShowMe();
        //本地保存
        passWordToggle.isOn = LoginMgr.Instance.loginData.passWordToggle;
        autoLoginToggle.isOn = LoginMgr.Instance.loginData.autoLoginToggle;
        
        
        usernameField.text = LoginMgr.Instance.loginData.username;
        //更具用户是否勾选了记住密码来保存密码
        if (passWordToggle.isOn)
        {
            passwordField.text = LoginMgr.Instance.loginData.password;
        }
        //如果勾选了自动登录
        if (autoLoginToggle.isOn)
        {
            //处理对应的内容
        }
    }


    public void ChangeInfro(string username, string password)
    {
        usernameField.text = username;
        passwordField.text = password;
    }
}


评论