using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking; using Cysharp.Threading.Tasks; using DefaultNamespace; using TMPro; namespace DefaultNamespace.Component { /// /// 好友项组件 - 负责单个好友项的显示和头像下载 /// 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; // 好友数据 private static readonly Dictionary imageCache = new Dictionary(); // 图片缓存 /// /// 初始化好友项数据 /// /// 好友数据 public void InitializeFriendItem(FriendData data) { if (data == null) { LogDebug("好友数据为空,无法初始化"); return; } friendData = data; LogDebug($"开始初始化好友项: {data.user_username}"); // 设置用户名和等级 SetUserInfo(data); // 异步下载头像 DownloadAvatarAsync(data.user_image).Forget(); } /// /// 设置用户信息(用户名和等级) /// /// 好友数据 private void SetUserInfo(FriendData data) { try { // 设置用户名 if (usernameText != null) { usernameText.text = string.IsNullOrEmpty(data.user_username) ? "未知用户" : data.user_username; LogDebug($"设置用户名: {usernameText.text}"); } else { LogDebug("用户名文本组件未设置"); } // 设置等级 if (levelText != null) { levelText.text = $"等级: {data.user_honor_level}"; LogDebug($"设置等级: {levelText.text}"); } else { LogDebug("等级文本组件未设置"); } } catch (Exception ex) { LogDebug($"设置用户信息时发生异常: {ex.Message}"); } } /// /// 异步下载头像 /// /// 头像URL /// UniTask private async UniTask DownloadAvatarAsync(string imageUrl) { try { // 检查URL是否有效 if (string.IsNullOrEmpty(imageUrl)) { LogDebug("头像URL为空,使用默认头像"); return; } LogDebug($"开始下载头像: {imageUrl}"); // 检查缓存中是否已存在该图片 if (imageCache.ContainsKey(imageUrl)) { LogDebug("从缓存中获取头像"); SetAvatarTexture(imageCache[imageUrl]); return; } // 使用UnityWebRequest下载图片 using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(imageUrl)) { // 设置超时时间 request.timeout = 10; LogDebug("发送头像下载请求"); await request.SendWebRequest(); // 检查请求结果 if (request.result == UnityWebRequest.Result.Success) { LogDebug("头像下载成功"); // 获取下载的纹理 Texture2D texture = DownloadHandlerTexture.GetContent(request); if (texture != null) { // 添加到缓存 imageCache[imageUrl] = texture; // 设置头像 SetAvatarTexture(texture); LogDebug($"头像设置完成,尺寸: {texture.width}x{texture.height}"); } else { LogDebug("下载的纹理为空,使用默认头像"); } } else { LogDebug($"头像下载失败: {request.error}"); } } } catch (Exception ex) { LogDebug($"下载头像时发生异常: {ex.Message}"); } } /// /// 设置头像纹理 /// /// 要设置的纹理 private void SetAvatarTexture(Texture2D texture) { try { if (avatarImage != null && texture != null) { avatarImage.texture = texture; LogDebug("头像纹理设置成功"); } else { LogDebug("头像图片组件未设置或纹理为空"); } } catch (Exception ex) { LogDebug($"设置头像纹理时发生异常: {ex.Message}"); } } /// /// 记录调试日志 /// /// 日志消息 private void LogDebug(string message) { if (enableDebugLog) { Debug.Log($"[FriendItemComponent] {message}"); } } /// /// 获取当前好友数据 /// /// 好友数据 public FriendData GetFriendData() { return friendData; } /// /// 清理缓存(静态方法,可在需要时调用) /// public static void ClearCache() { try { // 清理纹理缓存 foreach (var texture in imageCache.Values) { if (texture != null) { DestroyImmediate(texture); } } imageCache.Clear(); Debug.Log("[FriendItemComponent] 图片缓存已清理"); } catch (Exception ex) { Debug.LogError($"[FriendItemComponent] 清理缓存时发生异常: {ex.Message}"); } } /// /// 获取缓存统计信息 /// /// 缓存统计信息 public static string GetCacheStats() { return $"纹理缓存: {imageCache.Count} 个"; } } }