_TheStrongestSnail/TheStrongestSnail/Assets/Scripts/Login/UserManagerData.cs
2024-11-15 23:31:54 +08:00

103 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
/*public class UserManager : MonoBehaviour
{
private static UserManager _instance;
public static UserManager Instance
{
get
{
if (_instance == null)
{
// 尝试在场景中找到一个已经存在的实例
_instance = FindObjectOfType<UserManager>();
// 如果场景中不存在则创建一个新的GameObject并添加UserManager组件
if (_instance == null)
{
GameObject go = new GameObject("UserManager");
_instance = go.AddComponent<UserManager>();
}
// 确保实例不会在场景切换时销毁
DontDestroyOnLoad(_instance.gameObject);
}
return _instance;
}
}
// 用于存储服务器返回的用户数据
public int UserId { get; private set; }
public string UserName { get; private set; }
public string Token { get; private set; }
// 从服务器响应数据初始化
public void Initialize(UserManagerData data)
{
UserId = data.userId;
UserName = data.userName;
Token = data.token;
Debug.Log("UserManager Initialized with UserId: " + UserId + ", UserName: " + UserName);
}
}*/
//11/15/1851 awake初始化并保证不销毁转换场景
public class UserManager : MonoBehaviour
{
private static UserManager _instance;
public static UserManager Instance
{
get
{
// 如果实例还未被创建,抛出一个错误提示开发者。
if (_instance == null)
{
Debug.LogError("UserManager Instance is requested, but it has not been initialized yet. Make sure the UserManager is in the scene.");
}
return _instance;
}
}
// 确保在场景切换时实例不会被销毁
private void Awake()
{
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject); // 如果已经存在一个实例,销毁这个新实例,确保单例唯一性
}
}
// 用于存储服务器返回的用户数据
public int UserId { get; private set; }
public string UserName { get; private set; }
public string Token { get; private set; }
// 从服务器响应数据初始化
public void Initialize(UserManagerData data)
{
UserId = data.userId;
UserName = data.userName;
Token = data.token;
Debug.Log("UserManager Initialized with UserId: " + UserId + ", UserName: " + UserName);
}
}
[System.Serializable]
public class UserManagerData
{
public int userId;
public string userName;
public string token;
}