-
Notifications
You must be signed in to change notification settings - Fork 14
/
ObjectManager.cs
90 lines (81 loc) · 2.85 KB
/
ObjectManager.cs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Two examples how to read/write objects from/to file, without serialization/deserialization.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using System.IO;
[StructLayout(LayoutKind.Sequential, Pack = 1)] //read more: http://www.pzielinski.com/?p=1337
public class Tree
{
[MarshalAs(UnmanagedType.LPStr, SizeConst = 1)] //allocate memory for one element of type string
public string Title;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)] //allocate memory for five elements of type integer
public int[] Values;
}
public class ObjectManager : MonoBehaviour
{
public enum State {Write, Read}
public State Mode = State.Write;
// Method 1:
void WriteObjectToFile<T> (T structure, string path)
{
int size = Marshal.SizeOf(structure);
byte[] bytes = new byte[size];
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
IntPtr ptr = handle.AddrOfPinnedObject();
Marshal.StructureToPtr(structure, ptr, false);
handle.Free();
File.WriteAllBytes (path, bytes);
}
T ReadObjectFromFile<T> (string path)
{
byte[] bytes = File.ReadAllBytes (path);
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
IntPtr ptr = handle.AddrOfPinnedObject();
T structure = (T)Marshal.PtrToStructure(ptr, typeof(T));
handle.Free();
return structure;
}
///////////////////////////////////////////////////////////////
// Method 2:
void SaveObjectToFile<T> (T structure, string path)
{
int size = Marshal.SizeOf(structure);
byte[] bytes = new byte[size];
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(structure, ptr, false);
Marshal.Copy(ptr, bytes, 0, size);
Marshal.FreeHGlobal(ptr);
File.WriteAllBytes (path, bytes);
}
T LoadObjectFromFile<T> (string path)
{
byte[] bytes = File.ReadAllBytes (path);
int size = bytes.Length;
IntPtr ptr = Marshal.AllocHGlobal(size);
Marshal.Copy(bytes, 0, ptr, size);
T structure = (T)Marshal.PtrToStructure(ptr, typeof(T));
Marshal.FreeHGlobal(ptr);
return structure;
}
///////////////////////////////////////////////////////////////
void Start()
{
if (Mode == State.Write)
{
Tree tree = new Tree();
tree.Title = "Apple";
tree.Values = new int[5] {8, 16, 33, 47, 99};
WriteObjectToFile (tree, Path.Combine(Application.streamingAssetsPath, "test.bin"));
//SaveObjectToFile (tree, Path.Combine(Application.streamingAssetsPath, "test.bin"));
}
if (Mode == State.Read)
{
Tree tree = ReadObjectFromFile<Tree> (Path.Combine(Application.streamingAssetsPath, "test.bin"));
//Tree tree = LoadObjectFromFile<Tree> (Path.Combine(Application.streamingAssetsPath, "test.bin"));
Debug.Log(tree.Title);
for (int i = 0; i < tree.Values.Length; i++) Debug.Log(tree.Values[i]);
}
}
}