본문 바로가기
Dev/Unity

Google.Protobuf

by wenect 2023. 4. 27.
using System.Diagnostics;
using Google.Protobuf;
using System.IO;
using Wenect;

namespace MyProtoBuf
{
    /// <summary>
    /// https://github.com/protocolbuffers/protobuf/releases/tag/v22.3
    /// 
    /// https://protobuf.dev/getting-started/csharptutorial/
    ///
    /// https://protobuf.dev/reference/csharp/api-docs/
    /// 
    /// </summary>
    internal class Program
    {
        static void Main(string[] args)
        {
            Person person1 = new Person();
            person1.Id = 1;
            person1.Name = "홍길동";
            person1.Email = "gildong@gmail.com";
            person1.Phones.Add(new Person.Types.PhoneNumber()
                {
                    Number = "123-4567",
                    Type = Person.Types.PhoneType.Mobile
                });
            Person person0 = new Person()
            {
                Id = 2,
                Name = "Foo",
                Email = "foo@gmail.com",
                Phones = { new Person.Types.PhoneNumber() { Number = "111-2222" } }
            };

            //--------------------------------------------------------------//
            // serialize 
            byte[] bytes;
            using (MemoryStream stream = new MemoryStream())
            {
                person1.WriteTo(stream);
                bytes = stream.ToArray();
            }
            // deserialize
            Person person2 = Person.Parser.ParseFrom(bytes);
            Debug.WriteLine(person2.ToString());

            //--------------------------------------------------------------//
            AddressBook addressBook = new AddressBook();
            addressBook.Peoples.Add(person1);
            addressBook.Peoples.Add(person0);

            byte[] bytes2;
            using (MemoryStream stream = new MemoryStream())
            {
                addressBook.WriteTo(stream);
                bytes2 = stream.ToArray();
            }
            AddressBook addressBook2 = AddressBook.Parser.ParseFrom(bytes2);
            Debug.WriteLine(addressBook2.ToString());
        }
    }
}

구글의 Protobuf 유니티 연동

Protocol Buffers Documentation (protobuf.dev)

C# Generated Code Guide | Protocol Buffers Documentation (protobuf.dev)

 

C# Generated Code Guide

This topic describes exactly what C# code the protocol buffer compiler generates for protocol definitions using proto3 syntax.

protobuf.dev

유니티+Mediapipe - Blazepose연동시  ProtoBuf를 이미 포함하고 있어서 해당 라이브러리를 사용해도됩니다.

또는 Unity Package Version Controller을 설치하여 최신 버전을 사용하는 방식을 유도 가능하다고 합니다.(미확인)

 

'Dev > Unity' 카테고리의 다른 글

Unity_Chart  (0) 2024.01.21
UniRx_vs_UniTask  (0) 2023.11.26
Excel to SQLite  (0) 2023.09.07
오브젝트의 축 정렬하기(Quaternion.FromToRotation, Quaternion.LookRotation)  (0) 2023.05.05

댓글