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
using System;
using Cysharp.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
 
//方案预设: 多方案选择组件
//控制显隐: 1.未开启时全不显示
//          2.开启后显示已解锁 + 未解锁 3+0
//a. 按元宝解锁的为逐一开启,默认开启x个,第x+1个显示条件需要【流派预设】功能开启, 可解锁后逐一保留一个锁住的(直到全开启);
//b. 按功能条件开启的,如果默认只开其一个方案的情况不显示,达到下一个方案的条件满足后全显示;默认大于1个的情况直接全显示
//          3.当小于4个的时候,没有下拉显示,如果可以超过4个的显示下拉
//          4.当大于4个的时候,选择超过方案4时,第四个显示为选中的方案; 否则重开的时候默认显示1-4 不记录历史点击
//          5.名字展开分两种情况,外层功能默认不展开,点击后切换展开状态;流派预设界面里只有展开状态不可收缩
 
public class FuncPresetChooseCells : MonoBehaviour
{
    [SerializeField] HorizontalLayoutGroup layoutGroup;
    [SerializeField] FuncPresetChooseCell[] cells;
    [SerializeField] Button unFoldBtn;   //展开更多
    [SerializeField] Image redImg;
    
    //被选中的方案会显示在外层,如果不是第四个则第四个会显示在最上面
 
    [SerializeField] Transform moreCellObj;
    [SerializeField] FuncPresetChooseMoreCell[] moreCells;
    [SerializeField] Canvas canvas;
    bool forceUnFold = false; //强制展开,不能收缩; 流派界面的需求
    [NonSerialized] public int unFoldID = 0;    //当前展开的方案ID,在选中的情况下才生效
    int curBattleType;
    int curFuncType;
 
 
    /// <summary>
    /// 显示方案预设
    /// </summary>
    /// <param name="battleType">主线战斗 其他防守</param>
    /// <param name="funcType">功能类型</param>
    /// <param name="unFoldID">展开的方案ID,默认0,不展开</param>
    /// <param name="forceUnFold"> 强制展开,不能收缩</param>
    public void Display(int battleType, int funcType, bool _forceUnFold = false)
    {
        forceUnFold = _forceUnFold;
        curBattleType = battleType;
        curFuncType = funcType;
 
        var selectID = FuncPresetManager.Instance.GetFuncPresetIDByBattleType(battleType, funcType);
        if (unFoldID != 0 && selectID != unFoldID)
        {
            //其他地方切换了方案,需要验证当前展开是不是同一个
            unFoldID = 0;
        }
        var showCount = FuncPresetManager.Instance.GetShowFuncPresetCount(funcType);
        unFoldBtn.SetActive(showCount > 4);
        unFoldBtn.AddListener(() =>
        {
            moreCellObj.SetActive(true);
        });
 
        for (int i = 0; i < cells.Length; i++)
        {
            if (i < showCount)
            {
                cells[i].SetActive(true);
                if (i < 3)
                {
                    var _unFoldState = forceUnFold ? i + 1 == selectID : i + 1 == unFoldID;
                    cells[i].Display(battleType, funcType, i + 1, _unFoldState);
                }
                else
                {
                    var _unFoldState = forceUnFold ? selectID >= 4: (selectID > 4 ? selectID : 4) == unFoldID;
                    //第四个动态变化
                    cells[i].Display(battleType, funcType, selectID > 4 ? selectID : 4, _unFoldState);
                }
            }
            else
            {
                cells[i].SetActive(false);
            }
        }
 
        var showMoreCount = showCount - 4;
        for (int i = 0; i < moreCells.Length; i++)
        {
            if (i < showMoreCount)
            {
                moreCells[i].SetActive(true);
                var id = i + 5;
                if (i == 0 && selectID > 4)
                {
                    id = 4;
                }
                moreCells[i].Display(battleType, funcType, id);
            }
            else
            {
                moreCells[i].SetActive(false);
            }
        }
 
 
        redImg.SetActive(FuncPresetManager.Instance.GetNeedShowRedID(funcType) > 4);
 
        UIUtility.ForceRefreshLayout(this.transform).Forget();
    }
 
    void OnEnable()
    {
        canvas.sortingLayerName = "UI";
        FuncPresetManager.Instance.OnSelectPresetEvent += OnSelectPresetEvent;
    }
 
    void OnDisable()
    {
        unFoldID = 0;
        FuncPresetManager.Instance.OnSelectPresetEvent -= OnSelectPresetEvent;
    }
 
    void OnSelectPresetEvent(int battleType, int _funcType, int id, bool isUnFold)
    {
        if (battleType != curBattleType || curFuncType != _funcType)
            return;
        Display(curBattleType, _funcType, forceUnFold);
    }
 
    //默认是居中靠右
    public void ChangeAlignment(TextAnchor type)
    {
        layoutGroup.childAlignment = type;
    }
 
}