hch
10 小时以前 bc6f633a2f3cfc01122d8fd4452f69313ddcb32b
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
using UnityEngine;
 
public class TimingGiftCell : MonoBehaviour
{
    private void Awake()
    {
        //如果有需要按钮点击逻辑,在外层创建,此处不处理点击逻辑
        LoadPrefab();
        button.SetListener(() =>
        {
            UIManager.Instance.OpenWindow<TimingGiftWin>(type);
        });
    }
 
    ButtonEx m_button;
    public ButtonEx button
    {
        get
        {
            if (m_button == null)
            {
                m_button = this.GetComponent<ButtonEx>("TimingGiftCell");
            }
            return m_button;
        }
    }
 
    TextEx m_Time;
    private TextEx time
    {
        get
        {
            if (m_Time == null)
            {
                m_Time = this.GetComponent<TextEx>("TimingGiftCell/bgImage/timeImage/timeText");
            }
            return m_Time;
        }
    }
 
    public int type;
    GameObject prefab;
    TimingGiftManager manager { get { return TimingGiftManager.Instance; } }
    void OnEnable()
    {
        FuncOpen.Instance.OnFuncStateChangeEvent += OnFuncStateChangeEvent;
        manager.OnShowGiftIdListAddEvent += OnShowGiftIdListAddEvent;
        manager.OnRemoveExpiredEvent += OnRemoveExpiredEvent;
        GlobalTimeEvent.Instance.secondEvent += OnSecondEvent;
    }
 
    void OnDisable()
    {
        FuncOpen.Instance.OnFuncStateChangeEvent -= OnFuncStateChangeEvent;
        manager.OnShowGiftIdListAddEvent -= OnShowGiftIdListAddEvent;
        manager.OnRemoveExpiredEvent -= OnRemoveExpiredEvent;
        GlobalTimeEvent.Instance.secondEvent -= OnSecondEvent;
    }
 
    private void OnFuncStateChangeEvent(int obj)
    {
        if (obj == (int)FuncOpenEnum.TimingGift)
        {
            Display();
        }
    }
 
    private void OnSecondEvent()
    {
        Display();
    }
 
    private void OnShowGiftIdListAddEvent()
    {
        Display();
    }
    private void OnRemoveExpiredEvent()
    {
        Display();
    }
 
    public void Display()
    {
        //配0时是主界面入口
        if (type == 0)
        {
            var list = manager.GetCurrectTimingGiftIdList();
            bool isNullOrEmpty = list.IsNullOrEmpty();
            this.SetActive(!isNullOrEmpty);
            if (isNullOrEmpty)
                return;
            int id = list[0];
            if (!TimingGiftConfig.TryGetTimingGiftConfig(id, out var config))
                return;
            RefreshTime(config.GiftType);
            return;
        }
        RefreshTime(type);
    }
 
    private void RefreshTime(int type)
    {
        bool isShow = manager.IsShowGiftIdListHasType(type);
        this.SetActive(isShow);
        if (isShow)
        {
            int times = manager.GetRemainingSecondsByType(type);
            time.text = times <= 0 ? Language.Get("TimingGift04") : TimeUtility.SecondsToHMS(times);
            time.colorType = times <= 0 ? TextColType.Red : TextColType.LightGreen;
        }
    }
 
    protected void LoadPrefab()
    {
        if (prefab != null)
            return;
        var tmp = transform.Find("TimingGiftCell");
 
        if (tmp != null)
        {
            prefab = tmp.gameObject;
            return;
        }
        prefab = UIUtility.CreateWidget("TimingGiftCell", "TimingGiftCell");
 
        prefab.transform.SetParentEx(this.transform, Vector3.zero, Quaternion.identity, Vector3.one);
        prefab.transform.SetAsFirstSibling();
 
    }
 
    public void InitUI()
    {
        LoadPrefab();   //存在被卸载的可能,重新加载
        Display();
    }
 
}