yyl
18 小时以前 ad14e2586cd13c52b30e1cc481724a1e09407f6a
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
/// <summary>
/// 游戏主界面底部功能按钮
/// </summary>
public class MainWin : UIBase
{
    // 底部按钮组
    public Button[] bottomTabButtons;
 
    public Text hammerText;
 
    
    // 当前选中的底部标签索引
    private int currentTabIndex = 0;
    
    // 当前打开的子界面
    private UIBase currentSubUI;
 
    /// <summary>
    /// 初始化组件
    /// </summary>
    protected override void InitComponent()
    {
        base.InitComponent();
 
        // 初始化UI组件事件
        InitButtonEvents();
    }
    
    /// <summary>
    /// 初始化UI组件事件
    /// </summary>
    private void InitButtonEvents()
    {
        // 初始化底部按钮
        for (int i = 0; i < bottomTabButtons.Length; i++)
        {
            int index = i; // 捕获索引
            bottomTabButtons[i].onClick.AddListener(() => {
                OnBottomTabButtonClicked(index);
            });
        }
 
    }
    
    public override void Refresh()
    {
        UpdateCurrency();
    }
 
    protected override void OnPreOpen()
    {
        PlayerDatas.Instance.playerDataRefreshEvent += PlayerDataRefresh;
        // 默认选中第一个标签
        SelectBottomTab(0);
 
        // 刷新UI
        Refresh();
    }
 
    protected override void OnPreClose()
    {
        PlayerDatas.Instance.playerDataRefreshEvent -= PlayerDataRefresh;
    }
 
    void PlayerDataRefresh(PlayerDataType type)
    {
        if (type == PlayerDataType.default26)
        {
            UpdateCurrency();
        }
    }
 
    
    /// <summary>
    /// 更新货币信息
    /// </summary>
    private void UpdateCurrency()
    {
        hammerText.text = UIHelper.GetMoneyCnt(41).ToString();
    }
    
    /// <summary>
    /// 底部标签按钮点击
    /// </summary>
    private void OnBottomTabButtonClicked(int index)
    {
        if (index == 0)
        { 
            if (currentSubUI != null && currentSubUI.name == "HomeWin")
            {
                //打开主界面的情况下再点击按钮,执行攻击逻辑
            }
        }
        SelectBottomTab(index);
    }
    
    /// <summary>
    /// 选择底部标签
    /// </summary>
    private void SelectBottomTab(int index)
    {
        // 如果点击当前已选中的标签,不做处理
        if (currentTabIndex == index && currentSubUI != null)
        {
            return;
        }
        
        // 更新当前选中的标签索引
        currentTabIndex = index;
        
        // 更新按钮状态
        UpdateButtonsState();
        
        // 关闭当前打开的子界面
        CloseCurrentSubUI();
        
        // 根据选中的标签打开对应的界面
        OpenSubUIByTabIndex(index);
    }
    
    /// <summary>
    /// 更新按钮状态
    /// </summary>
    private void UpdateButtonsState()
    {
        // 遍历所有按钮,设置选中状态
        for (int i = 0; i < bottomTabButtons.Length; i++)
        {
 
            // bottomTabButtons[i].image.color = (i == currentTabIndex) ?  Color.white : Color.gray;
        }
    }
    
    /// <summary>
    /// 关闭当前打开的子界面
    /// </summary>
    private void CloseCurrentSubUI()
    {
        if (currentSubUI != null)
        {
            // 关闭当前界面
            currentSubUI.CloseWindow();
            currentSubUI = null;
        }
    }
 
    /// <summary>
    /// 根据标签索引打开对应的子界面
    /// </summary>
    private void OpenSubUIByTabIndex(int index)
    {
 
        Debug.Log("打开子界面 : " + index);
        // 主城 内政 武将 挑战 公会
        //根据索引打开不同的界面
         switch (index)
        {
            case 0:
                // 打开主页界面
                currentSubUI = UIManager.Instance.OpenWindow<HomeWin>();
                Debug.Log("打开主城界面");
                break;
            case 1:
                // currentSubUI = UIManager.Instance.OpenUI<CharacterUI>();
                Debug.Log("打开阵容界面");
                break;
            case 2:
                // currentSubUI = UIManager.Instance.OpenUI<BagUI>();
                Debug.Log("打开同盟界面");
                break;
            case 3:
                // currentSubUI = UIManager.Instance.OpenUI<QuestUI>();
                Debug.Log("打开福利界面");
                break;
            case 4:
                //currentSubUI = UIManager.Instance.OpenWindow<PlaceWin>();
                Debug.Log("打开冒险界面");
                BattleWin battleWin = UIManager.Instance.OpenWindow<BattleWin>();
                currentSubUI = battleWin;
                break;
            default:
                Debug.LogWarning("未知的标签索引: " + index);
                break;
        }
    }
}