public void LoadFile(SaveFile file) { if (file.fileName?.Length == 0) { Debug.LogWarning("Enter file name"); return; } string dataAsJson = ""; switch (file.saveLocation) { case SaveLocation.LocalFile: var filePath = Path.Combine(Application.persistentDataPath, file.fileName); if (File.Exists(filePath)) { dataAsJson = File.ReadAllText(filePath); } break; case SaveLocation.PlayerPref: dataAsJson = PlayerPrefs.GetString(file.fileName); break; } if (dataAsJson?.Length > 0) { if (file.useEncryption) { dataAsJson = BinaryString.BinaryToString(dataAsJson); dataAsJson = AesOperationEncryption.DecryptString(file.encryptionKey, dataAsJson); } for (int i = 0; i < file.objectsToSave.Length; i++) { string[] types = dataAsJson.Split(separator); JsonUtility.FromJsonOverwrite(types[i], file.objectsToSave[i]); Debug.Log(file.fileName + " Loaded"); } } else { Debug.LogError("Couldn't find save file"); } }
public void SaveFile(SaveFile file) { if (file.fileName?.Length == 0) { Debug.LogWarning("Enter file name"); return; } string dataAsJson = ""; foreach (var objectToSave in file.objectsToSave) { if (objectToSave) { dataAsJson += JsonUtility.ToJson(objectToSave, file.formatJson) + separator + "\n"; } else { Debug.LogError("missing object data"); } } if (dataAsJson?.Length > 0) { if (file.useEncryption) { dataAsJson = AesOperationEncryption.EncryptString(file.encryptionKey, dataAsJson); dataAsJson = BinaryString.StringToBinary(dataAsJson); } switch (file.saveLocation) { case SaveLocation.LocalFile: var filePath = Path.Combine(Application.persistentDataPath, file.fileName); File.WriteAllText(filePath, dataAsJson); break; case SaveLocation.PlayerPref: PlayerPrefs.SetString(file.fileName, dataAsJson); break; } Debug.Log(file.fileName + " Saved"); } }