加入用户最近100场战绩记录并实现展示
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class uRecentGameHistory : MonoBehaviour
|
||||
{
|
||||
private const string SongResourcesPath = "song_songIndex";
|
||||
private const int MaxVisibleRecordCount = 5;
|
||||
|
||||
[Header("gameobjects")]
|
||||
public GameObject gameHistoryPrefab;
|
||||
public Transform gameHistoryParent;
|
||||
public Transform dGameHistoryParent;
|
||||
public GameObject more_gameHistory;
|
||||
|
||||
[Header("buttons")]
|
||||
public Button more_gameHistoryButton;
|
||||
public Button shut_gameHistoryButton;
|
||||
|
||||
private SongData[] cachedSongs;
|
||||
private readonly List<GameObject> previewEntries = new List<GameObject>();
|
||||
private readonly List<GameObject> detailEntries = new List<GameObject>();
|
||||
|
||||
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 = Resources.LoadAll<SongData>(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);
|
||||
previewEntries.Add(instance);
|
||||
ghPrefab controller = instance.GetComponent<ghPrefab>();
|
||||
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<ghPrefab>();
|
||||
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 = Resources.LoadAll<SongData>(SongResourcesPath);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ClearEntries(List<GameObject> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user