长音符再修复 加入了关卡连接 优化了一些默认加载
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using UnityEngine.UI;
|
||||
using TMPro;
|
||||
using UnityEngine.SceneManagement;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class SongSelectUI : MonoBehaviour
|
||||
@@ -13,6 +14,12 @@ public class SongSelectUI : MonoBehaviour
|
||||
[SerializeField] Button button_Main;
|
||||
[SerializeField] string ui_Main_Scene_Name = "UI_UI";
|
||||
|
||||
// Current DLC key used for per-DLC saved song selection
|
||||
public static string CurrentDlcKey = "dlc_default_0";
|
||||
|
||||
// Track pending restore to avoid multiple overlapping restores
|
||||
private Coroutine restoreCoroutine;
|
||||
|
||||
void Start()
|
||||
{
|
||||
if (songList == null || songButtonPrefab == null || contentPanel == null)
|
||||
@@ -35,6 +42,9 @@ public class SongSelectUI : MonoBehaviour
|
||||
{
|
||||
InstantiateSongButton(song);
|
||||
}
|
||||
|
||||
if (restoreCoroutine != null) StopCoroutine(restoreCoroutine);
|
||||
restoreCoroutine = StartCoroutine(RestoreSelectedSongNextFrame());
|
||||
}
|
||||
|
||||
// Instantiate from a provided list (used by dlcButton to show DLC songs)
|
||||
@@ -45,6 +55,63 @@ public class SongSelectUI : MonoBehaviour
|
||||
if (list == null) return;
|
||||
foreach (var song in list)
|
||||
InstantiateSongButton(song);
|
||||
|
||||
// After populating, attempt to restore per-DLC selected song (next frame to avoid stale objects)
|
||||
if (restoreCoroutine != null) StopCoroutine(restoreCoroutine);
|
||||
restoreCoroutine = StartCoroutine(RestoreSelectedSongNextFrame());
|
||||
}
|
||||
|
||||
private IEnumerator RestoreSelectedSongNextFrame()
|
||||
{
|
||||
// Wait one frame so Destroy() in ClearSongList fully completes and stale SongButtons are gone.
|
||||
yield return null;
|
||||
RestoreSelectedSongForCurrentDlc();
|
||||
restoreCoroutine = null;
|
||||
}
|
||||
|
||||
private void RestoreSelectedSongForCurrentDlc()
|
||||
{
|
||||
// read saved index using CurrentDlcKey
|
||||
string dlcKey = (string.IsNullOrEmpty(CurrentDlcKey) ? "default_0" : CurrentDlcKey);
|
||||
string key = "song_selected_" + dlcKey;
|
||||
|
||||
// collect song buttons under contentPanel
|
||||
var allSongButtons = contentPanel.GetComponentsInChildren<SongButton>(true);
|
||||
if (allSongButtons == null || allSongButtons.Length == 0)
|
||||
{
|
||||
Debug.LogWarning("SongSelectUI.RestoreSelectedSongForCurrentDlc: no song buttons found to restore selection");
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure every DLC has a default persisted value (0)
|
||||
int savedIndex;
|
||||
if (!PlayerPrefs.HasKey(key))
|
||||
{
|
||||
PlayerPrefs.SetInt(key, 0);
|
||||
PlayerPrefs.Save();
|
||||
savedIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
savedIndex = PlayerPrefs.GetInt(key, 0);
|
||||
}
|
||||
|
||||
if (savedIndex < 0) savedIndex = 0;
|
||||
if (savedIndex >= allSongButtons.Length) savedIndex = 0;
|
||||
|
||||
var btn = allSongButtons[savedIndex];
|
||||
if (btn == null)
|
||||
{
|
||||
// hard fallback to first
|
||||
btn = allSongButtons[0];
|
||||
}
|
||||
|
||||
if (btn != null)
|
||||
{
|
||||
// simulate click
|
||||
btn.OnSongButtonClick();
|
||||
Debug.Log($"SongSelectUI: restored and clicked song index={savedIndex} for key={key}, totalButtons={allSongButtons.Length}");
|
||||
}
|
||||
}
|
||||
|
||||
private void ClearSongList()
|
||||
|
||||
Reference in New Issue
Block a user