using Cysharp.Threading.Tasks; using System; using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; using WXGame.Network; namespace DefaultNamespace.Component { /// /// 好友-聊天 /// public class FriendChat : MonoBehaviour { //数据 /// /// 好友数据 /// public FriendData friendData; /// /// 自身数据 /// public FriendData friendDataMy; /// /// 历史消息 /// public HistoryRoot historyRoot; //预制体 public GameObject friendBubblePrefab; public GameObject myBubblePrefab; public GameObject timePrefab; public Transform contentMy; //UI /// /// 关闭按钮 /// public Button closeBt; /// /// 消息发送按钮 /// public Button btnSend; /// /// 输入框 /// public TMP_InputField inputField; public List gameObjectList = new List(); // Start is called before the first frame update void Start() { closeBt.onClick.AddListener(delegate { gameObjectList.ForEach(x=> { x.gameObject.SetActive(false); }); UIManager.Instance.HidePopup("好友聊天"); }); friendDataMy = new FriendData(); friendDataMy.friend_id = MqttManager.Instance.userInfo.user_id; friendDataMy.user_username = MqttManager.Instance.userInfo.user_username; friendDataMy.user_image = MqttManager.Instance.userInfo.user_image; friendDataMy.user_honor_level = MqttManager.Instance.userInfo.user_honor_level; //Invoke(nameof(Init), 0.5f); btnSend.onClick.AddListener(delegate { if (inputField.text != "") { SendMessageMy(inputField.text); AddBubble(inputField.text, true); } inputField.text = ""; }); } /// /// 初始化 /// public void Init() { if (GetComponent() && GetComponent().strParam != "") { friendData= JsonUtility.FromJson(GetComponent().strParam); if (friendData != null) GetHistory(); } } /// /// 获取历史消息 /// async void GetHistory() { try { Debug.Log("开始获取历史消息..."); // 使用UserDataNetworkManager获取用户信息 HistoryRoot response = await UserDataNetworkManager.Instance.GetHistroy(friendData.friend_id.ToString()); if (response != null && response.data != null) { historyRoot = response; foreach (var item in historyRoot.data) { AddBubble(item.chat_content, item.chat_id != friendData.friend_id); } Debug.Log("获取历史消息获取成功!"); } else { Debug.LogError("获取历史消息为空"); } } catch (Exception ex) { Debug.LogError($"获取历史消息发生错误: {ex.Message}"); // 可以在这里添加错误处理逻辑,比如显示错误提示给用户 } } /// /// 发送消息 /// /// async void SendMessageMy(string value) { try { Debug.Log("发送消息..."); // 使用UserDataNetworkManager获取用户信息 SendMessageRoot response = await UserDataNetworkManager.Instance.SendMessage(value, friendData.friend_id.ToString()); // 根据code判断发送是否成功,code为0表示成功 if (response != null && response.code == 0) { Debug.Log($"发送消息成功!响应信息: {response.data}"); } else { Debug.LogError($"发送消息失败!code: {response?.code}, msg: {response?.msg}"); } } catch (Exception ex) { Debug.LogError($"发送消息发生错误: {ex.Message}"); // 可以在这里添加错误处理逻辑,比如显示错误提示给用户 } } /// /// 添加气泡 /// /// /// public void AddBubble(string content, bool isMy) { GameObject newBubble = isMy ? Instantiate(myBubblePrefab, contentMy) : Instantiate(friendBubblePrefab, contentMy); gameObjectList.Add(newBubble); FriendChatBubble friendChatBubble = newBubble.GetComponent(); FriendData _friendData = new FriendData(); if (isMy) { _friendData = friendDataMy; } else { _friendData = friendData; } friendChatBubble.username.text = _friendData.user_username; friendChatBubble.user_content.text = content; DownloadAvatarAsync(_friendData.user_image, friendChatBubble.avatarImage); } private async UniTask DownloadAvatarAsync(string imageUrl, RawImage avatarImage) { 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, avatarImage); Debug.Log($"头像设置完成,尺寸: {texture.width}x{texture.height}"); } else { Debug.Log("头像下载失败,使用默认头像"); } } catch (Exception ex) { Debug.Log($"下载头像时发生异常: {ex.Message}"); } } /// /// 设置头像纹理 /// /// 要设置的纹理 private void SetAvatarTexture(Texture2D texture, RawImage avatarImage) { try { if (avatarImage != null && texture != null) { avatarImage.texture = texture; Debug.Log("头像纹理设置成功"); } else { Debug.Log("头像图片组件未设置或纹理为空"); } } catch (Exception ex) { Debug.Log($"设置头像纹理时发生异常: {ex.Message}"); } } } }