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
using System;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using System.Collections;
 
[RequireComponent(typeof(RectTransform))]
public class ChatBulletItem : MonoBehaviour
{
    [SerializeField] ImageEx contentImage;
    [SerializeField] RichText contentText;
 
    [SerializeField] ImageEx sysImage;
    [SerializeField] RichText sysText;
    // 背景图左右的额外留白 (x:左边距, y:右边距)
    [SerializeField] Vector2 padding = new Vector2(50f, 50f);
    [SerializeField] Vector2 sysPadding = new Vector2(70f, 70f);
    private RectTransform rectTrans;
    private RectTransform imageRect;
    private RectTransform imageSysRect;
    private GameObject sourcePrefab;
    private Tweener moveTweener;
    private Action<ChatBulletItem> onFinishCallback;
 
    private void Awake()
    {
        rectTrans = GetComponent<RectTransform>();
        imageRect = contentImage.rectTransform;
        imageSysRect = sysImage.rectTransform;
    }
 
    public void Init(bool isSys, string content, ArrayList infoList, Color color, float speed, Vector2 startPos, float leftBoundary, GameObject prefab, Action<ChatBulletItem> onFinish)
    {
        if (rectTrans == null)
            rectTrans = GetComponent<RectTransform>();
        contentImage.SetActive(!isSys);
        sysImage.SetActive(isSys);
 
        if (infoList != null)
        {
            contentText.SetExtenalData(infoList);
            sysText.SetExtenalData(infoList);
        }
 
        contentText.text = content;
        sysText.text = content;
        contentText.color = color;
        sysText.color = color;
        sourcePrefab = prefab;
        onFinishCallback = onFinish;
 
        // 获取文本内容的理想宽度
        //LayoutRebuilder.ForceRebuildLayoutImmediate(contentText.rectTransform);
        float txtWidth = contentText.preferredWidth;
 
        float totalWidth = 0;
        float currentHeight = 0;
        if (!isSys)
        {
            // 计算背景图的总宽度 = 文本宽 + 左右Padding
            totalWidth = txtWidth + padding.x + padding.y;
            // 保持原有的高度
            currentHeight = rectTrans.sizeDelta.y;
            // 设置背景图(ContentImage)的大小
            if (imageRect != null)
            {
                imageRect.sizeDelta = new Vector2(totalWidth, currentHeight);
            }
        }
        else
        {
            // 计算背景图的总宽度 = 文本宽 + 左右Padding
            totalWidth = txtWidth + sysPadding.x + sysPadding.y;
            // 图片的高度
            currentHeight = 39;
            // 设置背景图(ContentImage)的大小
            if (imageSysRect != null)
            {
                imageSysRect.sizeDelta = new Vector2(totalWidth, currentHeight);
            }
        }
 
 
        // 设置根节点的大小
        rectTrans.sizeDelta = new Vector2(totalWidth, currentHeight);
        // 设置初始位置
        rectTrans.anchoredPosition = startPos;
 
        // 此时 rectTrans.rect.width 已经是我们手动设置的 totalWidth 了
        float endX = leftBoundary - totalWidth;
        float distance = Mathf.Abs(startPos.x - endX);
        float duration = distance / speed;
 
        // 5. 开始移动
        moveTweener?.Kill();
        moveTweener = rectTrans.DOAnchorPosX(endX, duration)
            .SetEase(Ease.Linear)
            .OnComplete(Recycle);
    }
 
    //ChatBulletItem最右侧的X坐标
    public float GetRightEdgeX()
    {
        return rectTrans.anchoredPosition.x + rectTrans.rect.width;
    }
 
    private void Recycle()
    {
        moveTweener?.Kill();
        moveTweener = null;
 
        onFinishCallback?.Invoke(this);
 
        if (GameObjectPoolManager.Instance != null && sourcePrefab != null)
        {
            GameObjectPoolManager.Instance.GetPool(sourcePrefab).Release(this.gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
 
    private void OnDestroy()
    {
        moveTweener?.Kill();
    }
}