Files
bansonic_beta_main/Assets/songsDatabase/loadDlcListPrefab.cs
T

142 lines
5.1 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class loadDlcListPrefab : MonoBehaviour
{
[System.Serializable]
public class Column
{
public string columnName;
public List<dlcData> dlcList = new List<dlcData>();
}
[Header("À¸Ä¿Áбí")]
public List<Column> columns = new List<Column>();
[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<columnCardPrefab>();
if (card != null && card.columnNameText != null)
{
card.columnNameText.text = col.columnName ?? "(Unnamed)";
}
else
{
var txt = headerGO.transform.Find("ColumnNameText")?.GetComponent<Text>();
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<dlcButton>();
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<RectTransform>();
if (rt != null) UnityEngine.UI.LayoutRebuilder.ForceRebuildLayoutImmediate(rt);
// After all buttons are instantiated, have each dlcButton resolve its dlcData (deferred lookup)
var allButtons = dlc_scContent.GetComponentsInChildren<dlcButton>(true);
foreach (var b in allButtons)
{
if (b != null)
b.ResolveDlcData();
}
// 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");
}
}
}