/// <summary> /// load data from persistentPath /// </summary> /// <returns>true if save file exist</returns> private bool LoadAllData() { data = new GameScriptData(); bool loadExist = false; string savePath = Path.Combine(Application.persistentDataPath, "Save"); if (Directory.Exists(savePath)) { string saveFile = Path.Combine(savePath, "main.json"); if (File.Exists(saveFile)) { using (StreamReader dat = new StreamReader(saveFile)) { string datJSON = dat.ReadToEnd(); data = JsonUtility.FromJson <GameScriptData>(datJSON); loadExist = true; } } } if (!loadExist) { return(false); } Vector2Int mapSize = MVMain.World.mapSize; savedMap = new ChunkData[mapSize.x, mapSize.y]; for (int i = 0; i < mapSize.x; i++) { for (int j = 0; j < mapSize.y; j++) { savedMap[i, j] = data.savedMapRow[i].rowData[j]; } } LoadMinimapData(); score = dead ? 0 : data.score; lastRoom = data.lastRoom; lastSpawn = data.lastSpawn; lastMaxHealth = data.lastMaxHealth; playerPlayer.weapons = data.weapons.Select(x => MVUtility.CreateWeapon(x.name, x.level)).ToList(); playerPlayer.maxHealth = lastMaxHealth; playerPlayer.health = lastMaxHealth; MVMain.Room.activeRoomName = lastRoom; MVMain.Room.spawnFrom = lastSpawn; MVMain.Room.LoadRoomData(); if (room != null) { Destroy(room); } LoadRoom(TeleporterData.TeleporterType.Direct); return(true); }
/// <summary> /// Save data to persistentPath /// </summary> /// <param name="spawner">last spawner(checkpoint) position</param> public void SaveAllData(string spawner = "") { if (spawner == "") { spawner = MVMain.Room.spawnFrom; } lastRoom = MVMain.Room.activeRoomName; lastSpawn = spawner; lastMaxHealth = playerPlayer.maxHealth; data = new GameScriptData { score = score, lastRoom = lastRoom, lastSpawn = lastSpawn, lastMaxHealth = lastMaxHealth, weapons = playerPlayer.weapons.Select(x => new SerializedWeapon(x)).ToList() }; SaveMinimapData(); for (int i = 0; i < savedMap.GetLength(0); i++) { ChunkRow row = new ChunkRow(); for (int j = 0; j < savedMap.GetLength(1); j++) { row.rowData.Add(savedMap[i, j]); } data.savedMapRow.Add(row); } string savePath = Path.Combine(Application.persistentDataPath, "Save"); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } string saveFile = Path.Combine(savePath, "main.json"); using (StreamWriter dat = new StreamWriter(saveFile)) { dat.Write(JsonUtility.ToJson(data)); } }