- Home /
Json Save Returns Empty(Code inside)
Hello everyone I'm trying to do Save Load System with Json. The codes im using are below. And the problem is When i click Save Button There is nothing in the Json text file.
public List<Transform> Transforms;
public void Save()
{
Vector3[] positions = new Vector3[Transforms.Count];
for (int i = 0; i < Transforms.Count; i++)
{
positions[i] = Transforms[i].localPosition;
Debug.Log($"position {i}: {positions[i]}"); // only adding this line
}
string json = JsonUtility.ToJson(positions);
Debug.Log($"json: {json}"); // and this
File.WriteAllText(Application.dataPath + "/save.txt", json);
}
public void Load()
{
if (File.Exists(Application.dataPath + "/save.txt"))
{
string savestring = File.ReadAllText(Application.dataPath + "/save.txt");
Vector3[] positions = JsonUtility.FromJson<Vector3[]>(savestring);
for (int i = 0; i < Transforms.Count; i++)
{
Transforms[i].localPosition = positions[i];
}
}
}
I believe you will need to create your own extended version of Vector3 and make sure you make it [System.Serializable]. I don’t think it is set as serializable by default. EDIT: Look, this link has your answer https://answers.unity.com/questions/956047/serialize-quaternion-or-vector3.html
No, not really. The answer you've linked is about normal .NET serialization and not about Unity's "relatively new" JsonUtility. The JsonUtility can serialize Vector3 and Quaternion values. However it does not support arrays as root element. The root element has to be an object. A relevant question would be this one.
Answer by Bunny83 · Mar 14, 2021 at 01:41 AM
As I mentioned in the comment above, Unity's JsonUtility does not support arrays as root objects. The root object has to be a serializable object. So you have to do something like this:
[System.Serializable]
public class SaveData
{
public List<Vector3> positions = new List<Vector3>();
}
public void Save()
{
SaveData data = new SaveData();
for (int i = 0; i < Transforms.Count; i++)
{
data.positions.Add(Transforms[i].localPosition);
}
string json = JsonUtility.ToJson(data);
File.WriteAllText(Application.dataPath + "/save.txt", json);
}
public void Load()
{
if (File.Exists(Application.dataPath + "/save.txt"))
{
string savestring = File.ReadAllText(Application.dataPath + "/save.txt");
SaveData data = JsonUtility.FromJson<SaveData>(savestring);
for (int i = 0; i < Transforms.Count; i++)
{
Transforms[i].localPosition = data.positions[i];
}
}
}
An alternative to Unity's JsonUtility would be either to use the Newtonsoft JSON.NET library. As far as I know it has a surrogate provider in order to serialize Vector3 values directly. Another alternative would be my SimpleJSON framework. It's not an object mapper but just a parser. So you can simply access the json data in a generic way. You only need the SimpleJSON.cs file as well as the SimpleJSONUnity.cs file which is an extension file that gives you easy conversion support for most common Unity primitive types.
Great Answer. Explaining and Teaching. We need more people like you^^ Thank you so much. One of my friend also mentions About Newtonsoft JSON.NET library. And i think it is time to take a look at it.
Your answer
Follow this Question
Related Questions
my json saves but doesnt load after i exit my game 2 Answers
Chest contents persistence between scenes 1 Answer
Problem when saving and loading 1 Answer
do something if a method cant be executed 1 Answer
How to save and load mobs? 0 Answers