三国卡牌客户端基础资源仓库
yyl
2025-04-23 7816f15f2e98a0faa2bdbc50307c7506cbb9a6b7
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Cysharp.Threading.Tasks;
 
/// <summary>
/// 启动加载窗口
/// </summary>
public class LaunchLoadingWin : MonoBehaviour
{
    // 单例实例
    private static LaunchLoadingWin _instance;
 
    public static LaunchLoadingWin Instance
    {
        get
        {
            if (_instance == null)
            {
                Debug.Log("LaunchLoadingWin 界面未打开");
                return null;
            }
 
            return _instance;
        }
    }
    
    // UI组件引用
    [SerializeField] public Text titleText;
    [SerializeField] public Text tipsText;
    [SerializeField] public Image progressBar;
    [SerializeField] public Text progressText;
    
    // 提示文本列表
    [SerializeField] private List<string> tipsList = new List<string>();
    
    // 当前提示索引
    private int currentTipIndex = 0;
    
    // 是否正在显示
    
    // 计时器
    private float tipChangeTimer = 0f;
    private const float TIP_CHANGE_INTERVAL = 3f; // 提示切换间隔时间
    
    private void Awake()
    {
        _instance = this;
        
        // 初始化提示列表
        InitTipsList();
        
        // 初始化进度条
        SetProgress(0f);
    }
    
    /// <summary>
    /// 初始化提示列表
    /// </summary>
    private void InitTipsList()
    {
        tipsList.Clear();
        tipsList.Add(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.LoadingResources));
        tipsList.Add(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.CheckingVersionUpdate));
        tipsList.Add(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.InitializingGame));
        tipsList.Add(FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.EnteringGameWorld));
    }
    
    
    /// <summary>
    /// 打开窗口
    /// </summary>
    public static void OpenWindow()
    {
        if (null != _instance)
        {
            DestroyImmediate(_instance.gameObject);
            _instance = null;
        }
 
        _instance = GameObject.Instantiate(Resources.Load<GameObject>("LaunchLoadingWin")).GetComponent<LaunchLoadingWin>();
        _instance.gameObject.SetActive(true);
    }
    
    /// <summary>
    /// 关闭窗口
    /// </summary>
    public void CloseWindow()
    {
        // 隐藏窗口而不是销毁
        DestroyImmediate(gameObject);
        _instance = null;
        
        Debug.Log("关闭加载窗口");
    }
    
    /// <summary>
    /// 设置进度
    /// </summary>
    /// <param name="progress">进度值(0-1)</param>
    public void SetProgress(float progress)
    {
        // 限制进度值范围
        progress = Mathf.Clamp01(progress);
        
        // 更新进度条
        if (progressBar != null)
        {
            progressBar.fillAmount = progress;
        }
        
        // 更新进度文本
        if (progressText != null)
        {
            progressText.text = $"{(int)(progress * 100)}%";
        }
    }
    
    /// <summary>
    /// 设置提示文本
    /// </summary>
    /// <param name="tip">提示文本</param>
    public void SetTip(string tip)
    {
        if (tipsText != null)
        {
            tipsText.text = tip;
        }
    }
    
    
    /// <summary>
    /// 异步显示加载进度
    /// </summary>
    /// <param name="duration">加载持续时间</param>
    /// <param name="targetProgress">目标进度</param>
    public async UniTask ShowProgressAsync(float duration, float targetProgress = 1.0f)
    {
        float startProgress = progressBar != null ? progressBar.fillAmount : 0f;
        float currentProgress = startProgress;
        float elapsedTime = 0f;
        
        while (elapsedTime < duration)
        {
            elapsedTime += Time.deltaTime;
            currentProgress = Mathf.Lerp(startProgress, targetProgress, elapsedTime / duration);
            SetProgress(currentProgress);
            await UniTask.Yield();
        }
        
        // 确保最终进度正确
        SetProgress(targetProgress);
    }
 
    /// <summary>
    /// 当前状态索引
    /// </summary>
    private int currentStateIndex = -1;
    
    /// <summary>
    /// 设置当前状态
    /// </summary>
    /// <param name="stateIndex">状态索引</param>
    public void SetCurrentState(int stateIndex)
    {
        currentStateIndex = stateIndex;
        
        // 根据状态索引更新提示文本
        UpdateTipsByState();
    }
 
    /// <summary>
    /// 根据当前状态更新提示文本
    /// </summary>
    private void UpdateTipsByState()
    {
        string tip = "";
        
        // 根据状态索引设置对应的提示文本
        switch (currentStateIndex)
        {
            case FirstPackLang.TextIndex.InitSDK:
                tip = FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.InitializingSDK);
                break;
            case FirstPackLang.TextIndex.ExtractResources:
                tip = FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.CheckingResources);
                break;
            case FirstPackLang.TextIndex.CheckVersion:
                tip = FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.CheckingVersion);
                break;
            case FirstPackLang.TextIndex.HotUpdate:
                tip = FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.PrepareUpdate);
                break;
            case FirstPackLang.TextIndex.EnterHotUpdateAssembly:
                tip = FirstPackLang.Instance.GetText(FirstPackLang.TextIndex.EnteringGame);
                break;
        }
        
        // 设置提示文本
        if (!string.IsNullOrEmpty(tip))
        {
            SetTip(tip);
        }
    }
}