using System.Collections; using UnityEngine; using UnityEngine.UI; [DisallowMultipleComponent] [RequireComponent(typeof(RectTransform))] [ExecuteAlways] public class ChatBubbleBehaviour : MonoBehaviour { [SerializeField] Text m_Target; [SerializeField] RectOffset m_Padding; [SerializeField] ImageEx m_BubbleIcon; [SerializeField] UIFrame m_UIFrame; [SerializeField] bool left = false; [SerializeField] bool m_PreferredWidth = false; RectTransform m_Rect; RectTransform rect { get { if (m_Rect == null) { m_Rect = transform as RectTransform; } return m_Rect; } } public RectOffset padding { get { return m_Padding; } } private void OnEnable() { Refresh(); } public void DisplayContent(string content, bool _left = false) { if (m_Target == null) { return; } left = _left; m_PreferredWidth = true; var richText = m_Target as RichText; if (richText != null && !left) { richText.AutoNewLine = false; } m_Target.text = content; if (richText != null) { richText.AutoNewLine = true; } Refresh(); } public void DisplaySysBubble(int id, Color color) { ChatBubbleData bubble; if (ChatManager.Instance.TryGetBubble(id, out bubble)) { var bubblePadding = left ? bubble.leftPadding : bubble.rifhtPadding; padding.top = bubblePadding.top; padding.left = bubblePadding.left; padding.right = bubblePadding.right; padding.bottom = bubblePadding.bottom; int resourceType = PhantasmPavilionManager.Instance.GetResourceType(PhantasmPavilionType.ChatBox, id); string resourceValue = PhantasmPavilionManager.Instance.GetResourceValue(PhantasmPavilionType.ChatBox, id); PhantasmPavilionManager.Instance.ShowChatBox(m_BubbleIcon, m_UIFrame, resourceType, resourceValue); m_Target.color = color; var position = rect.anchoredPosition; if (rect.anchoredPosition != position) { rect.anchoredPosition = position; } Refresh(); } } public void DisplayBubble(int id, int playerId) { ChatBubbleData bubble; if (ChatManager.Instance.TryGetBubble(id, out bubble)) { var bubblePadding = left ? bubble.leftPadding : bubble.rifhtPadding; padding.top = bubblePadding.top; padding.left = bubblePadding.left; padding.right = bubblePadding.right; padding.bottom = bubblePadding.bottom; int resourceType = PhantasmPavilionManager.Instance.GetResourceType(PhantasmPavilionType.ChatBox, id); string resourceValue = PhantasmPavilionManager.Instance.GetResourceValue(PhantasmPavilionType.ChatBox, id); PhantasmPavilionManager.Instance.ShowChatBox(m_BubbleIcon, m_UIFrame, resourceType, resourceValue); if (playerId == PlayerDatas.Instance.PlayerId) { m_Target.color = bubble.myColor; } else { m_Target.color = bubble.otherColor; } var position = rect.anchoredPosition; position.y = -bubble.top; if (rect.anchoredPosition != position) { rect.anchoredPosition = position; } Refresh(); } } [ExecuteAlways] private void LateUpdate() { Refresh(); } void Refresh() { if (m_Target == null) { return; } bool nullContent = string.IsNullOrEmpty(m_Target.text); var targetRect = m_Target.rectTransform; var sizeDelta = targetRect.sizeDelta; var width = m_PreferredWidth || !Application.isPlaying ? m_Target.preferredWidth : sizeDelta.x; if (nullContent) { width = 0f; } var height = sizeDelta.y; if (nullContent) { height = 0; } sizeDelta.x = width + m_Padding.left + m_Padding.right; sizeDelta.y = height + m_Padding.top + m_Padding.bottom; if (sizeDelta != rect.sizeDelta) { rect.sizeDelta = sizeDelta; } SetAnchor(m_Target.rectTransform); float top = padding.top; Vector2 position = Vector2.zero; position.x = left ? padding.left : -padding.right; position.y = -top; if (targetRect.anchoredPosition != position) { targetRect.anchoredPosition = position; } } void SetAnchor(RectTransform targetRect) { if (!left) { if (targetRect.anchorMin != Vector2.one) { targetRect.anchorMin = Vector2.one; } if (targetRect.anchorMax != Vector2.one) { targetRect.anchorMax = Vector2.one; } if (targetRect.pivot != Vector2.one) { targetRect.pivot = Vector2.one; } } else { if (targetRect.anchorMin != Vector2.up) { targetRect.anchorMin = Vector2.up; } if (targetRect.anchorMax != Vector2.up) { targetRect.anchorMax = Vector2.up; } if (targetRect.pivot != Vector2.up) { targetRect.pivot = Vector2.up; } } } public float GetBubbleHeight(string content, ArrayList list) { if (m_Target is RichText) { (m_Target as RichText).SetExtenalData(list); } m_Target.text = content; var height = m_Target.preferredHeight; bool nullContent = string.IsNullOrEmpty(content); if (nullContent) { height = 0f; } //Debug.Log($"GetBubbleHeight {height + padding.top + padding.bottom} height {height} padding.top {padding.top} padding.bottom {padding.bottom}"); return height + padding.top + padding.bottom; } }