/// <summary> /// Clears and repopulats the list of saved games /// </summary> private void Refresh() { //clear the list saves.Clear(); foreach (Transform t in gameSavesList.transform) { Destroy(t.gameObject); } //find the saved games in the save directory Directory.CreateDirectory(Level.SAVE_PATH); List <FileInfo> files = new DirectoryInfo(Level.SAVE_PATH).GetFiles().OrderByDescending(f => f.LastWriteTime).ToList(); //create a SavedGameItem for each saved game and add it to the saved game list foreach (FileInfo item in files) { GameObject saveItem = SavedGameItem.getFromFile(item); if (saveItem != null) { saveItem.GetComponent <Toggle>().group = group; saveItem.GetComponent <Toggle>().isOn = false; saves.Add(saveItem.GetComponent <SavedGameItem>()); } } SortByDate(); }
/// <summary> /// Static method that creates a SavedGameItem from a file containing a saved game. /// Returns the SavedGameItem if it was created succesfully, null if not. /// </summary> /// <param name="file">File containing the saved game.</param> /// <returns>The created SavedGameItem or null.</returns> public static GameObject getFromFile(FileInfo file) { StreamReader reader = null; try { string tempSaveName, tempDate, tempLevel, tempPlayers, tempDifficulty, tempPvp; bool autoSave; //make sure the file is the correct type if (file.Extension.Equals(Level.AUTO_SAVE_EXTENTION)) { tempSaveName = "Auto Save"; autoSave = true; } else if (file.Extension.Equals(Level.SAVE_EXTENTION)) { tempSaveName = Path.GetFileNameWithoutExtension(file.Name); autoSave = false; } else { return(null); } tempDate = file.LastWriteTime.ToString(); reader = new System.IO.StreamReader(file.FullName); //read in the Levels settings tempLevel = reader.ReadLine(); tempPlayers = reader.ReadLine(); tempDifficulty = reader.ReadLine(); tempPvp = reader.ReadLine(); //create the SavedGameItem GameObject obj = Instantiate(Resources.Load("SaveGameItemPF"), Vector3.zero, Quaternion.Euler(0, 0, 0)) as GameObject; SavedGameItem save = obj.GetComponent <SavedGameItem>(); //set the values in the SavedGameItem with the read in Level settings, //filename and file modified time save.date.text = tempDate; save.saveName.text = tempSaveName; save.level.text = tempLevel; save.players.text = tempPlayers; save.difficulty.text = tempDifficulty; save.pvp.text = tempPvp; save.saveFile = file; save.theAutoSave = autoSave; return(obj); } catch { return(null); } finally { if (reader != null) { reader.Close(); } } }