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/PlayerPrefsSyncManager.cs

61 lines
1.5 KiB
C#

using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using System.Collections.Generic;
using Google.Impl;
public class PlayerPrefsSyncManager : MonoBehaviour
{
private static PlayerPrefsSyncManager instance;
private void OnApplicationQuit()
{
SyncPlayerPrefsToPlayFabOnQuit();
}
private void OnApplicationPause()
{
SyncPlayerPrefsToPlayFabOnQuit();
}
void Awake()
{
// Make this object persistent across scenes
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
Application.quitting += SyncPlayerPrefsToPlayFabOnQuit;
}
else
{
Destroy(gameObject);
}
}
private void SyncPlayerPrefsToPlayFabOnQuit()
{
var keys = PlayerPrefsKeys.GetAllKeys();
if (keys.Count == 0)
{
Debug.Log("No PlayerPrefs keys registered, skipping sync.");
return;
}
Dictionary<string, string> allPrefs = new Dictionary<string, string>();
foreach (var key in keys)
{
string value = PlayerPrefs.GetString(key);
allPrefs[key] = value;
}
var request = new UpdateUserDataRequest
{
Data = allPrefs
};
PlayFabClientAPI.UpdateUserData(request,
result => Debug.Log("✅ Synced PlayerPrefs to PlayFab on quit."),
error => Debug.LogError("❌ Failed to sync PlayerPrefs on quit: " + error.GenerateErrorReport()));
}
}