using Assets.MyGame.Scripts.Net; using Assets.MyGame.Scripts.Net.APIRequest.Objs; using Assets.MyGame.Scripts.Net.APIRequest.Params; using Assets.MyGame.Scripts.Net.APIResponses; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; /// 公告面板 public class UIHome_BY_AnnouncementPanel : SubUIBase { public AnnouncementItem announcementItemPrefab; public Transform announcementItemParent; public TextMeshProUGUI tmp_TitleLabel; public TextMeshProUGUI tmp_Content; private List noticeList; protected override void OnShow(object args = null) { base.OnShow(args); LoadNoticeList(); } private void LoadNoticeList() { APIRequestList.GetNoticeList(new NoticeListObj { keyWord = "", pageIndex = 0, pageSize = 50 }, list => { if (list == null) return; noticeList = list; // 清除旧列表 for (int i = announcementItemParent.childCount - 1; i >= 0; i--) Destroy(announcementItemParent.GetChild(i).gameObject); foreach (var item in list) { var obj = Instantiate(announcementItemPrefab.gameObject, announcementItemParent); var tempItem = obj.GetComponent(); tempItem.Init(this, item.title, item.createTime); tempItem.tMP_Header.text = item.title; string noticeId = item.id; obj.GetComponent().onValueChanged.RemoveAllListeners(); obj.GetComponent().onValueChanged.AddListener((isOn) => { if (isOn) OnNoticeClick(noticeId); }); } // 默认选中第一条公告 if (list.Count > 0) { var firstToggle = announcementItemParent.GetChild(0).GetComponent(); firstToggle.isOn = true; OnNoticeClick(list[0].id); } }); } private void OnNoticeClick(string noticeId) { APIRequestList.GetNotice(new NoticeIdParam { id = noticeId }, notice => { if (notice == null) return; if (tmp_TitleLabel != null) tmp_TitleLabel.text = notice.title; if (tmp_Content != null) tmp_Content.text = notice.content; }); } }