「Unity/MessagePack」の版間の差分
(→CodeGeneratorを使う) |
(→mpcインストール) |
||
行70: | 行70: | ||
Serializer.generated生成 | Serializer.generated生成 | ||
− | + | <pre> | |
+ | $ mpc -i Assembly-CSharp.csproj -o ./Assets/Script/Serializer.generated.cs | ||
+ | |||
+ | Project Compilation Start:Assembly-CSharp | ||
+ | Project Compilation Complete:00:00:00.1201760 | ||
+ | Method Collect Start | ||
+ | Method Collect Complete:00:00:00.0297969 | ||
+ | Output Generation Start | ||
+ | [Out]./Assets/Script/Serializer.generated.cs | ||
+ | Output Generation Complete:00:00:00.0046382 | ||
+ | </pre> | ||
参考:https://limegame.hatenablog.com/entry/2023/05/30/164100 | 参考:https://limegame.hatenablog.com/entry/2023/05/30/164100 | ||
− | ====mpc実行で"Developer Pack をダウンロードして" | + | ====mpc実行で"Developer Pack をダウンロードして"とエラーが出た場合==== |
<pre> | <pre> | ||
/usr/local/share/dotnet/sdk/6.0.427/Microsoft.Common.CurrentVersion.targets(1220,5): error MSB3644: .NETFramework,Version=v4.7.1 の参照アセンブリが見つかりませんでした。この問題を解決するには、このフレームワーク バージョンの Developer Pack (SDK/Targeting Pack) をインストールするか、アプリケーションのターゲットを再設定してください。https://aka.ms/msbuild/developerpacks で .NET Framework Developer Pack をダウンロードできます。 | /usr/local/share/dotnet/sdk/6.0.427/Microsoft.Common.CurrentVersion.targets(1220,5): error MSB3644: .NETFramework,Version=v4.7.1 の参照アセンブリが見つかりませんでした。この問題を解決するには、このフレームワーク バージョンの Developer Pack (SDK/Targeting Pack) をインストールするか、アプリケーションのターゲットを再設定してください。https://aka.ms/msbuild/developerpacks で .NET Framework Developer Pack をダウンロードできます。 | ||
</pre> | </pre> | ||
+ | 以下のCodeGeneratorを使ってみる。 | ||
==CodeGeneratorを使う== | ==CodeGeneratorを使う== |
2025年6月13日 (金) 02:58時点における版
MessagePackとは
効率の良いバイナリ形式のオブジェクト・シリアライズ フォーマット
公式:https://msgpack.org/ja.html
c#のPackage
5つある C# msgpack C# ymofen C# caesay C# mlsomers C# neuecc
neueccのMessagePack導入
PackageManagerからMessagePack-v3のインストール
- MessagePack annotations for C# (NuGet)
- MessagePack for C# (NuGet)
- MessagePack Source Generator and Analyzer (NuGet)
もしくは、Packages/manifest.jsonに以下を追加
{ "dependencies": { "org.nuget.messagepack": "3.1.3",
v3になって、mpcはなくなった。(2024/12/06)
https://neue.cc/2024/12/06_MessagePack_v3.html
PackageManagerからMessagePack-v2のインストール
v3をインストールした場合は、v2は不要
neuecc https://github.com/MessagePack-CSharp/MessagePack-CSharp
PackageManagerのgiturlに
https://github.com/neuecc/MessagePack-CSharp.git?path=src/MessagePack.UnityClient/Assets/Scripts/MessagePack#v2.6.100-alpha
を追加。
もしくは、Packages/manifest.jsonに以下を追加
{ "dependencies": { "com.neuecc.messagepack": "https://github.com/neuecc/MessagePack-CSharp.git?path=src/MessagePack.UnityClient/Assets/Scripts/MessagePack#v2.6.100-alpha",
"namespace name 'NET' does not exist"エラーが出る時
error CS0234: The type or namespace name 'NET' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
↓の手順で、Microsoft.NET.StringToolsをインストールする
nugetでMicrosoft.NET.StringToolsインストール
unityメイン/Edit/ProjectSettings/PackageManagerから以下を追加
Name: OpenUPM URL: https://package.openupm.com Scope(s): org.nuget
unityメイン/PackageManager/MyRegistoryでMicrosoft.NET.StringTools検索してインストール
mpcインストール
v2では必要。v3では不要。
netインストール(net7) https://dotnet.microsoft.com/ja-jp/download
mpcインストール
dotnet tool install --global MessagePack.Generator
Serializer.generated生成
$ mpc -i Assembly-CSharp.csproj -o ./Assets/Script/Serializer.generated.cs Project Compilation Start:Assembly-CSharp Project Compilation Complete:00:00:00.1201760 Method Collect Start Method Collect Complete:00:00:00.0297969 Output Generation Start [Out]./Assets/Script/Serializer.generated.cs Output Generation Complete:00:00:00.0046382
参考:https://limegame.hatenablog.com/entry/2023/05/30/164100
mpc実行で"Developer Pack をダウンロードして"とエラーが出た場合
/usr/local/share/dotnet/sdk/6.0.427/Microsoft.Common.CurrentVersion.targets(1220,5): error MSB3644: .NETFramework,Version=v4.7.1 の参照アセンブリが見つかりませんでした。この問題を解決するには、このフレームワーク バージョンの Developer Pack (SDK/Targeting Pack) をインストールするか、アプリケーションのターゲットを再設定してください。https://aka.ms/msbuild/developerpacks で .NET Framework Developer Pack をダウンロードできます。
以下のCodeGeneratorを使ってみる。
CodeGeneratorを使う
unityメインメニュー/Window/MessagePack/CodeGenerator
Assets/Scripts/MessagePack/Unity/MessagePackWindow.cs の以下部分を変更すると、CodeGeneratorが、ちゃんと出た。
-ProcessStartAsync("dotnet", "tool list -g"); +ProcessStartAsync("/usr/local/share/dotnet/dotnet", "tool list -g");
参考:https://qiita.com/IShix/items/490c3670ad771adb3c3d
dotnetインストール
https://dotnet.microsoft.com/ja-jp/download
% dotnet --list-sdks 9.0.203 [/usr/local/share/dotnet/sdk]
dotnetのpath確認
which dotnet /usr/local/share/dotnet/dotnet
MessagePackサンプル
User.cs
using MessagePack; [MessagePackObject] public class User { [Key(0)] public string Name { get; set; } [Key(1)] public int Age { get; set; } }
処理
var user = new User(); user.Name = "taro"; user.Age = 10; var serialized = MessagePackSerializer.Serialize(user); Debug.Log("シリアライズされたデータ\n" + BitConverter.ToString(serialized)); var deserialized = MessagePackSerializer.Deserialize<User>(serialized); Debug.Log("デシリアライズされたデータ\n" + deserialized.Name + " Age=" + deserialized.Age);
出力
シリアライズされたデータ 92-A4-74-61-72-6F-0A デシリアライズされたデータ taro Age=10
オプション設定追加
sample
StaticCompositeResolver.Instance.Register( MessagePack.Unity.UnityResolver.Instance, MessagePack.Unity.Extension.UnityBlitWithPrimitiveArrayResolver.Instance, MessagePack.Resolvers.StandardResolver.Instance ); var options = MessagePackSerializerOptions.Standard.WithResolver(StaticCompositeResolver.Instance); MessagePackSerializer.DefaultOptions = options;
公式より:https://github.com/MessagePack-CSharp/MessagePack-CSharp