/// <summary> /// Loads the persistent data from the given file. /// </summary> /// <param name="fileName">Name of the file to load.</param> /// <returns>MLContentBindings from file contents if found, null if not found.</returns> public MLContentBindings Load(string fileName) { string fullPath = Path.Combine(Application.persistentDataPath, fileName); //open a file MLPluginLog.DebugFormat("Reading persistence data from : {0}", fullPath); if (fullPath != null && File.Exists(fullPath)) { StreamReader reader = new StreamReader(fullPath); if (reader != null) { string jsonString = reader.ReadToEnd(); MLPluginLog.DebugFormat("Found json of: {0}", jsonString); _data = JsonUtility.FromJson <MLContentBindings>(jsonString); reader.Dispose(); } else { MLPluginLog.Error("MLPersistentFileStorage.Load failed to create StreamReader."); } } else { MLPluginLog.DebugFormat("File was not found: {0}", fullPath); } return(_data); }
/// <summary> /// Saves the persistent data to the given file name. /// </summary> /// <param name="fileName">Name of the file to save to.</param> /// <param name="data">Data to save.</param> /// <returns>True if the file exists, false if not found after writing.</returns> public bool Save(string fileName, MLContentBindings data) { string fullPath = Path.Combine(Application.persistentDataPath, fileName); string jsonString = JsonUtility.ToJson(data); MLPluginLog.DebugFormat("Saving persistence data {0} to: {1}", jsonString, fullPath); try { File.WriteAllText(fullPath, jsonString); } catch (System.IO.IOException) { MLPluginLog.DebugFormat("Unable to write to file: {0}", fullPath); } MLPluginLog.DebugFormat("Save complete, file exists: {0}", File.Exists(fullPath)); return(File.Exists(fullPath)); }