https://github.com/stalomeow/protobuf-unity
Protocol Buffers package for Unity.
https://github.com/stalomeow/protobuf-unity
protobuf protocol-buffers serialization unity unity3d
Last synced: 8 months ago
JSON representation
Protocol Buffers package for Unity.
- Host: GitHub
- URL: https://github.com/stalomeow/protobuf-unity
- Owner: stalomeow
- License: mit
- Created: 2023-09-03T15:25:50.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-03T13:44:06.000Z (over 2 years ago)
- Last Synced: 2025-03-28T09:51:28.256Z (about 1 year ago)
- Topics: protobuf, protocol-buffers, serialization, unity, unity3d
- Language: C#
- Homepage:
- Size: 649 KB
- Stars: 18
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Protocol Buffers for Unity
[](https://openupm.cn/packages/com.stalomeow.google-protobuf/)
**None of the repo, the tool, nor the repo owner is affiliated with, or sponsored or authorized by, Google or its affiliates.**
## Overview
Protocol Buffers (a.k.a., protobuf) are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data. For more information, please check the original repository at [https://github.com/protocolbuffers/protobuf](https://github.com/protocolbuffers/protobuf).
This repository provides a Unity package that utilizes [my forked version of Protocol Buffers](https://github.com/stalomeow/protobuf/tree/unity), incorporating several optimizations.
## Requirements
- Unity >= 2021.3.
- Mono Cecil >= 1.10.1.
## Highlights
### Fast String
Faster string parsing by defining `GOOGLE_PROTOBUF_SUPPORT_FAST_STRING`.
### Message Pool & Protoc
This package provides a thread-safe object pool for Protobuf Messages.
- Usage 1
``` csharp
var msg = ExampleMessage.NewFromPool();
// ...
msg.Dispose(); // recycle to pool
```
- Usage 2
``` csharp
var msg = ExampleMessage.Parser.ParseFrom(data);
// ...
msg.Dispose(); // recycle to pool
```
The protoc that supports this feature is available here: [https://github.com/stalomeow/protobuf/tree/unity](https://github.com/stalomeow/protobuf/tree/unity); simply compile it according to the documentation, and you'll be able to use it.
## Best Practices in Unity
- **DO NOT** use `foreach` on a Repeated/Map Field because it will cause extra GC allocation. Simply use `for` instead (if possible).
- **DO** use
- `MessageParser.ParseFrom(ReadOnlySpan data)`
- `MessageParser.ParseFrom(ReadOnlySequence data)`
- `MessageExtensions.WriteTo(this IMessage message, Span output)`
when deserializing/serializing Messages because they do not cause extra GC allocation.
- Others can be found in [Protobuf's Official Document](https://protobuf.dev/programming-guides/dos-donts/).
- To be continued...
## License
The codes in the Runtime/Core folder are licensed under [Google's LICENSE](Runtime/Core/LICENSE), and the rest are licensed under [the MIT LICENSE](LICENSE).