UI update 02

This commit is contained in:
2026-06-30 21:21:30 +08:00
parent b2a1e307a4
commit f62f643cfc
1666 changed files with 211081 additions and 22799 deletions
@@ -7,6 +7,10 @@ using DG.Tweening;
public class SongButton : MonoBehaviour
{
private static readonly bool VerboseLogs = false;
private const string SongSelectedIndexPrefsPrefix = "song_selected_";
private const string SongSelectedIdPrefsPrefix = "song_selected_id_";
private const string SongSelectedDifficultyPrefsPrefix = "song_selected_difficulty_";
[Header("thisSong_so")]
public ScriptableObject thisSong_so;
[Header("Inspector")]
@@ -58,6 +62,23 @@ public class SongButton : MonoBehaviour
return song_songSerialID;
}
public static string BuildSongSelectedIndexPrefsKey(string dlcKey)
{
string safeDlcKey = string.IsNullOrEmpty(dlcKey) ? "default_0" : dlcKey;
return SongSelectedIndexPrefsPrefix + safeDlcKey;
}
public static string BuildSongSelectedIdPrefsKey(string dlcKey)
{
string safeDlcKey = string.IsNullOrEmpty(dlcKey) ? "default_0" : dlcKey;
return SongSelectedIdPrefsPrefix + safeDlcKey;
}
public static string BuildSongSelectedDifficultyPrefsKey(int songId)
{
return SongSelectedDifficultyPrefsPrefix + Mathf.Max(0, songId);
}
public void Start()
{
if (selected_boarder != null)
@@ -111,7 +132,7 @@ public class SongButton : MonoBehaviour
sd = SongDataLibrary.Instance.GetSongDataByID(songID) ?? sd;
if (sd != null)
{
Sprite resolvedBackground = sd.GetResolvedBackgroundPic();
Sprite resolvedIllustration = sd.GetResolvedIllustration();
Sprite resolvedProfile = sd.GetResolvedCardProfileImage();
if (songNameText != null) songNameText.text = sd.songName;
@@ -176,7 +197,7 @@ public class SongButton : MonoBehaviour
// Set rank images for the first three difficulties
SetDifficultyRankImages(sd);
if (buttonImage != null) buttonImage.sprite = resolvedBackground;
if (buttonImage != null) buttonImage.sprite = resolvedIllustration != null ? resolvedIllustration : songSprite;
if (profileImage != null)
{
@@ -252,10 +273,7 @@ public class SongButton : MonoBehaviour
{
selectedSong.EnsurePersistentDataLoaded();
selectedSong.GetAbsoluteHighestScore(out int bestTotal, out int bestDiff, out string bestDiffName);
if (bestDiff >= 0)
{
selectedSong.thisLevel_selectedDifficultyID = bestDiff;
}
selectedSong.thisLevel_selectedDifficultyID = ResolvePreferredDifficulty(selectedSong, bestDiff);
if (currentSelected != null && currentSelected != this)
{
currentSelected.FadeOutSelectedBorder();
@@ -273,9 +291,11 @@ public class SongButton : MonoBehaviour
// Save per-DLC selected song index to PlayerPrefs
string dlcKey = SongSelectUI.CurrentDlcKey;
if (string.IsNullOrEmpty(dlcKey)) dlcKey = "default_0";
string prefsKey = "song_selected_" + dlcKey;
string prefsKey = BuildSongSelectedIndexPrefsKey(dlcKey);
int index = GetIndexAmongSongButtons();
PlayerPrefs.SetInt(prefsKey, index);
PlayerPrefs.SetInt(BuildSongSelectedIdPrefsKey(dlcKey), selectedSong.songID);
PlayerPrefs.SetInt(BuildSongSelectedDifficultyPrefsKey(selectedSong.songID), selectedSong.thisLevel_selectedDifficultyID);
PlayerPrefs.Save();
if (VerboseLogs) Debug.Log($"SongButton: saved selected song index {index} for key {prefsKey}");
}
@@ -285,6 +305,82 @@ public class SongButton : MonoBehaviour
}
}
private int ResolvePreferredDifficulty(SongData selectedSong, int bestDiff)
{
if (selectedSong == null)
{
return 0;
}
int songId = selectedSong.songID;
string difficultyKey = BuildSongSelectedDifficultyPrefsKey(songId);
if (PlayerPrefs.HasKey(difficultyKey))
{
int savedDifficulty = PlayerPrefs.GetInt(difficultyKey, selectedSong.thisLevel_selectedDifficultyID);
if (IsDifficultyAvailable(selectedSong, savedDifficulty))
{
return savedDifficulty;
}
}
if (IsDifficultyAvailable(selectedSong, selectedSong.thisLevel_selectedDifficultyID))
{
return selectedSong.thisLevel_selectedDifficultyID;
}
if (bestDiff >= 0 && IsDifficultyAvailable(selectedSong, bestDiff))
{
return bestDiff;
}
return GetFirstAvailableDifficulty(selectedSong);
}
private bool IsDifficultyAvailable(SongData song, int difficulty)
{
if (song == null || difficulty < 0)
{
return false;
}
return song.GetResolvedChartFile(difficulty) != null;
}
private int GetFirstAvailableDifficulty(SongData song)
{
if (song == null)
{
return 0;
}
if (song.chartFiles != null)
{
for (int i = 0; i < song.chartFiles.Count; i++)
{
ChartFileEntry entry = song.chartFiles[i];
if (entry == null)
{
continue;
}
if (song.GetResolvedChartFile(entry.difficulty) != null)
{
return Mathf.Max(0, entry.difficulty);
}
}
}
for (int difficulty = 0; difficulty <= 3; difficulty++)
{
if (song.GetResolvedChartFile(difficulty) != null)
{
return difficulty;
}
}
return 0;
}
private int GetIndexAmongSongButtons()
{
// Use parent traversal to find only SongButton siblings, to avoid mismatch when other UI children exist.