59 lines
1.4 KiB
C#
59 lines
1.4 KiB
C#
|
using System;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.Events;
|
||
|
|
||
|
public class DailyLeaderboard : MonoBehaviour
|
||
|
{
|
||
|
[SerializeField]
|
||
|
protected RectTransform _content;
|
||
|
[SerializeField]
|
||
|
protected LeaderboardEntry _leaderboardEntryPrefab;
|
||
|
protected List<LeaderboardEntry> _entries = new List<LeaderboardEntry>();
|
||
|
|
||
|
private List<Player> _players = new List<Player>();
|
||
|
|
||
|
private LeaderboardController _leaderboardController;
|
||
|
|
||
|
public UnityEvent OnUpdated;
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
_leaderboardController = FindObjectOfType<LeaderboardController>();
|
||
|
}
|
||
|
|
||
|
private void OnEnable()
|
||
|
{
|
||
|
_leaderboardController.OnDailyInit.AddListener(InitLeaderboard);
|
||
|
}
|
||
|
|
||
|
|
||
|
private void OnDisable()
|
||
|
{
|
||
|
_leaderboardController.OnDailyInit.RemoveListener(InitLeaderboard);
|
||
|
}
|
||
|
|
||
|
private void InitLeaderboard(List<Player> players)
|
||
|
{
|
||
|
_players = players;
|
||
|
UpdateLeaderboard();
|
||
|
}
|
||
|
|
||
|
protected virtual void UpdateLeaderboard()
|
||
|
{
|
||
|
_entries.ForEach(x => Destroy(x.gameObject));
|
||
|
_entries.Clear();
|
||
|
_entries = new List<LeaderboardEntry>();
|
||
|
|
||
|
for (int i = 0; i < _players.Count; i++)
|
||
|
{
|
||
|
var newEntry = Instantiate(_leaderboardEntryPrefab, _content);
|
||
|
_entries.Add(newEntry);
|
||
|
newEntry.Init(_players[i], i);
|
||
|
}
|
||
|
OnUpdated?.Invoke();
|
||
|
}
|
||
|
}
|