yyl
9 天以前 b0ce53afc8346e1391807618e64bdb225e83fd64
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
using System;
using UnityEngine;
 
/// <summary>
/// 挑战标签页按钮的逻辑处理基类
/// 挂在与 ChallengeTabButton 同一个GameObject上
/// </summary>
[RequireComponent(typeof(ChallengeTabButton))]
public abstract class BaseChallengeTabHandler : MonoBehaviour
{
    protected ChallengeTabButton tabButton;
    private ChallengeTabButton.DisplayData displayData;
 
    protected virtual void Awake()
    {
        tabButton = GetComponent<ChallengeTabButton>();
        // 初始化一次 DisplayData,之后只修改变化的字段
        displayData = new ChallengeTabButton.DisplayData
        {
            Index = GetIndex(),
            RedpointId = GetRedpointId(),
            OpenState = GetOpenState(),
            FuncId = GetFuncId(),
            OnClickAction = GetOnClickAction()
        };
    }
 
    /// <summary>
    /// 订阅事件 (由 ChallengeTabWin 在 OnPreOpen 时调用)
    /// </summary>
    public virtual void SubscribeEvents()
    {
        // 订阅全局刷新事件
        TimeMgr.Instance.OnDayEvent += Refresh;
        FuncOpen.Instance.OnFuncStateChangeEvent += OnFuncStateChange;
        // 订阅此按钮特定的事件
        SubscribeToSpecificEvents();
    }
 
    /// <summary>
    /// 取消订阅事件 (由 ChallengeTabWin 在 OnPreClose 时调用)
    /// </summary>
    public virtual void UnsubscribeEvents()
    {
        TimeMgr.Instance.OnDayEvent -= Refresh;
        FuncOpen.Instance.OnFuncStateChangeEvent -= OnFuncStateChange;
        UnsubscribeFromSpecificEvents();
    }
 
    /// <summary>
    /// 刷新UI显示 (由 ChallengeTabWin 或事件回调调用)
    /// </summary>
    public void Refresh()
    {
        if (tabButton == null)
            return;
        displayData.CountInfo = GetCountInfo();
        tabButton.Display(displayData);
    }
 
    private void OnFuncStateChange(int funcId)
    {
        if (GetOpenState() == 0 && funcId == GetFuncId())
        {
            Refresh();
        }
    }
 
    /// <summary>
    /// 获取Tab的索引(用于Icon和Name)
    /// </summary>
    protected abstract int GetIndex();
 
    /// <summary>
    /// 获取开启方式 (0=FuncID, 1=活动)
    /// </summary>
    protected abstract int GetOpenState();
 
    /// <summary>
    /// 获取功能ID
    /// </summary>
    protected abstract int GetFuncId();
 
    /// <summary>
    /// 获取红点ID
    /// </summary>
    protected abstract int GetRedpointId();
 
    /// <summary>
    /// 获取数量的显示文本
    /// </summary>
    protected abstract string GetCountInfo();
 
    /// <summary>
    /// 获取按钮点击时触发的 *具体* 业务逻辑
    /// </summary>
    protected abstract Action GetOnClickAction();
 
    /// <summary>
    /// 订阅此Tab特有的事件
    /// </summary>
    protected abstract void SubscribeToSpecificEvents();
 
    /// <summary>
    /// 取消订阅此Tab特有的事件
    /// </summary>
    protected abstract void UnsubscribeFromSpecificEvents();
}