public void Load(string workoutDateName) { if (String.IsNullOrEmpty(SaveDataControl.instance.userName)) { userName = "******"; } else { userName = SaveDataControl.instance.userName; } // Passed file name string fileName = workoutDateName; if (File.Exists(Application.persistentDataPath + "/" + userName + "/" + fileName)) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + "/" + userName + "/" + fileName, FileMode.Open); CurrentWorkoutData data = (CurrentWorkoutData)bf.Deserialize(file); file.Close(); // Can now access data elements currentLoadedDateOfWorkout = data.dateOfWorkout; currentLoadedRepsLeft = data.repsCompletedLeft; currentLoadedRepsRight = data.repsCompletedRight; currentLoadedSets = data.setsCompleted; currentLoadedRestTimeTotal = data.restTimeTotal; currentLoadedWorkoutTimeTotal = data.workoutTimeTotal; currentLoadedRomLeft = data.maxROMAchievedLeft; currentLoadedRomRight = data.maxROMAchievedRight; } }
public void Save() { //Debug.Log("SAVE TIME: " + userName); // Set-up file for saving BinaryFormatter bf = new BinaryFormatter(); // Test if username exists if (String.IsNullOrEmpty(SaveDataControl.instance.userName)) { userName = "******"; } else { userName = SaveDataControl.instance.userName; } // File format (reverse ordered to help with ordering) string fileName = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); // Create directory for userName if (!Directory.Exists(Application.persistentDataPath + "/" + userName)) { Debug.Log("Create Dir"); Directory.CreateDirectory(Application.persistentDataPath + "/" + userName); } // Create file FileStream file = File.Create(Application.persistentDataPath + "/" + userName + "/" + fileName + ".dat"); // Data to save CurrentWorkoutData data = new CurrentWorkoutData(); data.dateOfWorkout = System.DateTime.Now; data.repsCompletedLeft = GameControl.instance.completedRepsLeft; data.repsCompletedRight = GameControl.instance.completedRepsRight; data.setsCompleted = GameControl.instance.completedSets; data.restTimeTotal = GameControl.instance.totalTimeAtRest; data.workoutTimeTotal = GameControl.instance.totalTimeOfWorkout; data.maxROMAchievedLeft = GameControl.instance.maxRomAchievedLeft; data.maxROMAchievedRight = GameControl.instance.maxRomAchievedRight; //data.maxROMAchievedLeft = 92.9f; //data.maxROMAchievedRight = 154.3f; // Save data bf.Serialize(file, data); file.Close(); }