yyl
2026-05-11 51b0f6ed9f4e1d3bb6f8144470b46908c7699a96
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
//--------------------------------------------------------
//    [Author]:           玩个游戏
//    [  Date ]:           Saturday, October 07, 2017
//--------------------------------------------------------
 
using System;
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
 
public class ScrollTipWin : UIBase
{
    [SerializeField] RectTransform content;
    [SerializeField] RectTransform mask;
 
    [SerializeField, Header("显示条数")]
    int tipDisplayCnt = 3;
    [SerializeField, Header("每次滚动的距离")]
    float m_TipDistance = 50.0f;
    [SerializeField, Header("提示预制体高度")]
    float m_TipHeight = 40.0f;
    [SerializeField, Header("显示时间")]
    float m_TipShowTime = 1.0f;
    [SerializeField, Header("移动时间")]
    float m_TipMoveTime = 0.2f;
    [SerializeField, Header("隐藏时间")]
    float m_TipHideTime = 0.5f;
    
    bool isLoopRunning = false;
 
    protected override void InitComponent()
    {
 
    }
 
    protected override void OnClose()
    {
 
    }
 
    protected override void OnOpen()
    {
 
    }
 
    protected override void OnPreClose()
    {
        ScrollTip.OnTipReceiveEvent -= OnAddTipEvent;
        ScrollTip.ReleaseAll();
    }
 
    protected override void OnPreOpen()
    {
        ScrollTip.tipMoveTime = m_TipMoveTime;
        ScrollTip.OnTipReceiveEvent += OnAddTipEvent;
        mask.sizeDelta = mask.sizeDelta.SetY(tipDisplayCnt * m_TipHeight + (tipDisplayCnt - 1) * (m_TipDistance - m_TipHeight) + 10);
        
        // 如果有待处理的提示,立即启动循环
        if (ScrollTip.m_Hints.Count > 0)
        {
            LoopTipReceiveEvent().Forget();
        }
    }
 
    async UniTask LoopTipReceiveEvent()
    {
        if (isLoopRunning) return; // 防止重复启动
        
        isLoopRunning = true;
        try
        {
            while (ScrollTip.m_Hints.Count > 0)
            {
                OnTipReceiveEvent().Forget();
                await UniTask.Delay(100);
                if (this == null) return; // destroyed during await
            }
        }
        finally
        {
            isLoopRunning = false;
        }
    }
 
    void OnAddTipEvent()
    {
        // 只有在没有循环运行时才启动新的循环
        if (!isLoopRunning)
        {
            LoopTipReceiveEvent().Forget();
        }
    }
 
    private async UniTask OnTipReceiveEvent()
    {
        if (ScrollTip.m_Hints.Count > 0 && IsCanAdd())
        {
            if (ScrollTip.m_ActiveTips.Count >= tipDisplayCnt)
            {
                ScrollTip.Release(ScrollTip.m_ActiveTips[0], false);
                ScrollTip.tipMoveTime = Time.deltaTime;
            }
            else
            {
                ScrollTip.tipMoveTime = m_TipMoveTime;
            }
            // for (int i = 0; i < ScrollTip.m_ActiveTips.Count; i++)
            // {
            //     ScrollTip.m_ActiveTips[i].Play(ScrollTip.ScrollTipState.Move);
            // }
            ScrollTipDetail tipDetail = await ScrollTip.Request();
            if (tipDetail != null)
            {
                // 首次登录时 pool 为 null,Request() 异步加载预制体耗时 >100ms。
                // LoopTipReceiveEvent 每 100ms 重入,可能已有另一次调用先消耗了 m_Hints[0]。
                // 若 await 返回时 m_Hints 已空,必须将 tipDetail 归还池,否则它会被插入 Canvas 但
                // 永远不会播放动画,导致提示条卡在屏幕上(第一次登录复现的根因)。
                if (ScrollTip.m_Hints.Count == 0)
                {
                    ScrollTip.Release(tipDetail, false);
                    return;
                }
                tipDetail.SetTipConfig(m_TipShowTime, m_TipHideTime, m_TipDistance);
                ScrollTip.m_ActiveTips.Add(tipDetail);
                var rt = tipDetail.transform;
                rt.SetParent(content.parent);
                rt.localScale = Vector3.one;
                rt.localPosition = content.localPosition;
                var _hint = ScrollTip.m_Hints[0];
                ScrollTip.m_Hints.RemoveAt(0);
                tipDetail.ShowTip(_hint);
                tipDetail.Play(ScrollTip.ScrollTipState.Move);
            }
        }
    }
 
    private bool IsCanAdd()
    {
        // for (int i = 0; i < ScrollTip.m_ActiveTips.Count; i++)
        // {
        //     if (ScrollTip.m_ActiveTips[i].presentState == ScrollTip.ScrollTipState.Move)
        //     {
        //         return false;
        //     }
        // }
        return true;
    }
}