I am working on an Unity Project which I use new CloudSave system. On my game normally I use serialization/ deserialization and write the game data to a file. I serialize/deserialize the custom data type: "GameData".
However, now I need to use both systems. writing/ reading on file and cloud as well. and this is first time for me. (since Unity CloudSave is new system, there is almost no source)
I am trying to convert a dictionary to JSON string where dictionary has one key and some of values. I want to have a JSON with only values of the dictionary.
to load the data from Unity Cloud I call LoadHandler() method in CouldHandler.cs from an other trigger script which returns me GameData type result.
CouldHandler.cs:
using System.Collections;
using System.Collections.Generic;
using Unity.Services.Authentication;
using Unity.Services.CloudSave;
using Unity.Services.Core;
using UnityEngine;
using Newtonsoft.Json;
using System.IO;
using System.Xml;
using UnityEditor.Experimental.RestService;
using System;
using Unity.VisualScripting;
using System.Text.Json;
using Newtonsoft.Json.Linq;
public class CloudHandler : MonoBehaviour
{
public static string PLAYER_CLOUD_KEY = "PLAYER_DATA";
public static int counter = 0;
public static GameData gameData;
public static Dictionary<string, string> data;
public static string myJson;
public static GameData LoadHandler()
{
LoadData();
return gameData;
}
public static async void LoadData()
{
if (counter == 0)
{
await UnityServices.InitializeAsync();
await AuthenticationService.Instance.SignInAnonymouslyAsync();
counter++;
}
data = await CloudSaveService.Instance.Data.LoadAsync(new HashSet<string> { PLAYER_CLOUD_KEY});
//string INeedAValueHere = // I NEED HERE JSON FROM DICTIONARY [data]
//GameData INeedAnotherValueHere = // I NEED HERE GAMEDATA FORMAT FROM JSON
string myString = data[PLAYER_CLOUD_KEY];
char[] MyChar = { '{', '}'};
myString = myString.TrimStart(MyChar);
myString = myString.TrimEnd(MyChar);
myJson= JsonConvert.SerializeObject(myString);
gameData = JsonUtility.FromJson<GameData>(myJson);
Debug.Log($"myStore: {myJson}"); // out come: "Panda":false,"Peacock":false,"RedPanda ":false
}
}
GameData.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using Unity.VisualScripting;
using UnityEngine;
[System.Serializable]
public class GameData
{
public bool Panda, Peacock, RedPanda, Sloth, Tapir;
public GameData()
{
this.Panda = false; this.Peacock = false; this.RedPanda = false; this.Sloth = false; this.Tapir = false;
}
}
I need a variable in Json string format as well as I need a variable in GameData format
I canNOT claim that my c# is so sharp!! but anyway at max I could get this outcome.
"Panda":false,"Peacock":false,"RedPanda ":false
above outcome may look like a JSON string. But unity is not writing this values to the file. It is just deleting all earlier values.
and when I want to print the values from my file Unity prints it as below format. Therefore I thought I do something wrong and Unity is not recognizing myJson variable as Json string.
{
"Alligator" : false,
"Alpaca" : false,
"Antelope" :false
}