using UnityEngine; using UnityEngine.UI; using TMPro; using UnityEngine.SceneManagement; using System.Collections.Generic; public class SongSelectUI : MonoBehaviour { public SongList songList; // 存储歌曲列表的 ScriptableObject public GameObject songButtonPrefab; // 歌曲按钮的 Prefab (Button) public Transform contentPanel; // 滚动视图的 Content 面板 [SerializeField] Button button_Main; [SerializeField] string ui_Main_Scene_Name = "UI_UI"; void Start() { if (songList == null || songButtonPrefab == null || contentPanel == null) { Debug.LogError("请在 Inspector 中分配所有字段!"); return; } button_Main.onClick.AddListener( () => SceneManager.LoadScene(ui_Main_Scene_NAME(), LoadSceneMode.Single)); // Do not populate the song list at start. DLC buttons will request songs when clicked. } // 显示所有歌曲 (kept for manual use) public void DisplaySongs() { ClearSongList(); foreach (SongData song in songList.songs) { InstantiateSongButton(song); } } // Instantiate from a provided list (used by dlcButton to show DLC songs) public void DisplaySongsFromList(List list) { ClearSongList(); if (list == null) return; foreach (var song in list) InstantiateSongButton(song); } private void ClearSongList() { if (contentPanel == null) return; for (int i = contentPanel.childCount - 1; i >= 0; i--) { #if UNITY_EDITOR if (!Application.isPlaying) DestroyImmediate(contentPanel.GetChild(i).gameObject); else Destroy(contentPanel.GetChild(i).gameObject); #else Destroy(contentPanel.GetChild(i).gameObject); #endif } } private void InstantiateSongButton(SongData song) { if (songButtonPrefab == null || contentPanel == null) return; GameObject songButtonObj = Instantiate(songButtonPrefab, contentPanel); songButtonObj.transform.SetParent(contentPanel, false); SongButton songButton = songButtonObj.GetComponent(); if (songButton != null) { songButton.SetButtonData(song.songName, song.personalRecord, song.difficultyID, song.illustration, song.songID); } Image songImage = songButtonObj.GetComponentInChildren(); TMP_Text[] texts = songButtonObj.GetComponentsInChildren(); if (songImage != null && song.illustration != null) { songImage.sprite = song.illustration; songImage.enabled = true; } if (texts.Length >= 4) { texts[0].text = song.songName; texts[1].text = "Record : " + song.personalRecord; texts[3].text = "" + song.difficultyID; texts[2].text = "SongID : " + song.songID.ToString(); } Button button = songButtonObj.GetComponent