Data now storing

hazim-dev
Ali Sharoz 2 weeks ago
parent fe0f97bf6a
commit 0e5d42e600

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

@ -0,0 +1,134 @@
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using System.Collections.Generic;
public class PlayerPrefsSyncManager : MonoBehaviour
{
private static PlayerPrefsSyncManager instance;
private void OnApplicationQuit()
{
SyncPlayerPrefsToPlayFabOnQuit();
}
private void OnApplicationPause()
{
SyncPlayerPrefsToPlayFabOnQuit();
}
private void OnApplicationFocus(bool focus)
{
//if (!focus)
SyncPlayerPrefsToPlayFabOnQuit();
}
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
Application.quitting += SyncPlayerPrefsToPlayFabOnQuit;
}
else
{
Destroy(gameObject);
}
}
private const int MaxKeysPerRequest = 10;
public void SyncPlayerPrefsToPlayFabOnQuit()
{
if (PlayFabClientAPI.IsClientLoggedIn())
{
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 strVal = PlayerPrefs.GetString(key, "__MISSING__");
if (strVal != "__MISSING__")
{
allPrefs[key] = "string:" + strVal;
continue;
}
int intVal = PlayerPrefs.GetInt(key, int.MinValue + 1);
if (intVal != int.MinValue + 1)
{
allPrefs[key] = "int:" + intVal;
continue;
}
float floatVal = PlayerPrefs.GetFloat(key, float.MinValue + 1);
if (floatVal != float.MinValue + 1)
{
allPrefs[key] = "float:" + floatVal.ToString("R");
}
}
foreach (var pair in allPrefs)
{
Debug.Log($"[Sync] {pair.Key} = {pair.Value}");
}
// Split into batches of 10
var batches = new List<Dictionary<string, string>>();
var currentBatch = new Dictionary<string, string>();
foreach (var pair in allPrefs)
{
currentBatch[pair.Key] = pair.Value;
if (currentBatch.Count == MaxKeysPerRequest)
{
batches.Add(currentBatch);
currentBatch = new Dictionary<string, string>();
}
}
if (currentBatch.Count > 0)
{
batches.Add(currentBatch);
}
UploadPlayerPrefsBatches(batches, 0);
}
}
private void UploadPlayerPrefsBatches(List<Dictionary<string, string>> batches, int index)
{
if (index >= batches.Count)
{
Debug.Log("✅ All PlayerPrefs batches synced to PlayFab.");
return;
}
var request = new UpdateUserDataRequest
{
Data = batches[index],
Permission = UserDataPermission.Public
};
PlayFabClientAPI.UpdateUserData(request,
result =>
{
Debug.Log($"✅ Synced batch {index + 1}/{batches.Count}");
UploadPlayerPrefsBatches(batches, index + 1);
},
error =>
{
Debug.LogError($"❌ Failed to sync batch {index + 1}/{batches.Count}: {error.GenerateErrorReport()}");
});
}
}

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 58fa5ecef02d8ee43bd6d89017eb6efd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

@ -2175,7 +2175,7 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 1440779029}
- component: {fileID: 1440779028}
- component: {fileID: 1440779030}
m_Layer: 0
m_Name: PlayerPrefSyncManager
m_TagString: Untagged
@ -2183,18 +2183,6 @@ GameObject:
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1440779028
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1440779027}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 505af05eda8a6824684d079a3bd27291, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1440779029
Transform:
m_ObjectHideFlags: 0
@ -2210,6 +2198,18 @@ Transform:
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1440779030
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1440779027}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 58fa5ecef02d8ee43bd6d89017eb6efd, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1601498681
GameObject:
m_ObjectHideFlags: 0

Loading…
Cancel
Save