private void LoadMap(string p_path) { lockHUD = true; string __loadPath = p_path; Action __loadAction = () => { //LOAD STREAM try { using (Stream __stream = File.Open(__loadPath, FileMode.Open)) { Map.SaveDummy __dummy = (Map.SaveDummy)(new BinaryFormatter().Deserialize(__stream)); _loadedTiles = __dummy.loadedTiles; _loadedTiles.ForEach(x => { Texture2D __texture = new Texture2D(1, 1); __texture.LoadImage(x.encodedTexture); x.texture = __texture; }); foreach (var tile in __dummy.map) { TileObject __tileObj = ((GameObject)GameObject.Instantiate(tilePrefab, tile.Key.GetVector3(), tilePrefab.transform.rotation)).GetComponent <TileObject>(); __tileObj.Initialize(_loadedTiles.Find(x => x.id == tile.Value)); _tilesOnScene.Add(__tileObj); if (_tileIdCount < tile.Value) { _tileIdCount = tile.Value; } } if (__dummy.tileGroups != null) { _groups = __dummy.tileGroups; } } lockHUD = false; onHUDLocked = null; ShowMessage("Loaded."); } catch (Exception e) { lockHUD = false; onHUDLocked = null; ShowMessage("Failed.\n" + e.Message); } }; __loadAction(); }
private void OnSaveBtnClicked() { lockHUD = true; string __savePath = ""; Action __saveAction = () => { //SAVE STREAM try { using (Stream __stream = File.Create(__savePath)) { Map.SaveDummy __dummy = new Map.SaveDummy(); __dummy.loadedTiles = new List <Tile>(); __dummy.tileGroups = _groups; _loadedTiles.ForEach(x => __dummy.loadedTiles.Add(x.GetTileForSerialization())); __dummy.map = new Dictionary <Map.SaveDummy.DummyVector, int>(); _tilesOnScene.ForEach(x => __dummy.map.Add(new Map.SaveDummy.DummyVector(x.transform.position), x.tile.id)); new BinaryFormatter().Serialize(__stream, __dummy); } lockHUD = false; onHUDLocked = null; ShowMessage("Saved."); } catch (Exception e) { lockHUD = false; onHUDLocked = null; ShowMessage("Failed.\n" + e.Message); } }; if (_useDialog) { System.Windows.Forms.SaveFileDialog __saveDialog = new System.Windows.Forms.SaveFileDialog(); __saveDialog.Title = "Select where to save the map"; __saveDialog.Filter = "bytes|*.bytes"; if (__saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { __savePath = __saveDialog.FileName; __saveAction(); } } else { onHUDLocked = null; onHUDLocked += () => { //DRAW WINDOW Rect __saveWindowRect = new Rect((Screen.width * 0.5f) - 200, (Screen.height * 0.5f) - 50, 400, 115); GUI.BeginGroup(__saveWindowRect); { GUI.Box(new Rect(0, 0, __saveWindowRect.width, __saveWindowRect.height), ""); //SELECT PATH Rect __selectSavePathRect = new Rect(15, 10, 750, 60); GUI.BeginGroup(__selectSavePathRect); { GUI.Label(new Rect(0, 0, 400, 30), "Enter save file name: "); __savePath = GUI.TextField(new Rect(0, 30, 310, 25), __savePath); //SELECT BTN if (GUI.Button(new Rect(315, 30, 50, 25), "Select")) { if (__savePath.ToLower().EndsWith(".bytes") == false) { __savePath = __savePath + ".bytes"; } __saveAction(); onHUDLocked = null; lockHUD = false; } } GUI.EndGroup(); //CANCEL BTN if (GUI.Button(new Rect((__saveWindowRect.width * 0.5f) - 40, (__saveWindowRect.height) - 35, 80, 25), "Cancel")) { onHUDLocked = null; lockHUD = false; } } GUI.EndGroup(); }; } }