public void SaveColours() { //JsonConvert.DefaultSettings().Converters.Add(new Newtonsoft.Json.Converters.ColorConverter()); ColourDataObject colourData = ColourDataObject.Instance; colourData.BackgroundIndex = BackgroundIndex; colourData.OnWireIndex = OnWireIndex; colourData.OffWireIndex = OffWireIndex; colourData.BackgroundColourObjects = BackgroundColours; colourData.WireColourObjects = WireColours; //serialise list of people, indented, with type info var jsonString = JsonConvert.SerializeObject(colourData, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); string path = Path.Combine(Application.dataPath, "Data/ColourData.json"); if (!File.Exists(path)) { File.Create(path).Dispose(); } File.WriteAllText(path, jsonString); Debug.Log("Saved"); }
public void LoadColours() { //JsonConvert.DefaultSettings().Converters.Add(new Newtonsoft.Json.Converters.ColorConverter()); string path = Path.Combine(Application.dataPath, "Data/ColourData.json"); string debug = Path.Combine(Application.dataPath, "Data/debug.json"); if (!File.Exists(path)) { Debug.Log("NO SAVE FILE RECOGNISED"); } else { ColourDataObject colourData = ColourDataObject.Instance; string jsonString = File.ReadAllText(path); //check that the file is read correctly File.WriteAllText(debug, jsonString); colourData = JsonConvert.DeserializeObject <ColourDataObject>(jsonString, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.Auto }); BackgroundIndex = colourData.BackgroundIndex; OnWireIndex = colourData.OnWireIndex; OffWireIndex = colourData.OffWireIndex; BackgroundColours = colourData.BackgroundColourObjects; foreach (ColourObject colour in BackgroundColours) { Debug.Log(colour.Name + "loaded from save."); } foreach (ColourObject colour in BackgroundColours) { Debug.Log(colour.Name + "loaded from save."); } Debug.Log("Finished Loading Colours"); } }