private void CreateSave() { //Create cupSave CupData cupSave = new CupData("SAVE", m_numberOfRounds, m_playersInCup); m_cupData = cupSave; if (saveCup) { //Get Folder Path // ! Change to Application.persistentDataPath for final build and update variables in Inspector string path = Application.dataPath + m_cupSavePath; //Assign Cup ID - dynamic so that it will be 'Cup 1', 'Cup 2', and so on m_cupID = (Directory.GetFiles(path, "*.json").Length + 1).ToString(); //Update Path and set m_cupFilePath path = path + m_cupSaveFileName + " " + m_cupID + ".json"; m_cupFilePath = path; m_cupData.cupID = m_cupID; Debug.Log(path); //Save SaveCup(cupSave, path); } }
private void SaveCup(CupData cupDataToSave, string path) { if (saveCup) { //Convert CupSave to JSON string saveContent = JsonUtility.ToJson(cupDataToSave, true); //Write to Path File.WriteAllText(path, saveContent); } }
private void LoadCup(string cupID) { if (saveCup) { // ! Change to Application.persistentDataPath for final build and update variables in Inspector string path = Application.dataPath + m_cupSavePath; //Technically the direct file path is already saved in m_cupFilePath, but this is practice to find it by just ID and folder path //Find the file by its cupID DirectoryInfo directory = new DirectoryInfo(path); FileInfo[] fileInfo = directory.GetFiles("*.json"); foreach (FileInfo file in fileInfo) { if (file.Name.Contains(cupID)) { path = path + file.Name; } } string data = File.ReadAllText(path); m_cupData = JsonUtility.FromJson <CupData>(data); } }
private void UnloadCupData() { m_cupData = null; }