Using the below code, the Json file written is completely empty (except for 2 curly brackets; “{}”).
void SaveData()
{
//Create Output from list of PrefabData
StringBuilder sb = new StringBuilder();
string outputData = "";
foreach (PrefabData prefabDataItem in prefabData)
{
sb.Append(prefabDataItem.CategoryName);
sb.Append(",");
foreach (string gameObjectRef in prefabDataItem.GameObjectRefs)
{
sb.Append(gameObjectRef);
sb.Append(",");
}
sb.Append("#");//end of category marker
}
outputData = sb.ToString();
//EditorPrefs.SetString("Prefab_Library_SaveData", outputData);
Debug.Log(outputData);
string jsonData = JsonUtility.ToJson(outputData, true);
File.WriteAllText(Application.persistentDataPath + "/PrefabLibrarySaveData.json", jsonData);
}
This code should get a result of this for example:
A,CategoryItem1,CategoryItem2,#B,CategoryItem1,CategoryItem2,#
And this is what is written to the console, but when going to see the file (opening it in Notepad) the file is blank apart from “{}”.
What exactly am I doing wrong here? Can Json not save data like this or have I just done something wrong?