「Unity/MessagePack」の版間の差分
| (同じ利用者による、間の27版が非表示) | |||
| 1行目: | 1行目: | ||
==MessagePackとは== | ==MessagePackとは== | ||
効率の良いバイナリ形式のオブジェクト・シリアライズ | 効率の良いバイナリ形式のオブジェクト・シリアライズ フォーマット。jsonみたいなもので、署名機能などはない。 | ||
公式:https://msgpack.org/ja.html | 公式:https://msgpack.org/ja.html | ||
| 13行目: | 13行目: | ||
==neueccのMessagePack導入== | ==neueccのMessagePack導入== | ||
===PackageManagerからMessagePack-v3のインストール=== | |||
*MessagePack annotations for C# (NuGet) | |||
*MessagePack for C# (NuGet) | |||
*MessagePack Source Generator and Analyzer (NuGet) | |||
もしくは、Packages/manifest.jsonに以下を追加 | |||
<pre> | |||
{ | |||
"dependencies": { | |||
"org.nuget.messagepack": "3.1.3", | |||
</pre> | |||
====v3になって、mpcはなくなった。(2024/12/06)==== | |||
https://neue.cc/2024/12/06_MessagePack_v3.html | |||
===PackageManagerからMessagePack-v2のインストール=== | |||
v3をインストールした場合は、v2は不要 | |||
neuecc | neuecc | ||
https://github.com/MessagePack-CSharp/MessagePack-CSharp | https://github.com/MessagePack-CSharp/MessagePack-CSharp | ||
| 26行目: | 45行目: | ||
"com.neuecc.messagepack": "https://github.com/neuecc/MessagePack-CSharp.git?path=src/MessagePack.UnityClient/Assets/Scripts/MessagePack#v2.6.100-alpha", | "com.neuecc.messagepack": "https://github.com/neuecc/MessagePack-CSharp.git?path=src/MessagePack.UnityClient/Assets/Scripts/MessagePack#v2.6.100-alpha", | ||
</pre> | </pre> | ||
==="namespace name 'NET' does not exist"エラーが出る時=== | ==="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?) | error CS0234: The type or namespace name 'NET' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) | ||
| 33行目: | 53行目: | ||
unityメイン/Edit/ProjectSettings/PackageManagerから以下を追加 | unityメイン/Edit/ProjectSettings/PackageManagerから以下を追加 | ||
<pre> | <pre> | ||
Name: | Name: OpenUPM | ||
URL: https:// | URL: https://package.openupm.com | ||
Scope(s): org.nuget | Scope(s): org.nuget | ||
</pre> | </pre> | ||
| 41行目: | 61行目: | ||
===mpcインストール=== | ===mpcインストール=== | ||
netインストール | v2では必要。v3では不要。 | ||
netインストール(net7) | |||
https://dotnet.microsoft.com/ja-jp/download | |||
mpcインストール | mpcインストール | ||
| 47行目: | 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 | ||
===="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を使う== | |||
unityメインメニュー/Window/MessagePack/CodeGenerator | |||
Assets/Scripts/MessagePack/Unity/MessagePackWindow.cs | |||
の以下部分を変更すると、CodeGeneratorが、macで、ちゃんと出たし、Generateもできた。 | |||
<pre> | |||
-ProcessStartAsync("dotnet", "tool list -g"); | |||
+ProcessStartAsync("/usr/local/share/dotnet/dotnet", "tool list -g"); | |||
-var log = await ProcessHelper.InvokeProcessStartAsync("mpc", commnadLineArguments); | |||
+var log = await ProcessHelper.InvokeProcessStartAsync("/Users/account1/.dotnet/tools/mpc", commnadLineArguments); | |||
</pre> | |||
入力 | |||
<pre> | |||
-i input path | |||
../Assembly-CSharp.csproj | |||
-o output filepath | |||
Scripts/Serializer.generated.cs | |||
</pre> | |||
出力 | |||
<pre> | |||
Generate MessagePack Files, command:-i ../Assembly-CSharp.csproj -o Scripts/Serializer.generated.cs | |||
UnityEngine.Debug:Log (object) | |||
</pre> | |||
出力ログは出るけど、ファイルはできなかった・・。なぜだろう・・。やってることは、上のmpcコマンド処理と同じなので、そちらが動いてファイルができてれば問題ない。 | |||
参考:https://qiita.com/IShix/items/490c3670ad771adb3c3d | |||
===dotnetインストール=== | |||
https://dotnet.microsoft.com/ja-jp/download | |||
<pre> | |||
% dotnet --list-sdks | |||
9.0.203 [/usr/local/share/dotnet/sdk] | |||
</pre> | |||
===dotnetのpath確認=== | |||
<pre> | |||
$ which dotnet | |||
/usr/local/share/dotnet/dotnet | |||
</pre> | |||
===mpcのpath確認=== | |||
<pre> | |||
$ which mpc | |||
/Users/account1/.dotnet/tools/mpc | |||
</pre> | |||
==MessagePackサンプル== | |||
User.cs | |||
<pre> | |||
using MessagePack; | |||
[MessagePackObject] | |||
public class User | |||
{ | |||
[Key(0)] | |||
public string Name { get; set; } | |||
[Key(1)] | |||
public int Age { get; set; } | |||
} | |||
</pre> | |||
処理 | |||
<pre> | |||
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); | |||
</pre> | |||
出力 | |||
<pre> | |||
シリアライズされたデータ | |||
92-A4-74-61-72-6F-0A | |||
デシリアライズされたデータ | |||
taro Age=10 | |||
</pre> | |||
==オプション設定追加== | |||
sample | |||
<pre> | |||
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; | |||
</pre> | |||
公式より:https://github.com/MessagePack-CSharp/MessagePack-CSharp | |||
==参考== | ==参考== | ||
https://qiita.com/p_a_sta/items/2b9522888b7488aafb7c | https://qiita.com/p_a_sta/items/2b9522888b7488aafb7c | ||
2025年6月13日 (金) 20:25時点における最新版
MessagePackとは
効率の良いバイナリ形式のオブジェクト・シリアライズ フォーマット。jsonみたいなもので、署名機能などはない。
公式: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が、macで、ちゃんと出たし、Generateもできた。
-ProcessStartAsync("dotnet", "tool list -g");
+ProcessStartAsync("/usr/local/share/dotnet/dotnet", "tool list -g");
-var log = await ProcessHelper.InvokeProcessStartAsync("mpc", commnadLineArguments);
+var log = await ProcessHelper.InvokeProcessStartAsync("/Users/account1/.dotnet/tools/mpc", commnadLineArguments);
入力
-i input path ../Assembly-CSharp.csproj -o output filepath Scripts/Serializer.generated.cs
出力
Generate MessagePack Files, command:-i ../Assembly-CSharp.csproj -o Scripts/Serializer.generated.cs UnityEngine.Debug:Log (object)
出力ログは出るけど、ファイルはできなかった・・。なぜだろう・・。やってることは、上のmpcコマンド処理と同じなので、そちらが動いてファイルができてれば問題ない。
参考: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
mpcのpath確認
$ which mpc /Users/account1/.dotnet/tools/mpc
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