public void loadBoardFromFile() { //test read: if (File.Exists(filename)) { try { XmlSerializer serializer = new XmlSerializer(typeof(SaveBoardObject)); FileStream fs = new FileStream(filename, FileMode.Open); saveBoard = (SaveBoardObject)serializer.Deserialize(fs); Debug.Log("Game data loaded!"); if (saveBoard.boardHeight > 0 && saveBoard.boardWidth > 0) { tileColumns = saveBoard.boardWidth; tileRows = saveBoard.boardHeight; loadEmptyBoard(); loadBoardTilesOntoBoard(); } else { ShowConfigOptions(); } fs.Close(); } catch { ShowConfigOptions(); } } else { Debug.LogError("There is no save data!"); //loadBoardFresh } }
public void saveBoardToFile() { saveBoard = new SaveBoardObject(tileColumns, tileRows); //saves the edge tiles: for (int i = 0; i < 4; i++) { for (int j = 0; j < edgeTiles[i].Length; j++) { var currentTile = edgeTiles[i][j].GetComponent <EdgeTile>(); if (currentTile.edgeTileAttribute) { var currentAttribute = currentTile.edgeTileAttribute.GetComponent <TileAttribute>(); if (currentAttribute) { Debug.Log(currentAttribute.name); var saveEdgeTile = new SaveEdgeTile(); saveEdgeTile.side = currentTile.side; saveEdgeTile.index = currentTile.index; saveEdgeTile.type = currentAttribute.type; saveEdgeTile.colorArrayNumber = currentAttribute.colorArrayNumber; saveEdgeTile.colourName = currentAttribute.mainTileColour.colourName; //saveEdgeTile.isLocked = currentAttribute.isLocked; saveBoard.edgeTiles.Add(saveEdgeTile); } } } } //saves the game tiles: for (int i = 0; i < tileColumns; i++) { for (int j = 0; j < tileRows; j++) { var currentTile = tiles[i, j].GetComponent <Tile>(); if (currentTile.tileAttribute) { var currentAttribute = currentTile.tileAttribute.GetComponent <TileAttribute>(); if (currentAttribute) { Debug.Log(currentAttribute.name); var saveGameTile = new SaveGameTile(); saveGameTile.positionX = (int)currentTile.position.x; saveGameTile.positionY = (int)currentTile.position.y; saveGameTile.type = currentAttribute.type; saveGameTile.colorArrayNumber = currentAttribute.colorArrayNumber; saveGameTile.colourName = currentAttribute.mainTileColour.colourName; saveGameTile.rotation = currentAttribute.rotation; saveGameTile.isLocked = currentAttribute.isLocked; saveBoard.gameTiles.Add(saveGameTile); } } } } // Create an instance of the XmlSerializer class; // specify the type of object to serialize. XmlSerializer serializer = new XmlSerializer(typeof(SaveBoardObject)); TextWriter writer = new StreamWriter(filename); // Serialize the purchase order, and close the TextWriter. serializer.Serialize(writer, saveBoard); writer.Close(); ReturnToMenu(); }