using TH1_Logic.Chat; using TH1_UI.View.Common; using TMPro; using UnityEngine; using UnityEngine.UI; namespace TH1_UI.View.Bottom.Chat { public class ChatMessageRowMono : MonoBehaviour { public TextMeshProUGUI SenderText; public TextMeshProUGUI MessageText; public TextMeshProUGUI TimeText; public Image BackgroundImage; // 消息容器(用于对齐控制) public RectTransform MessageContainer; // 颜色配置 public Color SelfMessageColor = new Color(0.8f, 0.95f, 0.8f, 0.6f); public Color OtherMessageColor = new Color(0.95f, 0.95f, 0.95f, 0.6f); public Color SystemMessageColor = new Color(1f, 0.9f, 0.6f, 0.6f); // 字体颜色 public Color SelfNameColor = new Color(0.2f, 0.6f, 0.2f); public Color OtherNameColor = new Color(0.2f, 0.4f, 0.6f); public Color SystemNameColor = new Color(0.8f, 0.4f, 0.2f); public void SetContent(ChatMessageData data) { // 设置发送者名称 if (SenderText != null) { if (data.IsSystem) { SenderText.text = "[系统]"; SenderText.color = SystemNameColor; } else { SenderText.text = BannedWordFilter.Filter(data.SenderName, BannedTextContext.Name) + ":"; SenderText.color = data.IsSelf ? SelfNameColor : OtherNameColor; } } // 设置消息内容 if (MessageText != null) { MessageText.text = BannedWordFilter.Filter(data.Message, BannedTextContext.Chat); } // 设置时间 if (TimeText != null) { TimeText.text = data.Timestamp.ToString("HH:mm"); } // 设置背景颜色 if (BackgroundImage != null) { if (data.IsSystem) { BackgroundImage.color = SystemMessageColor; } else { BackgroundImage.color = data.IsSelf ? SelfMessageColor : OtherMessageColor; } } } /// /// 设置消息对齐方式 /// public void SetAlignment(bool isRight) { if (MessageContainer != null) { MessageContainer.pivot = isRight ? new Vector2(1, 1) : new Vector2(0, 1); MessageContainer.anchorMin = isRight ? new Vector2(1, 0) : new Vector2(0, 0); MessageContainer.anchorMax = isRight ? new Vector2(1, 1) : new Vector2(0, 1); } // 调整文本对齐 if (SenderText != null) { SenderText.alignment = isRight ? TextAlignmentOptions.Right : TextAlignmentOptions.Left; } if (MessageText != null) { MessageText.alignment = isRight ? TextAlignmentOptions.Right : TextAlignmentOptions.Left; } if (TimeText != null) { TimeText.alignment = isRight ? TextAlignmentOptions.Right : TextAlignmentOptions.Left; } } void Awake() { // 如果MessageContainer未设置,尝试获取子物体 if (MessageContainer == null) { // 查找名为"MessageContainer"的子物体,或使用当前transform var container = transform.Find("MessageContainer"); if (container != null) { MessageContainer = container.GetComponent(); } else { MessageContainer = GetComponent(); } } } } }