登录保留上次登录的账号

This commit is contained in:
huyulong 2025-01-02 16:55:57 +08:00
parent d4b6a1c663
commit c3f56cb8ca
2 changed files with 10 additions and 3 deletions

View File

@ -12,12 +12,12 @@ public class AccountManager : MonoBehaviour
public static void SaveLastLoggedAccount(string accountName)
{
PlayerPrefs.SetString(AccountKey, accountName);
PlayerPrefs.Save();
//PlayerPrefs.Save();
}
// 获取上次登录的账号名
public static string GetLastLoggedAccount()
{
return PlayerPrefs.GetString(AccountKey, null); // 默认值为 null如果没有保存过的账号名
return PlayerPrefs.GetString(AccountKey,string.Empty);
}
}

View File

@ -22,9 +22,13 @@ public class LoginPanel : Base
void Start()
{
string lastAccount = AccountManager.GetLastLoggedAccount();
id.text = lastAccount;
Debug.Log("存储数据"+lastAccount);
if (lastAccount != null)
{
Debug.Log($"上次登录的账号是: {lastAccount}");
}
else
{
@ -60,6 +64,9 @@ public class LoginPanel : Base
password = pwd.text//"YYL5371!"
};
image.gameObject.SetActive (true);
//保存登录的账号
AccountManager.SaveLastLoggedAccount(id.text);
// 发送请求
Debug.Log("登陆入参"+ JsonUtility.ToJson(loginBody));
string response = await web.SendRequest(web.URL + "/auth/login", "POST", JsonUtility.ToJson(loginBody));
@ -68,7 +75,7 @@ public class LoginPanel : Base
// 解析服务器返回的数据
loginResponse serverData = JsonConvert.DeserializeObject<loginResponse>(response);
AccountManager.SaveLastLoggedAccount(loginBody.username);
// 将登录响应数据保存到静态变量中
GlobalData.ServerData = serverData;