using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; public class loadDlcListPrefab : MonoBehaviour { [System.Serializable] public class Column { public string columnName; public List dlcList = new List(); } [Header("栏目列表")] public List columns = new List(); [Header("UI 预制体")] public GameObject dlc_scContent; public GameObject columnPrefab; public GameObject dlcButtonPrefab; void Start() { RefreshUI(); } public void RefreshUI() { if (dlc_scContent == null) return; for (int i = dlc_scContent.transform.childCount - 1; i >= 0; i--) { var c = dlc_scContent.transform.GetChild(i).gameObject; #if UNITY_EDITOR if (!Application.isPlaying) DestroyImmediate(c); else Destroy(c); #else Destroy(c); #endif } if (columns == null) return; for (int colIndex = 0; colIndex < columns.Count; colIndex++) { var col = columns[colIndex]; if (columnPrefab != null) { var headerGO = Instantiate(columnPrefab, dlc_scContent.transform); headerGO.name = "Column_" + colIndex; var card = headerGO.GetComponent(); if (card != null && card.columnNameText != null) { card.columnNameText.text = col.columnName ?? "(Unnamed)"; } else { var txt = headerGO.transform.Find("ColumnNameText")?.GetComponent(); if (txt != null) txt.text = col.columnName ?? "(Unnamed)"; } } if (col.dlcList == null) continue; for (int entryIndex = 0; entryIndex < col.dlcList.Count; entryIndex++) { var dlc = col.dlcList[entryIndex]; if (dlcButtonPrefab == null) { Debug.LogWarning("dlcButtonPrefab is not assigned"); break; } var itemGO = Instantiate(dlcButtonPrefab, dlc_scContent.transform); itemGO.name = $"DLC_Col{colIndex}_Item{entryIndex}"; var btn = itemGO.GetComponent(); if (btn != null) { if (dlc != null) { if (btn.dlc_profileImgae != null) { btn.dlc_profileImgae.sprite = dlc.dlc_image; btn.dlc_profileImgae.enabled = dlc.dlc_image != null; } if (btn.dlcName != null) btn.dlcName.text = dlc.dlcName ?? "(Unnamed)"; if (btn.dlcID != null) btn.dlcID.text = dlc.dlcID.ToString(); if (btn.dlcProducer != null) btn.dlcProducer.text = dlc.dlcProducer ?? ""; if (btn.dlcPubliushDate != null) btn.dlcPubliushDate.text = dlc.dlcPublishDate ?? ""; if (btn.dlcDescription != null) btn.dlcDescription.text = dlc.dlcDescription ?? ""; } else { if (btn.dlcName != null) btn.dlcName.text = "(None)"; if (btn.dlcID != null) btn.dlcID.text = ""; if (btn.dlc_profileImgae != null) btn.dlc_profileImgae.enabled = false; if (btn.dlcDescription != null) btn.dlcDescription.text = ""; } } } } var rt = dlc_scContent.GetComponent(); if (rt != null) UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(rt); var allButtons = dlc_scContent.GetComponentsInChildren(true); StartCoroutine(ResolveAndRestore(allButtons)); } private IEnumerator ResolveAndRestore(dlcButton[] allButtons) { if (allButtons != null) { for (int i = 0; i < allButtons.Length; i++) { var b = allButtons[i]; if (b != null) { yield return b.ResolveDlcDataAsync(); } } } // Let layout and any fades settle for a frame yield return null; // Restore last selected DLC button from PlayerPrefs and simulate a click on it int savedIndex = PlayerPrefs.GetInt("dlc_selected_index", 0); if (allButtons != null && allButtons.Length > 0) { if (savedIndex < 0) savedIndex = 0; if (savedIndex >= allButtons.Length) savedIndex = 0; var savedBtn = allButtons[savedIndex]; if (savedBtn != null) { // simulate a user clicking the button: this will Select() and display its songs savedBtn.OnButtonClicked_ShowSongs(); Debug.Log($"loadDlcListPrefab: restored and clicked saved dlc button index={savedIndex} name={savedBtn.gameObject.name}"); } else { Debug.LogWarning($"loadDlcListPrefab: saved dlc button at index {savedIndex} was null"); } } else { Debug.LogWarning("loadDlcListPrefab: no dlc buttons found after RefreshUI"); } } }