141 lines
4.5 KiB
C#
141 lines
4.5 KiB
C#
using Assets.Framework.Tool;
|
||
using Assets.MyGame.Scripts.Net;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
|
||
/// <summary>
|
||
/// 用户信息面板(设置已分离至 SettingPanel)
|
||
/// </summary>
|
||
public class UserInfoPanel : SubUIBase
|
||
{
|
||
[Header("基础信息")]
|
||
public Image sprite_Avatar;
|
||
public TextMeshProUGUI tMPText_Name;
|
||
public TextMeshProUGUI tMPText_ID;
|
||
public TextMeshProUGUI tMPText_UserNumber;
|
||
public TextMeshProUGUI tMPText_LabelQM;
|
||
public TextMeshProUGUI tMPText_IP;
|
||
public TextMeshProUGUI tMPText_VIPLeave;
|
||
public TextMeshProUGUI tMPText_PhoneNumber;
|
||
|
||
[Header("头像选择")]
|
||
public GameObject prefab_AvatarItem; // 头像选项预制体(含 Image + Button)
|
||
public Transform parent_AvatarList;
|
||
|
||
GameManager gameManager => GameManager.Instance;
|
||
|
||
public override void Awake()
|
||
{
|
||
base.Awake();
|
||
}
|
||
|
||
protected override void OnShow(object args = null)
|
||
{
|
||
base.OnShow(args);
|
||
InitInformation();
|
||
BuildAvatarList();
|
||
}
|
||
|
||
// ==================== 基础信息 ====================
|
||
|
||
private void InitInformation()
|
||
{
|
||
tMPText_Name.color = gameManager.user.BaseInfo.isRedName ? Color.red : Color.white;
|
||
|
||
// 加载当前用户头像
|
||
LoadAvatarImage(sprite_Avatar, gameManager.user.BaseInfo.avatarUrl);
|
||
|
||
tMPText_Name.text = gameManager.user.BaseInfo.userName;
|
||
tMPText_ID.text = gameManager.user.BaseInfo.number;
|
||
tMPText_UserNumber.text = gameManager.user.BaseInfo.mobile;
|
||
tMPText_LabelQM.text = PlayerPrefs.GetString("QM", "我比较懒,什么都没写");
|
||
tMPText_IP.text = gameManager.user.@static.lastLoginIp;
|
||
tMPText_VIPLeave.text = gameManager.user.assets.vipLevel.ToString();
|
||
tMPText_PhoneNumber.text = MaskPhone(gameManager.user.BaseInfo.mobile);
|
||
}
|
||
|
||
// ==================== 头像加载 ====================
|
||
|
||
/// <summary>
|
||
/// 加载头像图片:判断 avatarUrl 是本地ID还是网络URL
|
||
/// </summary>
|
||
private void LoadAvatarImage(Image target, string avatarUrl)
|
||
{
|
||
if (target == null) return;
|
||
|
||
var sprite = gameManager.GetAvatarSprite(avatarUrl);
|
||
if (sprite != null)
|
||
{
|
||
target.sprite = sprite;
|
||
}
|
||
else if (!int.TryParse(avatarUrl, out _))
|
||
{
|
||
WebTool.DownImage(avatarUrl, (texture) =>
|
||
{
|
||
if (texture != null && target != null)
|
||
target.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.one * 0.5f);
|
||
});
|
||
}
|
||
}
|
||
|
||
// ==================== 头像列表 ====================
|
||
|
||
private void BuildAvatarList()
|
||
{
|
||
if (prefab_AvatarItem == null || parent_AvatarList == null) return;
|
||
|
||
// 清空旧列表
|
||
for (int i = parent_AvatarList.childCount - 1; i >= 0; i--)
|
||
Destroy(parent_AvatarList.GetChild(i).gameObject);
|
||
|
||
// 从 GameManager 获取所有头像缓存
|
||
gameManager.InitAvatarCache();
|
||
var allSprites = Resources.LoadAll<Sprite>("Avatar");
|
||
|
||
foreach (var sprite in allSprites)
|
||
{
|
||
var obj = Instantiate(prefab_AvatarItem, parent_AvatarList);
|
||
|
||
// 设置头像图片
|
||
var img = obj.GetComponentInChildren<Image>();
|
||
if (img != null)
|
||
img.sprite = sprite;
|
||
|
||
// 点击按钮 → 应用头像
|
||
var btn = obj.GetComponent<Button>();
|
||
if (btn != null)
|
||
{
|
||
string avatarName = sprite.name; // 文件名即 ID
|
||
btn.onClick.RemoveAllListeners();
|
||
btn.onClick.AddListener(() => ApplyAvatar(avatarName));
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 点击头像项 → 更新主头像 + 上传到服务端
|
||
/// </summary>
|
||
private void ApplyAvatar(string avatarUrl)
|
||
{
|
||
// 更新主头像显示
|
||
LoadAvatarImage(sprite_Avatar, avatarUrl);
|
||
|
||
// 统一交给 GameManager 处理(上传+缓存+事件)
|
||
gameManager.ApplyAvatar(avatarUrl);
|
||
}
|
||
|
||
// ==================== 工具 ====================
|
||
|
||
public static string MaskPhone(string phone)
|
||
{
|
||
if (string.IsNullOrEmpty(phone) || phone.Length <= 6)
|
||
return phone;
|
||
int prefix = 3;
|
||
int suffix = 3;
|
||
int maskCount = phone.Length - prefix - suffix;
|
||
return phone.Substring(0, prefix) + new string('*', maskCount) + phone.Substring(phone.Length - suffix);
|
||
}
|
||
}
|