lcy
2025-12-03 b9a6e7e896b451e9c915e782a1789b2afe079cc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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;
    }
}