using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; public class LeaderboardBase : MonoBehaviour { [SerializeField] protected RectTransform _content; [SerializeField] protected LeaderboardEntry _leaderboardEntryPrefab; [SerializeField] protected LeaderboardEntry _currentPlayerLeaderboardEntry; protected List _entries = new List(); protected List _players = new List(); protected LeaderboardController _leaderboardController; public UnityEvent OnUpdated; protected void Awake() { _leaderboardController = FindObjectOfType(); if (_currentPlayerLeaderboardEntry != null) { if (GlobalSettings.Instance.AllMaps) _currentPlayerLeaderboardEntry.gameObject.SetActive(true); else _currentPlayerLeaderboardEntry.gameObject.SetActive(false); } } protected virtual void OnEnable() { //_leaderboardController.OnDailyInit.AddListener(InitLeaderboard); } protected virtual void OnDisable() { //_leaderboardController.OnDailyInit.RemoveListener(InitLeaderboard); } protected void InitLeaderboard(List players) { _players = players; UpdateLeaderboard(); } protected void UpdateLeaderboard() { _entries.ForEach(x => Destroy(x.gameObject)); _entries.Clear(); _entries = new List(); int currentPlayerIndex = int.MaxValue; for (int i = 0; i < _players.Count; i++) { var newEntry = Instantiate(_leaderboardEntryPrefab, _content); _entries.Add(newEntry); newEntry.Init(_players[i], i, _players[i] == PlayerSetup.Instance.CurrentPlayer); if (_players[i] == PlayerSetup.Instance.CurrentPlayer) currentPlayerIndex = i; } if (_currentPlayerLeaderboardEntry != null && GlobalSettings.Instance.AllMaps) { _currentPlayerLeaderboardEntry.Init(PlayerSetup.Instance.CurrentPlayer, currentPlayerIndex, true); } OnUpdated?.Invoke(); } }