Created
March 16, 2023 11:38
-
-
Save Matthew-J-Spencer/336fcb9b3c06dc17fdd5834ca8251b35 to your computer and use it in GitHub Desktop.
SaveClient.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Unity.Services.CloudSave; | |
using UnityEngine; | |
public class CloudSaveClient : ISaveClient | |
{ | |
private readonly ICloudSaveDataClient _client = CloudSaveService.Instance.Data; | |
public async Task Save(string key, object value) | |
{ | |
var data = new Dictionary<string, object> { { key, value } }; | |
await Call(_client.ForceSaveAsync(data)); | |
} | |
public async Task Save(params (string key, object value)[] values) | |
{ | |
var data = values.ToDictionary(item => item.key, item => item.value); | |
await Call(_client.ForceSaveAsync(data)); | |
} | |
public async Task<T> Load<T>(string key) | |
{ | |
var query = await Call(_client.LoadAsync(new HashSet<string> { key })); | |
return query.TryGetValue(key, out var value) ? Deserialize<T>(value) : default; | |
} | |
public async Task<IEnumerable<T>> Load<T>(params string[] keys) | |
{ | |
var query = await Call(_client.LoadAsync(keys.ToHashSet())); | |
return keys.Select(k => | |
{ | |
if (query.TryGetValue(k, out var value)) | |
{ | |
return value != null ? Deserialize<T>(value) : default; | |
} | |
return default; | |
}); | |
} | |
public async Task Delete(string key) | |
{ | |
await Call(_client.ForceDeleteAsync(key)); | |
} | |
public async Task DeleteAll() | |
{ | |
var keys = await Call(_client.RetrieveAllKeysAsync()); | |
var tasks = keys.Select(k => _client.ForceDeleteAsync(k)).ToList(); | |
await Call(Task.WhenAll(tasks)); | |
} | |
private static T Deserialize<T>(string input) | |
{ | |
if (typeof(T) == typeof(string)) return (T)(object)input; | |
return JsonConvert.DeserializeObject<T>(input); | |
} | |
private static async Task Call(Task action) | |
{ | |
try | |
{ | |
await action; | |
} | |
catch (CloudSaveValidationException e) | |
{ | |
Debug.LogError(e); | |
} | |
catch (CloudSaveRateLimitedException e) | |
{ | |
Debug.LogError(e); | |
} | |
catch (CloudSaveException e) | |
{ | |
Debug.LogError(e); | |
} | |
} | |
private static async Task<T> Call<T>(Task<T> action) | |
{ | |
try | |
{ | |
return await action; | |
} | |
catch (CloudSaveValidationException e) | |
{ | |
Debug.LogError(e); | |
} | |
catch (CloudSaveRateLimitedException e) | |
{ | |
Debug.LogError(e); | |
} | |
catch (CloudSaveException e) | |
{ | |
Debug.LogError(e); | |
} | |
return default; | |
} | |
} | |
public interface ISaveClient | |
{ | |
Task Save(string key, object value); | |
Task Save(params (string key, object value)[] values); | |
Task<T> Load<T>(string key); | |
Task<IEnumerable<T>> Load<T>(params string[] keys); | |
Task Delete(string key); | |
Task DeleteAll(); | |
} |
do you have documentation anywere telling how to use this in detail. im new to file saving and i just learned to output files in a jason format but ive been looking at switchign to cload saving. i saw your youtube video but it seams just a little to far disconnected formt he code to tell exactly how i can implement it.
idk if you ever found out but you have to implement it into any scripts you plan on saving strings in;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading.Tasks;
public class ScriptWithSaveData : MonoBehaviour
{
//PLAYER
public string Username { get; set; }
public int XPCount { get; set; }
//CURRENCY
public int Coins { get; set; }
public int Gems { get; set; }
ISaveClient _client;
async void Awake()
{
await UnityServices.InitializeAsync();
}
private void Start()
{
InvokeRepeating(nameof(SavePlayerData), 0, 3);
}
public async Task SavePlayerData()
{
await _client.Save("XP", XPCount);
await _client.Save("coins", Coins);
await _client.Save("gems", Gems);
}
}
for sure not the best way to do it, but certainly is one way to do it
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
do you have documentation anywere telling how to use this in detail. im new to file saving and i just learned to output files in a jason format but ive been looking at switchign to cload saving. i saw your youtube video but it seams just a little to far disconnected formt he code to tell exactly how i can implement it.