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.
CrowdControl/Assets/Scripts/Dev/PlayFab/PlayFabInit.cs

110 lines
3.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayFabInit : MonoBehaviour
{
public string playFabTitleId = "7D3B9"; // Replace with your PlayFab Title ID
void Start()
{
if (string.IsNullOrEmpty(PlayFabSettings.TitleId))
{
PlayFabSettings.TitleId = playFabTitleId;
}
Login();
}
void Login()
{
var request = new LoginWithCustomIDRequest
{
CustomId = SystemInfo.deviceUniqueIdentifier,
CreateAccount = true,
// InfoRequestParameters = new GetPlayerCombinedInfoRequestParams
// {
// GetPlayerProfile = true, // This will include player profile data in the login response
// ProfileConstraints = new PlayerProfileViewConstraints
// {
// ShowDisplayName = true,
// ShowAvatarUrl = true,
// ShowCreated = true
// }
// }
};
PlayFabClientAPI.LoginWithCustomID(request, OnLoginSuccess, OnLoginFailure);
}
private void OnLoginSuccess(LoginResult result)
{
Debug.Log("Login successful!");
GetPlayerProfile();
GetCustomUserData();
}
private void GetCustomUserData()
{
GetPlayerProfileWithUserData();
}
public void GetPlayerProfileWithUserData()
{
var request = new GetPlayerCombinedInfoRequest
{
PlayFabId = PlayFabSettings.staticPlayer.PlayFabId, // Use the logged-in player's PlayFab ID
InfoRequestParameters = new GetPlayerCombinedInfoRequestParams
{
GetUserData = true // This will include User Data like AvatarID in the response
}
};
PlayFabClientAPI.GetPlayerCombinedInfo(request, OnGetCombinedInfoSuccess, OnGetCombinedInfoFailure);
}
private void OnGetCombinedInfoSuccess(GetPlayerCombinedInfoResult result)
{
var userData = result.InfoResultPayload.UserData;
PlayFabManager.Instance.playFabUserDataManager.myCustomData = userData;
}
private void OnGetCombinedInfoFailure(PlayFabError error)
{
Debug.LogError("Failed to get combined player info: " + error.GenerateErrorReport());
}
public void GetPlayerProfile()
{
var request = new GetPlayerProfileRequest
{
PlayFabId = PlayFabSettings.staticPlayer.PlayFabId,
ProfileConstraints = new PlayerProfileViewConstraints
{
ShowDisplayName = true, // Allowed by your title settings
// Do not request AvatarUrl or other fields, since they are disallowed
}
};
PlayFabClientAPI.GetPlayerProfile(request, OnGetProfileSuccess, OnGetProfileFailure);
}
private void OnGetProfileFailure(PlayFabError obj)
{
Debug.LogError("Couldn't Fetch Player Profile " + obj.GenerateErrorReport());
}
private void OnGetProfileSuccess(GetPlayerProfileResult obj)
{
Debug.Log("Player profile fetching successful");
PlayFabManager.Instance.playFabUserDataManager.myProfile = obj.PlayerProfile;
}
private void OnLoginFailure(PlayFabError error)
{
Debug.LogError("Login failed: " + error.GenerateErrorReport());
}
}