You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
3.7 KiB
C#
115 lines
3.7 KiB
C#
5 days ago
|
using PlayFab.ClientModels;
|
||
|
using PlayFab;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
using MS;
|
||
|
using TMPro;
|
||
|
using static D2D.Utilities.CommonGameplayFacade;
|
||
|
|
||
|
public class Profile : MonoBehaviour
|
||
|
{
|
||
|
// Start is called before the first frame update
|
||
|
public Button EnteredNameButton;
|
||
|
public List<Sprite> AvatarSprites;
|
||
|
public Sprite AvatarSelectedSprite;
|
||
|
public Popup EnterNamePanel;
|
||
|
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
|
||
|
{
|
||
|
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;
|
||
|
HeaderProfileName.text = name;
|
||
|
LevelFillerSetter();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (!EnterNamePanel.isOpen)
|
||
|
EnterNamePanel.Open();
|
||
|
}
|
||
|
}
|
||
|
public void LevelFillerSetter()
|
||
|
{
|
||
|
float passedLevels = _db.PassedLevels.Value;
|
||
|
LevelFillerImg.fillAmount = passedLevels/ 20f;
|
||
|
LevelFillerText.text = passedLevels.ToString()+"/20";
|
||
|
}
|
||
|
public void OnDisplayNameEntered()
|
||
|
{
|
||
|
PlayFabManager.Instance.playFabLeaderboards.SetDisplayName(nameInputField.text);
|
||
|
PlayerPrefs.SetInt(GameConstants.NameEnteredCheckKey, 1);
|
||
|
PlayerPrefs.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;
|
||
|
PlayerPrefs.SetInt(GameConstants.AvatarSelectedCheckKey, 1);
|
||
|
PlayerPrefs.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<string, string>
|
||
|
{
|
||
|
{ "AvatarID", avatarID.ToString() } // Convert int to string for storage
|
||
|
},
|
||
|
Permission = UserDataPermission.Public
|
||
|
};
|
||
|
|
||
|
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());
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|