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;