using UnityEngine; public class TimingGiftCell : MonoBehaviour { private void Awake() { //如果有需要按钮点击逻辑,在外层创建,此处不处理点击逻辑 LoadPrefab(); button.SetListener(() => { UIManager.Instance.OpenWindow(type); }); } ButtonEx m_button; public ButtonEx button { get { if (m_button == null) { m_button = this.GetComponent("TimingGiftCell"); } return m_button; } } TextEx m_Time; private TextEx time { get { if (m_Time == null) { m_Time = this.GetComponent("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(); } }