-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
manager to save/load height map to/from a file added
- Loading branch information
Showing
3 changed files
with
81 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using UnityEngine; | ||
using System.IO; | ||
using System.Runtime.Serialization.Formatters.Binary; | ||
using TerrainGeneration; | ||
using UnityEditor; | ||
|
||
namespace Managers | ||
{ | ||
public class SaveManager : MonoBehaviour | ||
{ | ||
private readonly string fileName = "height_map.dat"; | ||
|
||
[SerializeField] | ||
private TerrainGenerationManager terrainManager; | ||
|
||
public void SaveFloatArray() | ||
{ | ||
var arrayToSave = terrainManager.HeightMap; | ||
string filePath = EditorUtility.SaveFilePanel("Save Height Map as...", "", fileName, "dat"); | ||
|
||
if (string.IsNullOrEmpty(filePath)) | ||
return; | ||
|
||
BinaryFormatter formatter = new(); | ||
using (FileStream stream = new(filePath, FileMode.Create)) | ||
{ | ||
formatter.Serialize(stream, arrayToSave); | ||
} | ||
} | ||
|
||
public void LoadFloatArray() | ||
{ | ||
string filePath = EditorUtility.OpenFilePanel("Load Height Map from...", "", "dat"); | ||
|
||
if (!string.IsNullOrEmpty(filePath)) | ||
{ | ||
BinaryFormatter formatter = new(); | ||
using (FileStream stream = new(filePath, FileMode.Open)) | ||
{ | ||
//TODO Also need to set the size of the map accordingly. Now it can cause errors if loaded map is smaller/bigger than the size that is set. | ||
terrainManager.isLoaded = true; | ||
terrainManager.HeightMap = (float[,])formatter.Deserialize(stream); | ||
} | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,33 @@ | ||
using SharedDefs; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using TerrainGeneration; | ||
using UnityEngine; | ||
using UnityEngine.UIElements; | ||
|
||
public class UIManager : MonoBehaviour | ||
namespace Managers | ||
{ | ||
[SerializeField] | ||
private UIDocument document; | ||
[SerializeField] | ||
private TerrainGenerationManager terrainGenerator; | ||
public class UIManager : MonoBehaviour | ||
{ | ||
[SerializeField] | ||
private UIDocument document; | ||
[SerializeField] | ||
private TerrainGenerationManager terrainGenerator; | ||
[SerializeField] | ||
private SaveManager saveManager; | ||
|
||
private VisualElement root; | ||
private VisualElement root; | ||
|
||
void OnEnable() | ||
{ | ||
root = document.rootVisualElement; | ||
root.Q<Button>("Generate").clicked += () => terrainGenerator.DisplayResult(); | ||
} | ||
void OnEnable() | ||
{ | ||
root = document.rootVisualElement; | ||
root.Q<Button>("Generate").clicked += () => terrainGenerator.DisplayResult(); | ||
root.Q<Button>("Save").clicked += () => saveManager.SaveFloatArray(); | ||
root.Q<Button>("Load").clicked += () => saveManager.LoadFloatArray(); | ||
} | ||
|
||
void OnDisable() | ||
{ | ||
root.Q<Button>("Generate").clicked -= () => terrainGenerator.DisplayResult(); | ||
void OnDisable() | ||
{ | ||
root.Q<Button>("Generate").clicked -= () => terrainGenerator.DisplayResult(); | ||
root.Q<Button>("Save").clicked -= () => saveManager.SaveFloatArray(); | ||
root.Q<Button>("Load").clicked -= () => saveManager.LoadFloatArray(); | ||
} | ||
} | ||
} |