public static void Save(string saveString)
{
File.AppendAllText(SAVE_FOLDER + "/" + "savegame.txt", saveString);
}
The method is being called twice or even more times and each time with another saveString. The content of the saved file is like this :
{"objects":[{"Key":"367f6ac2-6fd3-4c99-91e5-cc335a104ac4","Value":"{\"s1\":false}"}],"instanceID":0,"position":{"x":8.140000343322754,"y":0.0,"z":0.0},"scaling":{"x":1.0,"y":1.0,"z":1.0},"rotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0}}{"objects":[{"Key":"ecb45f8e-463c-4fe3-a436-5d836165bcce","Value":"{\"s1\":false}"}],"instanceID":0,"position":{"x":-11.229999542236329,"y":0.0,"z":8.920000076293946},"scaling":{"x":1.0,"y":1.0,"z":1.0},"rotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0}}
And I want to format it to something nicer to be easier to look at maybe adding an empty line between each string ? Or some other format ?
This is how I'm saving it with json :
public void Save()
{
SaveObject saveObject = new SaveObject();
for (int i = 0; i < objectsToSave.Count; i++)
{
var x = objectsToSave[i].GetComponents<Component>();
var y = x.Where(component => component is IStateQuery).ToList();
List<KeyToValue> myObjects = new List<KeyToValue>();
foreach(var z in y)
{
var w = z as IStateQuery;
myObjects.Add(new KeyToValue(w.UniqueId.ToString(), w.GetState()));
}
saveObject.position = objectsToSave[i].position;
saveObject.scaling = objectsToSave[i].localScale;
saveObject.rotation = objectsToSave[i].rotation;
saveObject.objects = myObjects;
string json = JsonUtility.ToJson(saveObject);
SaveSystem.Save(json);
}
}
But I don't like the format of the content in the file.
I tried to add Environment.NewLine :
File.AppendAllText(SAVE_FOLDER + "/" + "savegame.txt" + Environment.NewLine, saveString);
But it's giving me error :
ArgumentException: Illegal characters in path.
I think your fix might be as simple as this:
File.AppendAllText(SAVE_FOLDER + "/" + "savegame.txt" , Environment.NewLine + saveString);
// or even better at the end
File.AppendAllText(SAVE_FOLDER + "/" + "savegame.txt" , saveString + Environment.NewLine);
You are adding the new line to the filename and it is indeed invalid. Add it before or after your content and you'll do fine.
By the way, the content of the file, is not a valid json. While each line is, the total file is not as the following syntax is invalid
{}
{}
If you want it all to be a JSon object you'll need something like this
[
{},
{}
]
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras