using UnityEngine; using UnityEngine.UI; using System.Collections.Generic; public class uRecentGameHistory : MonoBehaviour { private const string SongResourcesPath = "song_songIndex"; private const int MaxVisibleRecordCount = 5; private const float DefaultPrefabScaleRatio = 1.25f; [Header("gameobjects")] public GameObject gameHistoryPrefab; public Transform gameHistoryParent; public Transform dGameHistoryParent; public GameObject more_gameHistory; [Header("layout")] public float prefabScaleRatio = DefaultPrefabScaleRatio; [Header("buttons")] public Button more_gameHistoryButton; public Button shut_gameHistoryButton; private SongData[] cachedSongs; private readonly List previewEntries = new List(); private readonly List detailEntries = new List(); private void Start() { BindButtons(); if (more_gameHistory != null) { more_gameHistory.SetActive(false); } RebuildHistory(); } private void OnDestroy() { if (more_gameHistoryButton != null) { more_gameHistoryButton.onClick.RemoveListener(OpenDetailedHistory); } if (shut_gameHistoryButton != null) { shut_gameHistoryButton.onClick.RemoveListener(CloseDetailedHistory); } } public void RebuildHistory() { if (gameHistoryParent == null || gameHistoryPrefab == null) { return; } ClearEntries(previewEntries, gameHistoryParent); cachedSongs = RuntimeResourcesCache.LoadSongsFromPath(SongResourcesPath); var records = RecentPlayHistoryStore.GetRecords(); if (records == null) { return; } int visibleCount = Mathf.Min(records.Count, MaxVisibleRecordCount); for (int i = 0; i < visibleCount; i++) { RecentPlayRecord record = records[i]; if (record == null) { continue; } GameObject instance = Instantiate(gameHistoryPrefab, gameHistoryParent); instance.transform.localScale *= prefabScaleRatio; previewEntries.Add(instance); ghPrefab controller = instance.GetComponent(); if (controller == null) { continue; } controller.Setup(record, FindSongById(record.songID)); } } private void OpenDetailedHistory() { if (more_gameHistory != null) { more_gameHistory.SetActive(true); } if (dGameHistoryParent == null || gameHistoryPrefab == null) { return; } ClearEntries(detailEntries, dGameHistoryParent); EnsureSongCache(); var records = RecentPlayHistoryStore.GetRecords(); if (records == null) { return; } for (int i = 0; i < records.Count; i++) { RecentPlayRecord record = records[i]; if (record == null) { continue; } GameObject instance = Instantiate(gameHistoryPrefab, dGameHistoryParent); detailEntries.Add(instance); ghPrefab controller = instance.GetComponent(); if (controller == null) { continue; } controller.Setup(record, FindSongById(record.songID)); } } private void CloseDetailedHistory() { ClearEntries(detailEntries, dGameHistoryParent); if (more_gameHistory != null) { more_gameHistory.SetActive(false); } } private SongData FindSongById(int songId) { if (songId <= 0 || cachedSongs == null) { return null; } for (int i = 0; i < cachedSongs.Length; i++) { SongData song = cachedSongs[i]; if (song != null && song.songID == songId) { return song; } } return null; } private void BindButtons() { if (more_gameHistoryButton != null) { more_gameHistoryButton.onClick.RemoveListener(OpenDetailedHistory); more_gameHistoryButton.onClick.AddListener(OpenDetailedHistory); } if (shut_gameHistoryButton != null) { shut_gameHistoryButton.onClick.RemoveListener(CloseDetailedHistory); shut_gameHistoryButton.onClick.AddListener(CloseDetailedHistory); } } private void EnsureSongCache() { if (cachedSongs == null || cachedSongs.Length == 0) { cachedSongs = RuntimeResourcesCache.LoadSongsFromPath(SongResourcesPath); } } private static void ClearEntries(List entries, Transform parent) { for (int i = entries.Count - 1; i >= 0; i--) { if (entries[i] != null) { Destroy(entries[i]); } } entries.Clear(); if (parent == null) { return; } for (int i = parent.childCount - 1; i >= 0; i--) { Destroy(parent.GetChild(i).gameObject); } } }