stalomeow / Protobuf-Unity

Protocol Buffers package for Unity.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Protocol Buffers for Unity

openupm

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.

This repository provides a Unity package that utilizes my forked version of Protocol Buffers, 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

    var msg = ExampleMessage.NewFromPool();
    // ...
    msg.Dispose(); // recycle to pool
  • Usage 2

    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; 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<T>.ParseFrom(ReadOnlySpan<byte> data)
    • MessageParser<T>.ParseFrom(ReadOnlySequence<byte> data)
    • MessageExtensions.WriteTo(this IMessage message, Span<byte> output)

    when deserializing/serializing Messages because they do not cause extra GC allocation.

  • Others can be found in Protobuf's Official Document.

  • To be continued...

License

The codes in the Runtime/Core folder are licensed under Google's LICENSE, and the rest are licensed under the MIT LICENSE.

About

Protocol Buffers package for Unity.

License:MIT License


Languages

Language:C# 100.0%