登录保留上次的账号

This commit is contained in:
huyulong 2025-01-02 16:27:21 +08:00
parent 4e4fe3bfd9
commit d4b6a1c663
3 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,23 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AccountManager : MonoBehaviour
{
private const string AccountKey = "LastLoggedAccount";
// 保存账号名
public static void SaveLastLoggedAccount(string accountName)
{
PlayerPrefs.SetString(AccountKey, accountName);
PlayerPrefs.Save();
}
// 获取上次登录的账号名
public static string GetLastLoggedAccount()
{
return PlayerPrefs.GetString(AccountKey, null); // 默认值为 null如果没有保存过的账号名
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bf09cb5642c570843b317a6c507757f6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -17,10 +17,19 @@ public class LoginPanel : Base
public InputField sjh;
public InputField yzm;
public Image image;
// Start is called before the first frame update
void Start()
{
string lastAccount = AccountManager.GetLastLoggedAccount();
if (lastAccount != null)
{
Debug.Log($"上次登录的账号是: {lastAccount}");
}
else
{
Debug.Log("没有找到上次登录的账号.");
}
loginBtn.onClick.AddListener(OnClickLoginBtn);
getYzmBtn.onClick.AddListener(OnClickGetYzmBtn);
image.gameObject.SetActive(false);
@ -39,6 +48,7 @@ public class LoginPanel : Base
//登录逻辑
public async void Login()
{
// 创建登录请求的 body
auth_login loginBody = new auth_login
@ -58,6 +68,8 @@ public class LoginPanel : Base
// 解析服务器返回的数据
loginResponse serverData = JsonConvert.DeserializeObject<loginResponse>(response);
AccountManager.SaveLastLoggedAccount(loginBody.username);
// 将登录响应数据保存到静态变量中
GlobalData.ServerData = serverData;
ReadRoom.instance.ID = id.text;