WX-Game1/Assets/Scripts/Component/FriendItemComponent.cs

179 lines
5.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 System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Cysharp.Threading.Tasks;
using DefaultNamespace;
using TMPro;
using WXGame.Network;
namespace DefaultNamespace.Component
{
/// <summary>
/// 好友项组件
/// </summary>
public class FriendItemComponent : MonoBehaviour
{
[Header("UI组件引用")]
[SerializeField] private RawImage avatarImage; // 头像图片组件
[SerializeField] private TMP_Text usernameText; // 用户名文本组件
[SerializeField] private TMP_Text levelText; // 等级文本组件
[Header("调试设置")]
[SerializeField] private bool enableDebugLog = true; // 是否启用调试日志
// 私有字段
private FriendData friendData; // 好友数据
/// <summary>
/// 初始化好友项数据
/// </summary>
/// <param name="data">好友数据</param>
public void InitializeFriendItem(FriendData data)
{
if (data == null)
{
Debug.Log("好友数据为空,无法初始化");
return;
}
friendData = data;
Debug.Log($"开始初始化好友项: {data.user_username}");
// 设置用户名和等级
SetUserInfo(data);
// 异步下载头像
DownloadAvatarAsync(data.user_image).Forget();
}
/// <summary>
/// 设置用户信息(用户名和等级)
/// </summary>
/// <param name="data">好友数据</param>
private void SetUserInfo(FriendData data)
{
try
{
// 设置用户名
if (usernameText != null)
{
usernameText.text = string.IsNullOrEmpty(data.user_username) ? "未知用户" : data.user_username;
Debug.Log($"设置用户名: {usernameText.text}");
}
else
{
Debug.Log("用户名文本组件未设置");
}
// 设置等级
if (levelText != null)
{
levelText.text = $"等级: {data.user_honor_level}";
Debug.Log($"设置等级: {levelText.text}");
}
else
{
Debug.Log("等级文本组件未设置");
}
}
catch (Exception ex)
{
Debug.Log($"设置用户信息时发生异常: {ex.Message}");
}
}
/// <summary>
/// 异步下载头像
/// </summary>
/// <param name="imageUrl">头像URL</param>
/// <returns>UniTask</returns>
private async UniTask DownloadAvatarAsync(string imageUrl)
{
try
{
// 检查URL是否有效
if (string.IsNullOrEmpty(imageUrl))
{
Debug.Log("头像URL为空使用默认头像");
return;
}
Debug.Log($"开始下载头像: {imageUrl}");
// 使用WebRequestManager下载图片
Texture2D texture = await WebRequestManager.Instance.DownloadImageAsync(imageUrl);
if (texture != null)
{
// 设置头像
SetAvatarTexture(texture);
Debug.Log($"头像设置完成,尺寸: {texture.width}x{texture.height}");
}
else
{
Debug.Log("头像下载失败,使用默认头像");
}
}
catch (Exception ex)
{
Debug.Log($"下载头像时发生异常: {ex.Message}");
}
}
/// <summary>
/// 设置头像纹理
/// </summary>
/// <param name="texture">要设置的纹理</param>
private void SetAvatarTexture(Texture2D texture)
{
try
{
if (avatarImage != null && texture != null)
{
avatarImage.texture = texture;
Debug.Log("头像纹理设置成功");
}
else
{
Debug.Log("头像图片组件未设置或纹理为空");
}
}
catch (Exception ex)
{
Debug.Log($"设置头像纹理时发生异常: {ex.Message}");
}
}
/// <summary>
/// 获取当前好友数据
/// </summary>
/// <returns>好友数据</returns>
public FriendData GetFriendData()
{
return friendData;
}
/// <summary>
/// 清理图片缓存已移除使用WebRequestManager的简单缓存
/// </summary>
public static void ClearCache()
{
Debug.Log("[FriendItemComponent] 缓存清理功能已移除使用WebRequestManager的简单缓存");
}
/// <summary>
/// 获取缓存统计信息已移除使用WebRequestManager的简单缓存
/// </summary>
/// <returns>缓存统计信息</returns>
public static string GetCacheStats()
{
return "使用WebRequestManager的简单缓存";
}
}
}