Skip to content

Commit

Permalink
manager to save/load height map to/from a file added
Browse files Browse the repository at this point in the history
  • Loading branch information
vyshnovka committed Feb 5, 2024
1 parent 3bb6c54 commit 11de9eb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 18 deletions.
47 changes: 47 additions & 0 deletions Assets/Scripts/Managers/SaveManager.cs
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);
}
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Managers/SaveManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 23 additions & 18 deletions Assets/Scripts/Managers/UIManager.cs
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();
}
}
}

0 comments on commit 11de9eb

Please sign in to comment.