重做粗略选曲界面 浮动小游戏可开关
修复一些小bug和加入部分新功能
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class SongSelectUI : MonoBehaviour
|
||||
{
|
||||
@@ -9,9 +10,9 @@ public class SongSelectUI : MonoBehaviour
|
||||
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)
|
||||
@@ -21,77 +22,80 @@ public class SongSelectUI : MonoBehaviour
|
||||
}
|
||||
|
||||
button_Main.onClick.AddListener(
|
||||
() => SceneManager.LoadScene(ui_Main_Scene_Name, LoadSceneMode.Single));
|
||||
DisplaySongs();
|
||||
() => SceneManager.LoadScene(ui_Main_Scene_NAME(), LoadSceneMode.Single));
|
||||
// Do not populate the song list at start. DLC buttons will request songs when clicked.
|
||||
}
|
||||
|
||||
// 显示所有歌曲
|
||||
void DisplaySongs()
|
||||
// 显示所有歌曲 (kept for manual use)
|
||||
public void DisplaySongs()
|
||||
{
|
||||
// 清空现有按钮
|
||||
foreach (Transform child in contentPanel)
|
||||
{
|
||||
Destroy(child.gameObject);
|
||||
}
|
||||
ClearSongList();
|
||||
|
||||
// 为每首歌曲创建一个按钮
|
||||
foreach (SongData song in songList.songs)
|
||||
{
|
||||
GameObject songButtonObj = Instantiate(songButtonPrefab, contentPanel);
|
||||
songButtonObj.transform.SetParent(contentPanel, false); // 确保 UI 结构正确
|
||||
|
||||
// 获取 SongButton 组件
|
||||
SongButton songButton = songButtonObj.GetComponent<SongButton>();
|
||||
if (songButton != null)
|
||||
{
|
||||
// 调用 SetButtonData 赋值
|
||||
songButton.SetButtonData(song.songName, song.personalRecord, song.difficultyID, song.illustration, song.songID);
|
||||
Debug.Log(" SetButtonData 被调用,songID: " + song.songID);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError(" SongButton 组件未找到!");
|
||||
}
|
||||
|
||||
// **获取组件**
|
||||
Image songImage = songButtonObj.GetComponentInChildren<Image>(); // 获取图片组件
|
||||
TMP_Text[] texts = songButtonObj.GetComponentsInChildren<TMP_Text>();
|
||||
|
||||
if (songImage != null && song.illustration != null)
|
||||
{
|
||||
songImage.sprite = song.illustration; // 设置歌曲图片
|
||||
songImage.enabled = true;
|
||||
Debug.Log(" 设置图片:" + song.songName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning(" Illustration 为空:" + song.songName);
|
||||
}
|
||||
|
||||
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();
|
||||
Debug.Log(" 设置文本:" + song.songName);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("TextMeshPro 组件未找到");
|
||||
}
|
||||
|
||||
// **添加按钮事件**
|
||||
Button button = songButtonObj.GetComponent<Button>();
|
||||
button.onClick.AddListener(() => songButton.OnSongButtonClick());
|
||||
InstantiateSongButton(song);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 处理点击事件
|
||||
void OnSongButtonClicked(SongData song)
|
||||
// Instantiate from a provided list (used by dlcButton to show DLC songs)
|
||||
public void DisplaySongsFromList(List<SongData> list)
|
||||
{
|
||||
ClearSongList();
|
||||
|
||||
Debug.Log("🎵 选择歌曲:" + song.songName);
|
||||
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<SongButton>();
|
||||
if (songButton != null)
|
||||
{
|
||||
songButton.SetButtonData(song.songName, song.personalRecord, song.difficultyID, song.illustration, song.songID);
|
||||
}
|
||||
|
||||
Image songImage = songButtonObj.GetComponentInChildren<Image>();
|
||||
TMP_Text[] texts = songButtonObj.GetComponentsInChildren<TMP_Text>();
|
||||
|
||||
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<Button>();
|
||||
if (button != null && songButton != null)
|
||||
button.onClick.AddListener(() => songButton.OnSongButtonClick());
|
||||
}
|
||||
|
||||
// helper to avoid hardcoding scene name string in delegate - keeps serializer-friendly field
|
||||
private string ui_Main_Scene_NAME() => ui_Main_Scene_Name;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user