From d4b6a1c663ab8b8d4357324f082edde2d7e33e54 Mon Sep 17 00:00:00 2001 From: huyulong <1838407198@qq.com> Date: Thu, 2 Jan 2025 16:27:21 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95=E4=BF=9D=E7=95=99=E4=B8=8A?= =?UTF-8?q?=E6=AC=A1=E7=9A=84=E8=B4=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Script/loginScripts/AccountManager.cs | 23 +++++++++++++++++++ .../loginScripts/AccountManager.cs.meta | 11 +++++++++ .../Assets/Script/loginScripts/LoginPanel.cs | 14 ++++++++++- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 xiaofang/Assets/Script/loginScripts/AccountManager.cs create mode 100644 xiaofang/Assets/Script/loginScripts/AccountManager.cs.meta diff --git a/xiaofang/Assets/Script/loginScripts/AccountManager.cs b/xiaofang/Assets/Script/loginScripts/AccountManager.cs new file mode 100644 index 00000000..860f4f35 --- /dev/null +++ b/xiaofang/Assets/Script/loginScripts/AccountManager.cs @@ -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,如果没有保存过的账号名 + } +} diff --git a/xiaofang/Assets/Script/loginScripts/AccountManager.cs.meta b/xiaofang/Assets/Script/loginScripts/AccountManager.cs.meta new file mode 100644 index 00000000..ac280129 --- /dev/null +++ b/xiaofang/Assets/Script/loginScripts/AccountManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bf09cb5642c570843b317a6c507757f6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/xiaofang/Assets/Script/loginScripts/LoginPanel.cs b/xiaofang/Assets/Script/loginScripts/LoginPanel.cs index bcdae806..ae6e9e8b 100644 --- a/xiaofang/Assets/Script/loginScripts/LoginPanel.cs +++ b/xiaofang/Assets/Script/loginScripts/LoginPanel.cs @@ -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(response); + AccountManager.SaveLastLoggedAccount(loginBody.username); + // 将登录响应数据保存到静态变量中 GlobalData.ServerData = serverData; ReadRoom.instance.ID = id.text;