//Save the tile info out to a binary file private void SaveTiles() { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + gameSavesDirectoryPath + "/GameTiles.cas", FileMode.OpenOrCreate); GameTilesInfoList myInfo = new GameTilesInfoList(); //put what ever you're saving as myInfo.whatever myInfo.gameTileInfoList = gameTilesInfoList; bf.Serialize(file, myInfo); file.Close(); }
//called from GameManagerScript public void LoadGameState() { if (File.Exists(Application.persistentDataPath + gameSavesDirectoryPath + "/GameTiles.cas")) { BinaryFormatter bf = new BinaryFormatter(); FileStream file = File.Open(Application.persistentDataPath + gameSavesDirectoryPath + "/GameTiles.cas", FileMode.Open); GameTilesInfoList myLoadedTileInfo = (GameTilesInfoList)bf.Deserialize(file); gameTilesInfoList = myLoadedTileInfo.gameTileInfoList; file.Close(); if (File.Exists(Application.persistentDataPath + gameSavesDirectoryPath + "/GameManager.cas")) { bf = new BinaryFormatter(); file = File.Open(Application.persistentDataPath + gameSavesDirectoryPath + "/GameManager.cas", FileMode.Open); GameManagerInfo myLoadedGameManagerInfo = (GameManagerInfo)bf.Deserialize(file); gameManagerInfo.townHallHP = myLoadedGameManagerInfo.townHallHP; gameManagerInfo.wood = myLoadedGameManagerInfo.wood; gameManagerInfo.stone = myLoadedGameManagerInfo.stone; gameManagerInfo.ore = myLoadedGameManagerInfo.ore; gameManagerInfo.steel = myLoadedGameManagerInfo.steel; SetLoadedTiles(); GameObject.Find("GameManager").GetComponent <GameManagerScript>().Load(gameManagerInfo); file.Close(); } else { Debug.LogError("SaveLoadGame -- LoadGame: Tile were saved but the GameManager file cannot be found!"); } } else { Debug.Log("No saved tiles found"); } }