- Home /
Fail to load data from json to list of list.
Sorry to my bad question because i not sure how my question going to write. I have using JsonUtility to save my data. Below my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveLoadFunction : MonoBehaviour {
AnyScript anyScript;
void Start()
{
anyScript = AnyScript.instance;
}
private Save CreateSaveData()
{
Save save = new Save ();
for (int i = 0; i < anyScript.ListsOfVariable.Count; i++)
{
save.variableName.Add (anyScript.ListsOfVariable [i].variableName);
save.intVariable.Add (anyScript.ListsOfVariable [i].intVariable);
save.floatVariable.Add (anyScript.ListsOfVariable [i].floatVariable);
save.boolVariable.Add (anyScript.ListsOfVariable [i].boolVariable);
save.randomPosition.Add (anyScript.ListsOfVariable [i].randomPosition);
for (int j = 0; j < anyScript.ListsOfVariable [i].intListVariable.Count; j++)
{
save.intListVariable.Add (anyScript.ListsOfVariable [i].intListVariable [j]);
}
}
return save;
}
public void SaveAsJSON()
{
Save save = CreateSaveData ();
string json = JsonUtility.ToJson (save);
string filePath = Application.dataPath + "/gamesave.save";
File.WriteAllText (filePath, json);
Debug.Log ("Saving as JSON: " + json);
}
public void LoadFromJSON()
{
string filePath = Application.dataPath + "/gamesave.save";
if (File.Exists (filePath))
{
string json = File.ReadAllText (filePath);
Save save = JsonUtility.FromJson<Save> (json);
for (int i = 0; i < anyScript.ListsOfVariable.Count; i++)
{
anyScript.ListsOfVariable [i].variableName = save.variableName [i];
anyScript.ListsOfVariable [i].intVariable = save.intVariable [i];
anyScript.ListsOfVariable [i].floatVariable = save.floatVariable [i];
anyScript.ListsOfVariable [i].boolVariable = save.boolVariable [i];
anyScript.ListsOfVariable [i].randomPosition = save.randomPosition [i];
//?????????????????????????????????????????????????????????????????????????
for (int j = 0; j < anyScript.ListsOfVariable [i].intListVariable.Count; j++)
{
anyScript.ListsOfVariable [i].intListVariable [j] = save.intListVariable [j];
}
}
Debug.Log ("Saving Variable Loaded");
}
else
{
Debug.Log ("No Saving Variable Saved");
}
}
By using code above i have manage to save all the data from the anyscript and success to load most of the data except ListsOfVariable. The AnyScript data will be randomize every time play. Below will be shown the image of before load and after load. Can be see that after load all of the ListsofVariable will only load first 3 number that saved. How to solve this???
Answer by Bunny83 · Mar 15, 2019 at 09:08 AM
We don't know how your Save class looks like. However it seems that you just flattened all the data into a few one dimensional arrays. You just concatenate all the int values into one long list of integers. If you are sure that each item actually has 3 values you could pull out the values correctly. However if one element could have 2 another 4 or 7 elements it's impossible to tell which int values belong to which item.
Why don't you model your data as actual objects in json? Json is a structured object notation language. We also don't know how the serializable class that is stored inside your "ListsOfVariable" list looks like. Though i don't see a reason why you couldn't store those directly as json. The only thing that's important is that Unity's JsonUtility does only support an object as root element. So you can not directly save a List / array (which json itself does support). So you should be able to do:
[System.Serializable]
public class Save
{
public List<YourItemType> allItems;
}
To save your data you could just do:
Save save = new Save();
save.allItems = anyScript.ListsOfVariable;
string json = JsonUtility.ToJson (save);
string filePath = Application.dataPath + "/gamesave.save";
File.WriteAllText (filePath, json);
To load, just do:
//[...]
Save save = JsonUtility.FromJson<Save> (json);
anyScript.ListsOfVariable = save.allItems;
I have try and wow, is it so easy to save the variable... why i think so complicate... Thank you.