From 0e5d42e6002e4cc0857d02ee702b2428e634c79c Mon Sep 17 00:00:00 2001 From: Ali Sharoz Date: Wed, 26 Mar 2025 02:55:43 +0500 Subject: [PATCH] Data now storing --- .../GoogleSignInManager.cs | 0 .../GoogleSignInManager.cs.meta | 0 .../SigninWithGoogle.png | Bin .../SigninWithGoogle.png.meta | 0 Assets/PlayerPrefsSyncManager.cs | 134 ++++++++++++++++++ Assets/PlayerPrefsSyncManager.cs.meta | 11 ++ Assets/Scenes/Loading.unity | 26 ++-- 7 files changed, 158 insertions(+), 13 deletions(-) rename Assets/{ => GoogleSigninSDK}/GoogleSignInManager.cs (100%) rename Assets/{ => GoogleSigninSDK}/GoogleSignInManager.cs.meta (100%) rename Assets/{ => GoogleSigninSDK}/SigninWithGoogle.png (100%) rename Assets/{ => GoogleSigninSDK}/SigninWithGoogle.png.meta (100%) create mode 100644 Assets/PlayerPrefsSyncManager.cs create mode 100644 Assets/PlayerPrefsSyncManager.cs.meta diff --git a/Assets/GoogleSignInManager.cs b/Assets/GoogleSigninSDK/GoogleSignInManager.cs similarity index 100% rename from Assets/GoogleSignInManager.cs rename to Assets/GoogleSigninSDK/GoogleSignInManager.cs diff --git a/Assets/GoogleSignInManager.cs.meta b/Assets/GoogleSigninSDK/GoogleSignInManager.cs.meta similarity index 100% rename from Assets/GoogleSignInManager.cs.meta rename to Assets/GoogleSigninSDK/GoogleSignInManager.cs.meta diff --git a/Assets/SigninWithGoogle.png b/Assets/GoogleSigninSDK/SigninWithGoogle.png similarity index 100% rename from Assets/SigninWithGoogle.png rename to Assets/GoogleSigninSDK/SigninWithGoogle.png diff --git a/Assets/SigninWithGoogle.png.meta b/Assets/GoogleSigninSDK/SigninWithGoogle.png.meta similarity index 100% rename from Assets/SigninWithGoogle.png.meta rename to Assets/GoogleSigninSDK/SigninWithGoogle.png.meta diff --git a/Assets/PlayerPrefsSyncManager.cs b/Assets/PlayerPrefsSyncManager.cs new file mode 100644 index 00000000..a6881f5a --- /dev/null +++ b/Assets/PlayerPrefsSyncManager.cs @@ -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 allPrefs = new Dictionary(); + + 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>(); + var currentBatch = new Dictionary(); + + foreach (var pair in allPrefs) + { + currentBatch[pair.Key] = pair.Value; + if (currentBatch.Count == MaxKeysPerRequest) + { + batches.Add(currentBatch); + currentBatch = new Dictionary(); + } + } + + if (currentBatch.Count > 0) + { + batches.Add(currentBatch); + } + + UploadPlayerPrefsBatches(batches, 0); + } + + } + + private void UploadPlayerPrefsBatches(List> 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()}"); + }); + } + +} diff --git a/Assets/PlayerPrefsSyncManager.cs.meta b/Assets/PlayerPrefsSyncManager.cs.meta new file mode 100644 index 00000000..11522f73 --- /dev/null +++ b/Assets/PlayerPrefsSyncManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 58fa5ecef02d8ee43bd6d89017eb6efd +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Loading.unity b/Assets/Scenes/Loading.unity index 9d4f138d..b8ff2880 100644 --- a/Assets/Scenes/Loading.unity +++ b/Assets/Scenes/Loading.unity @@ -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