-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathSerialization.cs
More file actions
57 lines (52 loc) · 1.31 KB
/
Serialization.cs
File metadata and controls
57 lines (52 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class Serialization : MonoBehaviour
{
[System.Serializable]
struct Map
{
public float[,] heightmap;
public float[,] alphamap;
};
void PrintArray (float[,] table)
{
for (int x=0;x<table.GetLength(0);x++)
{
for (int y=0;y<table.GetLength(1);y++)
{
Debug.Log(table[x,y]);
}
}
}
void Serialize (string path, Map source)
{
try
{
BinaryFormatter bin = new BinaryFormatter();
FileStream writer = new FileStream(path,FileMode.Create);
bin.Serialize(writer, (object)source);
writer.Close();
}
catch (IOException) {}
}
Map Deserialize (string path)
{
FileStream reader = new FileStream(path, FileMode.Open, FileAccess.Read);
BinaryFormatter bin = new BinaryFormatter();
Map target = (Map) bin.Deserialize(reader);
reader.Close();
return target;
}
void Start()
{
string Path = Application.dataPath + "/StreamingAssets/" + "map.data";
Map source;
source.heightmap = new float[2,2] {{1.0f,2.0f},{3.0f,4.0f}};
source.alphamap = new float[2,2] {{5.0f,6.0f},{7.0f,8.0f}};
Serialize (Path,source);
Map destination = Deserialize (Path);
PrintArray(destination.heightmap);
PrintArray(destination.alphamap);
}
}