using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; [DisallowMultipleComponent] [RequireComponent(typeof(RectTransform))] [ExecuteAlways] public class ChatBubbleBehaviour : MonoBehaviour { [SerializeField] Text m_Target; [SerializeField] RectOffset m_Padding; [SerializeField] FlipImage m_Flip; [SerializeField] Image m_BubbleIcon; [SerializeField] bool left = false; [SerializeField] bool m_PreferredWidth = false; [SerializeField] RectTransform m_ContainerVoice; const float space = 5.0f; private int bubbleId = 0; 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 targetRect = m_Target.rectTransform; var richText = m_Target as RichText; if (richText != null && !left) { richText.AutoNewLine = false; } m_Target.text = content; if (!left) { if (m_Target.preferredWidth > targetRect.rect.width) { m_Target.alignment = TextAnchor.UpperLeft; m_PreferredWidth = false; } else { m_Target.alignment = TextAnchor.UpperRight; } } if (richText != null) { richText.AutoNewLine = true; } Refresh(); } public void DisplayBubble(int id) { bubbleId = id; ChatBubbleManager.ChatBubble bubble; if (ChatBubbleManager.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; bool requireFlip = false; var iconKey = bubble.GetBubbleIcon(left, ref requireFlip); //m_BubbleIcon.SetSprite(iconKey); UIFrame frame = m_BubbleIcon.GetComponent(); if (UIFrameMgr.Inst.ContainsDynamicImage(iconKey)) { if (frame == null) frame = m_BubbleIcon.gameObject.AddComponent(); frame.ResetFrame(iconKey); frame.enabled = true; } else { if (frame != null) frame.enabled = false; m_BubbleIcon.SetSprite(iconKey); } m_Flip.flipHorizontal = requireFlip; m_Target.color = bubble.color; var position = rect.anchoredPosition; // TODO YYL // ChatBubbleBoxConfig config = ChatBubbleBoxConfig.Get(bubbleId); // position.y = -config.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; } if (m_ContainerVoice != null) { width = Mathf.Max(m_ContainerVoice.sizeDelta.x, width); height += m_ContainerVoice.sizeDelta.y; if (!nullContent) { height += space; } } 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); if (m_ContainerVoice != null) { SetAnchor(m_ContainerVoice); } float top = padding.top; Vector2 position = Vector2.zero; position.x = left ? padding.left : -padding.right; if (m_ContainerVoice != null) { position.y = -top; if (m_ContainerVoice.anchoredPosition != position) { m_ContainerVoice.anchoredPosition = position; } top = top + m_ContainerVoice.sizeDelta.y; top += space; } 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; } if (m_ContainerVoice != null) { height += m_ContainerVoice.sizeDelta.y; if (!nullContent) { height += space; } } return height + padding.top + padding.bottom; } }