yyl
6 天以前 f3b4a6331589ff092681169ce6fa7b8aa250c74d
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
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
 
public class ChatBulletView : MonoBehaviour
{
    [SerializeField] GameObject bulletPrefab; // 必须赋值预制体
    [SerializeField] RectTransform bulletContainer;
 
    [SerializeField] float bulletSpeed = 150f;
    [SerializeField] float position1Y = 220f;
    [SerializeField] Vector2 randomYRange = new Vector2(200f, -200);
    [SerializeField] float safeSpacing = 20f;
    [SerializeField] Color defaultColor = Color.white;
 
    private ChatBulletItem lastItemOnPos1;
    private float screenWidth;
    private float screenLeftX;
    ChatManager manager { get { return ChatManager.Instance; } }
    private void Awake()
    {
        screenWidth = 750;
        screenLeftX = -screenWidth / 2;
        GameObjectPoolManager.Instance.CacheGameObject(bulletPrefab, 10, false);
    }
 
    private void Start()
    {
        ChatManager.Instance.OnUpdateTalkEvent += OnUpdateTalkEvent;
    }
 
    private void OnDestroy()
    {
        ChatManager.Instance.OnUpdateTalkEvent -= OnUpdateTalkEvent;
    }
 
    private void OnUpdateTalkEvent(ChatChannel channel, TalkData data)
    {
        if (!ChatManager.Instance.GetBulletSetting(channel))
            return;
        // 0-系统 1-日期 2-自己 3-其他玩家
        int type = manager.GetTalkDataType(data);
        if (type == 1)
            return;
        SpawnBullet(channel, type == 0, type == 0 ? data.Content : GetBulletContent(channel, data), data.InfoList);
    }
 
    private string GetBulletContent(ChatChannel channel, TalkData data)
    {
        switch (channel)
        {
            case ChatChannel.World:
                return Language.Get("Chat17", data.Name, data.Content);
            case ChatChannel.Guild:
                FairyMember fairyMember = PlayerDatas.Instance.fairyData.GetMember((int)data.PlayerID);
                int fmlv = 0;
                if (fairyMember != null)
                {
                    fmlv = fairyMember.FmLV;
                }
                return Language.Get("Chat16", RichTextMsgReplaceConfig.GetRichReplace("FAMILY", fmlv), data.Name, data.Content);
            default:
                return data.Content;
        }
    }
 
 
    private void SpawnBullet(ChatChannel channel, bool isSys, string content, ArrayList infoList)
    {
        if (bulletPrefab == null || GameObjectPoolManager.Instance == null)
            return;
 
        // 【关键修改】从特定对象池获取对象
        GameObject obj = GameObjectPoolManager.Instance.RequestGameObject(bulletPrefab);
 
        // 【重要】对象池管理器会把父节点设为null,这里必须重设回UI容器
        obj.transform.SetParent(bulletContainer, false);
        obj.transform.localScale = Vector3.one;
        obj.SetActive(true);
 
        ChatBulletItem item = obj.GetComponent<ChatBulletItem>();
        if (item == null)
            item = obj.AddComponent<ChatBulletItem>();
 
        Color color = GetChannelColor(channel);
        float spawnY = CalculateSpawnY();
        Vector2 startPos = new Vector2(screenWidth / 2, spawnY);
 
        item.Init(isSys, content, infoList, color, bulletSpeed, startPos, screenLeftX, bulletPrefab, OnBulletFinish);
 
        if (Mathf.Abs(spawnY - position1Y) < 1f)
        {
            lastItemOnPos1 = item;
        }
    }
 
    private float CalculateSpawnY()
    {
        bool pos1Available = true;
 
        if (lastItemOnPos1 != null && lastItemOnPos1.gameObject.activeSelf)
        {
            // 上一条弹幕还没完全离开右侧安全区
            float spawnX = screenWidth / 2;
            if (lastItemOnPos1.GetRightEdgeX() > spawnX - safeSpacing)
            {
                pos1Available = false;
            }
        }
 
        if (pos1Available)
        {
            return position1Y;
        }
        else
        {
            return Random.Range(randomYRange.x, randomYRange.y);
        }
    }
 
    private void OnBulletFinish(ChatBulletItem item)
    {
        if (item == lastItemOnPos1)
        {
            lastItemOnPos1 = null;
        }
    }
 
    private Color GetChannelColor(ChatChannel channel)
    {
        if (!manager.chatChannelBulletColorDict.TryGetValue(channel, out var color))
            return defaultColor;
        return color;
    }
}