public void SaveData() { if (_levelID < 1) { Debug.LogError("Cannot save without selecting a level slot."); return; } string path = GetFilePath(); try { System.IO.FileInfo finfo = new System.IO.FileInfo(path); finfo.Directory.Create(); BinaryFormatter formatter = new BinaryFormatter(); FileStream file = File.Create(path); BoardSerializeData data = GetSerializeData(); formatter.Serialize(file, data); file.Close(); } catch (System.Exception e) { Debug.LogError(string.Format("Could not save to file '{0}' - {1}", path, e)); return; } #if UNITY_EDITOR // Reimport the file to make sure it appears in the editor folder view UnityEditor.AssetDatabase.ImportAsset("Assets/StreamingAssets" + path.Substring(Application.streamingAssetsPath.Length)); #endif }
public BoardSerializeData GetSerializeData() { BoardSerializeData data = new BoardSerializeData(); int numCells = sizeX * sizeY; data.sizeX = sizeX; data.sizeY = sizeY; data.cells = new Cell.CellSerializeData[numCells]; for (int i = 0; i < numCells; ++i) { data.cells[i] = _cells[i].GetSerializeData(); } return(data); }
void InitializeBoard(BoardSerializeData data = null) { bool rebuildGrid = true; if (data != null) { Debug.Log("Initializing from data"); if (data.sizeX == sizeX && data.sizeY == sizeY) { rebuildGrid = false; } sizeX = data.sizeX; sizeY = data.sizeY; } sizeX = Mathf.Clamp(sizeX, 1, 100); sizeY = Mathf.Clamp(sizeY, 1, 100); int numCells = sizeX * sizeY; _cells = new Cell[numCells]; if (data != null) { for (int i = 0; i < numCells; ++i) { _cells[i] = new Cell(data.cells[i], inEditMode); // load the solution if in edit mode } } else { for (int i = 0; i < numCells; ++i) { _cells[i] = new Cell(); } } if (rebuildGrid) { BuildGrid(); } else { RefreshGrid(); } PositionCamera(); }
public void LoadData() { string path = GetFilePath(); try { BinaryFormatter formatter = new BinaryFormatter(); FileStream file = File.Open(path, FileMode.Open); BoardSerializeData data = (BoardSerializeData)formatter.Deserialize(file); InitializeBoard(data); file.Close(); } catch (System.Exception) { Debug.LogError(string.Format("Could not load file '{0}'.", path)); return; } }