using UnityEngine; using UnityEngine.UI; using System.Collections; using System.Collections.Generic; public class loadDlcListPrefab : MonoBehaviour { private static readonly bool VerboseLogs = false; private const string RuntimeInstalledColumnName = "Installed DLC"; [System.Serializable] public class Column { public string columnName; public List dlcList = new List(); } [Header("Inspector")] public List columns = new List(); [Header("Runtime DLC")] [SerializeField] bool includeRuntimeInstalledDlcs = true; [SerializeField] string runtimeInstalledColumnName = RuntimeInstalledColumnName; [Header("Inspector")] 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 } List displayColumns = BuildDisplayColumns(); if (displayColumns == null) return; for (int colIndex = 0; colIndex < displayColumns.Count; colIndex++) { var col = displayColumns[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(); if (VerboseLogs) Debug.Log($"loadDlcListPrefab: restored and clicked saved dlc button index={savedIndex} name={savedBtn.gameObject.name}"); } else { if (VerboseLogs) Debug.LogWarning($"loadDlcListPrefab: saved dlc button at index {savedIndex} was null"); } } else { if (VerboseLogs) Debug.LogWarning("loadDlcListPrefab: no dlc buttons found after RefreshUI"); } } private List BuildDisplayColumns() { List result = new List(); HashSet knownDlcs = new HashSet(); if (columns != null) { for (int i = 0; i < columns.Count; i++) { Column source = columns[i]; if (source == null) { continue; } Column copy = new Column { columnName = source.columnName, dlcList = new List() }; if (source.dlcList != null) { for (int j = 0; j < source.dlcList.Count; j++) { dlcData item = source.dlcList[j]; if (item == null) { continue; } copy.dlcList.Add(item); knownDlcs.Add(item); } } result.Add(copy); } } if (!includeRuntimeInstalledDlcs) { return result; } dlcData[] runtimeDlcs = DlcRuntimeRegistry.GetAllDlcs(); if (runtimeDlcs == null || runtimeDlcs.Length == 0) { return result; } Column runtimeColumn = new Column { columnName = string.IsNullOrWhiteSpace(runtimeInstalledColumnName) ? RuntimeInstalledColumnName : runtimeInstalledColumnName, dlcList = new List() }; for (int i = 0; i < runtimeDlcs.Length; i++) { dlcData dlc = runtimeDlcs[i]; if (dlc == null || knownDlcs.Contains(dlc)) { continue; } runtimeColumn.dlcList.Add(dlc); } if (runtimeColumn.dlcList.Count > 0) { result.Add(runtimeColumn); } return result; } }