using System.Collections; using System.Collections.Generic; using DG.Tweening; using PlayFab; using PlayFab.ClientModels; using UnityEngine; public class LeaderboardUIScreen : MonoBehaviour { [SerializeField] private GameObject lbItemPrefab; [SerializeField] private GameObject lbItemSelfPrefab; [SerializeField] private Transform content; [SerializeField] private List _lbPedestalItems; public void Init() { Debug.Log("Init called. IsClientLoggedIn: " + PlayFabClientAPI.IsClientLoggedIn()); if (PlayFabClientAPI.IsClientLoggedIn()) { PlayFabManager.Instance.playFabLeaderboards.GetLeaderboard(OnLeaderboardFetchSuccess, OnLeaderboardFetchFailure); } else { Debug.LogWarning("Not logged in yet. Cannot fetch leaderboard."); } } public void OnClose() { // GameObject[] temp = content.transform.GetComponentsInChildren(); // for (int i = 0; i < temp.Length; i++) // { // Destroy(temp[i]); // } } private void OnLeaderboardFetchSuccess(List leaderboard) { Debug.Log("OnLeaderboardFetchSuccess"); foreach (Transform child in content.transform) { Destroy(child.gameObject); } PopulateLeaderboard(leaderboard); } private void PopulateLeaderboard(List leaderboard) { foreach (PlayerLeaderboardEntry lbEntry in leaderboard) { PopulateLbItem(lbEntry); if (lbEntry.Position <= 2) { PopulatePedestalItem(lbEntry); } } content.GetComponent().DOAnchorPosX(0f, 1f).SetEase(Ease.OutElastic); } private void PopulateLbItem(PlayerLeaderboardEntry lbEntry) { bool isSelf = false; if (lbEntry.Profile != null && PlayFabManager.Instance.playFabUserDataManager.myProfile != null) { isSelf = lbEntry.Profile.PlayerId == PlayFabManager.Instance.playFabUserDataManager.myProfile.PlayerId; } LBEntryItem lbItem = Instantiate(isSelf ? lbItemSelfPrefab : lbItemPrefab, content).GetComponent(); lbItem.nameText.text = lbEntry.DisplayName ?? lbEntry.PlayFabId; lbItem.rankText.text = (lbEntry.Position + 1).ToString(); lbItem.scoreText.text = lbEntry.StatValue.ToString(); PlayFabManager.Instance.playFabUserDataManager.GetPlayerAvatarImage(lbEntry.PlayFabId, sprite => { lbItem.profilePic.sprite = sprite; }, error => { Debug.Log("Couldn't get pic"); }); } //private void PopulateLbItem(PlayerLeaderboardEntry lbEntry) //{ // bool isSelf = lbEntry.Profile.PlayerId == PlayFabManager.Instance.playFabUserDataManager.myProfile.PlayerId; // LBEntryItem lbItem = Instantiate(isSelf ? lbItemSelfPrefab : lbItemPrefab, content).GetComponent(); // lbItem.nameText.text = lbEntry.DisplayName; // lbItem.rankText.text = (lbEntry.Position + 1).ToString(); // lbItem.scoreText.text = lbEntry.StatValue.ToString(); // PlayFabManager.Instance.playFabUserDataManager.GetPlayerAvatarImage(lbEntry.PlayFabId, (sprite) => // { // lbItem.profilePic.sprite = sprite; // }, // (s) => // { // Debug.Log("Couldnt get pic"); // }); //} private void PopulatePedestalItem(PlayerLeaderboardEntry lbEntry) { LBPedestalItem pedestalItem = _lbPedestalItems[lbEntry.Position]; pedestalItem.nameText.text = lbEntry.DisplayName ?? lbEntry.PlayFabId; pedestalItem.scoreText.text = lbEntry.StatValue.ToString(); PlayFabManager.Instance.playFabUserDataManager.GetPlayerAvatarImage(lbEntry.PlayFabId, (sprite) => { pedestalItem.profilePic.sprite = sprite; }, (s) => { Debug.Log("Could'nt get pic"); }); } private void OnLeaderboardFetchFailure(PlayFabError error) { Debug.LogError("❌ Failed to fetch leaderboard: " + error.GenerateErrorReport()); } }