public void LoadPersonalBest() { try { // Load the content from the file, if the file does not exist then create it first if (!File.Exists(fileName)) { playerData = new PlayerData(); SavePersonalBest(); } var fileContent = File.ReadAllBytes(fileName); var decrypted = ""; if (fileContent.Length > 0) { decrypted = AESEcryption.Decrypt(fileContent); } // JSON.net - convert the string into an object playerData = JsonConvert.DeserializeObject <PlayerData>(decrypted); // Uncomment this line if you want to print out the playerData object //PrintPlayerData(); } catch (FileNotFoundException e) { Debug.Log("File not found: " + e.Message); } catch (Exception e) { Debug.Log("Error occured: " + e.Message); Debug.Log("Error stack trace: " + e.StackTrace); } }
// Use this function to generate Random values //public void SetRandomData() //{ // playerData = new PlayerData() // { // username = "******" + UnityEngine.Random.Range(1, 5), // bestScore = UnityEngine.Random.Range(1, 4), // date = DateTime.UtcNow, // totalPlayersInTheGame = UnityEngine.Random.Range(1, 5), // roomName = "Random Room " + UnityEngine.Random.Range(1, 5) // }; // PrintPlayerData(); //} public void SavePersonalBest() { // Uncomment this line if you want to use Unity's built-in JSON solution //var serializedData = JsonUtility.ToJson(playerData); // JSON.net - convert the object into a string var serializedData = JsonConvert.SerializeObject(playerData); var encrypted = AESEcryption.Encrypt(serializedData); // Write the string to the file File.WriteAllBytes(fileName, encrypted); Debug.Log("Data saved!"); }