rabidus-test/Assets/Scripts/LeaderboardController.cs

49 lines
1.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class LeaderboardController : MonoBehaviour
{
[SerializeField]
protected RectTransform _content;
[SerializeField]
protected LeaderboardEntry _leaderboardEntryPrefab;
[SerializeField]
private InputField _nameInputField;
protected List<LeaderboardEntry> _entries = new List<LeaderboardEntry>();
protected virtual void Start()
{
UpdateLeaderboard();
}
protected virtual void UpdateLeaderboard()
{
_entries.ForEach(x => Destroy(x.gameObject));
_entries.Clear();
_entries = new List<LeaderboardEntry>();
var sortedList = PlayerSetup.Instance.AllPlayers.OrderByDescending(x => x.Score).ToList();
for (int i = 0; i < sortedList.Count; i++)
{
var newEntry = Instantiate(_leaderboardEntryPrefab, _content);
_entries.Add(newEntry);
newEntry.Init(sortedList[i], i);
}
}
[ContextMenu("Debug submit")]
public void SubmitNewEntry()
{
PlayerSetup.Instance.SetPlayerName(_nameInputField.text);
UpdateLeaderboard();
}
}