using PlayFab.ClientModels; using PlayFab; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using MS; using TMPro; public class Profile : MonoBehaviour { // Start is called before the first frame update public Button EnteredNameButton; public List AvatarSprites; public Sprite AvatarSelectedSprite; public Popup EnterNamePanel; public Popup MainPanel; public TMP_InputField nameInputField; public Image HeaderProfileImg; public TextMeshProUGUI HeaderProfileName; public Image LevelFillerImg; public TextMeshProUGUI LevelFillerText; private void Start() { CheckForDisplayName(); CheckForAvatar(); if (EnteredNameButton != null) EnteredNameButton.interactable = false; } private void CheckForAvatar() { int hasSelectedAvatar = PlayerPrefs.GetInt(GameConstants.AvatarSelectedCheckKey, 0); if (hasSelectedAvatar > 0) { //string name = PlayerPrefs.GetString(GameConstants.DisplayNameKey); //PlayFabLeaderboards.DisplayName = name; AvatarSelectedSprite = AvatarSprites[PlayerPrefs.GetInt(GameConstants.AvatarSelectedIndex)]; HeaderProfileImg.sprite = AvatarSelectedSprite; } else { MainPanel.Close(); if (!EnterNamePanel.isOpen) EnterNamePanel.Open(); } } private void CheckForDisplayName() { int hasEnteredName = PlayerPrefs.GetInt(GameConstants.NameEnteredCheckKey, 0); if (hasEnteredName > 0) { string name = PlayerPrefs.GetString(GameConstants.DisplayNameKey); PlayFabLeaderboards.DisplayName = name; PlayFabManager.Instance.playFabLeaderboards.SetDisplayName(name); HeaderProfileName.text = name; LevelFillerSetter(); } else { Debug.Log("Name not found in plaerprefs"); MainPanel.Close(); if (!EnterNamePanel.isOpen) EnterNamePanel.Open(); } } public void LevelFillerSetter() { int bestScore = PlayerPrefs.GetInt("BestScore", 0); // Default to 0 if not set float fillAmount = Mathf.Clamp01((float)bestScore / 100f); // Ensure it stays between 0 and 1 LevelFillerImg.fillAmount = fillAmount; //LevelFillerText.text = bestScore + "/" + maxScore; } public void OnDisplayNameEntered() { if (PlayFabClientAPI.IsClientLoggedIn()) { PlayFabManager.Instance.playFabLeaderboards.SetDisplayName(nameInputField.text); } SafePlayerPrefs.SetInt(GameConstants.NameEnteredCheckKey, 1); SafePlayerPrefs.SetString(GameConstants.DisplayNameKey, nameInputField.text); HeaderProfileName.text = nameInputField.text; EnterNamePanel.Close(); LevelFillerSetter(); } public void OnAvatarSelected(int AvatarID) { //PlayFabManager.Instance.playFabLeaderboards.SetDisplayName(nameInputField.text); EnteredNameButton.interactable = true; SafePlayerPrefs.SetInt(GameConstants.AvatarSelectedCheckKey, 1); SafePlayerPrefs.SetInt(GameConstants.AvatarSelectedIndex, AvatarID); AvatarSelectedSprite = AvatarSprites[AvatarID]; HeaderProfileImg.sprite = AvatarSelectedSprite; SetAvatarID(AvatarID); //NameEnterPanel.Close(); } public void SetAvatarID(int avatarID) { var request = new UpdateUserDataRequest { Data = new Dictionary { { "AvatarID", avatarID.ToString() } // Convert int to string for storage }, Permission = UserDataPermission.Public }; if (PlayFabClientAPI.IsClientLoggedIn()) PlayFabClientAPI.UpdateUserData(request, OnDataUpdateSuccess, OnDataUpdateFailure); } private void OnDataUpdateSuccess(UpdateUserDataResult result) { Debug.Log("Avatar ID updated successfully."); } private void OnDataUpdateFailure(PlayFabError error) { Debug.LogError("Failed to update Avatar ID: " + error.GenerateErrorReport()); } }