https://github.com/insthync/unity-serialization-surrogates
Serialization surrogates for Unity's structs
https://github.com/insthync/unity-serialization-surrogates
Last synced: about 2 months ago
JSON representation
Serialization surrogates for Unity's structs
- Host: GitHub
- URL: https://github.com/insthync/unity-serialization-surrogates
- Owner: insthync
- License: mit
- Created: 2018-02-20T08:10:00.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2025-03-02T17:57:00.000Z (over 1 year ago)
- Last Synced: 2025-12-28T01:51:20.557Z (6 months ago)
- Language: C#
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# unity3d-serialization-surrogates
Serialization surrogates for Unity's structs
**Can make formatter to serialize and deserialize:**
* Color
* Quaternion
* Vector2Int
* Vector2
* Vector3Int
* Vector3
* Vector4
## Usage Example
**Serialize**
```csharp
using (var memoryStream = new MemoryStream())
{
var binaryFormatter = new BinaryFormatter();
// Setup Unity's structs serialization surrogates
var surrogateSelector = new SurrogateSelector();
surrogateSelector.AddAllUnitySurrogate();
binaryFormatter.SurrogateSelector = surrogateSelector;
// Serialize and put to packet
binaryFormatter.Serialize(memoryStream, data);
memoryStream.Flush();
memoryStream.Seek(0, SeekOrigin.Begin);
var bytes = memoryStream.ToArray();
}
```
**Deserialize**
```csharp
using (MemoryStream memoryStream = new MemoryStream(bytes))
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
// Setup Unity's structs serialization surrogates
var surrogateSelector = new SurrogateSelector();
surrogateSelector.AddAllUnitySurrogate();
binaryFormatter.SurrogateSelector = surrogateSelector;
// Deserialize
var data = binaryFormatter.Deserialize(memoryStream);
}
```