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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
 
//武将筛选
public class HeroSelectBehaviour : MonoBehaviour
{
    [SerializeField] Transform unFoldForm;  //展开后的容器
    [SerializeField] Button foldBtn;    //收起按钮
    [SerializeField] Transform foldForm;    //收起容器
    [SerializeField] Button unFoldBtn;  //展开按钮
    [SerializeField] Toggle[] jobsBtn;
    [SerializeField] Toggle[] countrysBtn;
    [SerializeField] Toggle[] hurtTypeBtn;
    [SerializeField] Toggle[] specialType6Btn;
    [SerializeField] Transform specialTypeMoreRect;
    [SerializeField] ClickScreenOtherSpaceEvent clickScreenOtherSpaceEvent;
    [SerializeField] Button resetBtn;
 
    Toggle[] specialTypeMoreBtn;
 
    int m_Job = 0;  //职业 多选项按位存储
    int m_Country = 0;  //国家 多选项位按存储
    int m_HurtType = 0;  //伤害类型 多选项位按存储
    int m_SpecialType6 = 0;  //6大战斗属性 多选项位按存储
    int m_SpecialTypeMore = 0;  //特殊属性 多选项位按存储
    int foldState = 0;  //0 收起,1 展开
 
    //点击按钮需通知响应外部事件
    private Action<List<int>> selectAction;
 
 
 
 
    void Awake()
    {
        foldBtn.AddListener(() =>
        {
            foldState = 0;
            RefreshFolState();
        });
        unFoldBtn.AddListener(() =>
        {
            foldState = 1;
            RefreshFolState();
        });
 
        specialTypeMoreBtn = specialTypeMoreRect.GetComponentsInChildren<Toggle>();
 
        // 初始化特殊属性按钮的显示和文本
        for (int i = 0; i < specialTypeMoreBtn.Length; i++)
        {
            if (i < HeroUIManager.Instance.heroSpecialAttrsForSelect.Count)
            {
                int index = HeroUIManager.Instance.heroSpecialAttrsForSelect[i];
                specialTypeMoreBtn[i].SetActive(true);
                specialTypeMoreBtn[i].GetComponentInChildren<Text>().text = Language.Get($"HeroSpecialty2_{index}");
            }
            else
            {
                specialTypeMoreBtn[i].SetActive(false);
            }
        }
 
        // 添加所有Toggle监听器
        AddAllListeners();
 
        clickScreenOtherSpaceEvent.AddListener(() =>
        {
            if (foldState == 0)
                return;
            foldBtn.onClick.Invoke();
        });
 
        resetBtn.AddListener(Reset);
 
    }
 
    // 移除所有Toggle监听器
    void RemoveAllListeners()
    {
        for (int i = 0; i < jobsBtn.Length; i++)
        {
            jobsBtn[i].onValueChanged.RemoveAllListeners();
        }
        for (int i = 0; i < countrysBtn.Length; i++)
        {
            countrysBtn[i].onValueChanged.RemoveAllListeners();
        }
        for (int i = 0; i < hurtTypeBtn.Length; i++)
        {
            hurtTypeBtn[i].onValueChanged.RemoveAllListeners();
        }
        for (int i = 0; i < specialType6Btn.Length; i++)
        {
            specialType6Btn[i].onValueChanged.RemoveAllListeners();
        }
        for (int i = 0; i < specialTypeMoreBtn.Length; i++)
        {
            specialTypeMoreBtn[i].onValueChanged.RemoveAllListeners();
        }
    }
 
    // 添加所有Toggle监听器
    void AddAllListeners()
    {
        for (int i = 0; i < jobsBtn.Length; i++)
        {
            int index = i + 1;
            jobsBtn[i].onValueChanged.AddListener((bool value) =>
            {
                m_Job = value ? m_Job | (1 << index) : m_Job & ~(1 << index);
                selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
            });
        }
 
        for (int i = 0; i < countrysBtn.Length; i++)
        {
            int index = i + 1;
            countrysBtn[i].onValueChanged.AddListener((bool value) =>
            {
                m_Country = value ? m_Country | (1 << index) : m_Country & ~(1 << index);
                selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
            });
        }
 
        for (int i = 0; i < hurtTypeBtn.Length; i++)
        {
            int index = i + 1;
            hurtTypeBtn[i].onValueChanged.AddListener((bool value) =>
            {
                m_HurtType = value ? m_HurtType | (1 << index) : m_HurtType & ~(1 << index);
                selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
            });
        }
 
        for (int i = 0; i < specialType6Btn.Length; i++)
        {
            int index = i + 1;
            specialType6Btn[i].onValueChanged.AddListener((bool value) =>
            {
                m_SpecialType6 = value ? m_SpecialType6 | (1 << index) : m_SpecialType6 & ~(1 << index);
                selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
            });
        }
 
        for (int i = 0; i < specialTypeMoreBtn.Length; i++)
        {
            if (i < HeroUIManager.Instance.heroSpecialAttrsForSelect.Count)
            {
                int index = HeroUIManager.Instance.heroSpecialAttrsForSelect[i];
                specialTypeMoreBtn[i].onValueChanged.AddListener((bool value) =>
                {
                    m_SpecialTypeMore = value ? m_SpecialTypeMore | (1 << index) : m_SpecialTypeMore & ~(1 << index);
                    selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
                });
            }
        }
    }
 
    /// <summary>
    /// 国家职业筛选
    /// </summary>
    /// <param name="state"> 0收起,1展开</param>
    /// <param name="onRefresh"> 点击按钮需通知响应外部事件</param>
    /// 回调参数: 职业,国家,伤害类型,6大战斗属性,特殊属性
    public void Display(int state, Action<List<int>> onRefresh)
    {
        foldState = state;
 
        RefreshFolState();
        selectAction = onRefresh;
        Reset();
 
    }
 
 
 
    //刷新展开收起状态
    void RefreshFolState()
    {
        unFoldForm.SetActive(foldState == 1);
        foldForm.SetActive(foldState == 0);
        if (foldState == 1)
        {
            UIHelper.ForceRefreshLayout(unFoldForm).Forget();
        }
    }
 
    public static HeroSelectBehaviour Create(Transform heroSelectBehaviour)
    {
        var instanceGO = UIUtility.CreateWidget("HeroSelectBehaviour", "HeroSelectBehaviour");
        instanceGO.transform.SetParentEx(heroSelectBehaviour, Vector3.zero, Quaternion.identity, Vector3.one);
        return instanceGO.GetComponent<HeroSelectBehaviour>();
    }
 
    void Reset()
    {
        // 暂时移除所有监听器避免重置时多次触发
        RemoveAllListeners();
        
        m_Job = 0;
        m_Country = 0;
        m_HurtType = 0;
        m_SpecialType6 = 0;
        m_SpecialTypeMore = 0;
 
        for (int i = 0; i < jobsBtn.Length; i++)
        {
            jobsBtn[i].isOn = false;
        }
        for (int i = 0; i < countrysBtn.Length; i++)
        {
            countrysBtn[i].isOn = false;
        }
        for (int i = 0; i < hurtTypeBtn.Length; i++)
        {
            hurtTypeBtn[i].isOn = false;
        }
        for (int i = 0; i < specialType6Btn.Length; i++)
        {
            specialType6Btn[i].isOn = false;
        }
        for (int i = 0; i < specialTypeMoreBtn.Length; i++)
        {
            specialTypeMoreBtn[i].isOn = false;
        }
 
        // 重新添加监听器
        AddAllListeners();
        selectAction?.Invoke(new List<int>() { m_Job, m_Country, m_HurtType, m_SpecialType6, m_SpecialTypeMore });
    }
}