はじめに
今回はSystem.Text.Jsonのv8.0.1をUnityで動作させる手順について紹介したいと思います。
またSystem.Text.JsonをUnityの型に対応させる記事を前に書いたので、気になる方はそちらもチェックしてみてください。
engineering.dena.com
その前に
System.Text.JsonのバージョンはUnityのMicrosoft.CodeAnalysis.CSharpのバージョンに依存しています。System.Text.Json v8.0.1のソース生成を行うSystem.Text.Json.SourceGenerationはroslyn3.11,roslyn4.0,roslyn4.4に対応しています。
一応Unity2023.3の公式ドキュメントではSystem.Text.Json v6.0.0-previewのみ対応しているとのことです。
Unity only supports version 6.0.0-preview of the ‘System.Text.Json’ namespace. If you want to use this namespace in your application, ensure you use version 6.0.0-preview. For more information about System.Text.Json, refer to Microsoft’s System.Text.Json Namespace documentation
// DeepL翻訳
Unityは'System.Text.Json'名前空間のバージョン6.0.0-previewのみをサポートしています。アプリケーションでこの名前空間を使用する場合は、バージョン6.0.0-previewを使用してください。System.Text.Jsonの詳細については、MicrosoftのSystem.Text.Json Namespaceドキュメントを参照してください。
しかし少なくともUnity2022.3.10f1以降ではMicrosoft.CodeAnalysis.CSharp v3.11以降を利用しており、System.Text.Json v8.0.1が動作することを確認できました。(最低限のバージョンは調べていませんのでご注意ください)
手順
nuget.orgからdllを取得する
nuget.orgからSystem.Text.Jsonと依存するパッケージを取得します。
- System.Text.Json v8.0.1
- System.Runtime.CompilerServices.Unsafe v6.0.0
- System.Text.Encodings.Web v8.0.0
- Microsoft.Bcl.AsyncInterfaces v8.0.0
右側にあるDownload packageからダウンロードができます。またダウンロードするバージョンがあっているか注意してください。

.nupkgファイルがダウンロードできますが、拡張子を.zipに書き換えることで展開することができます。4つのファイルに対してそれぞれ操作を行なってください。
.dllをUnityにインポートする
UnityのPluginsフォルダ以下に以下の.dllをインポートしてください。
system.text.json.8.0.1/lib/netstandard2.0/System.Text.Json.dllsystem.text.json.8.0.1/analyzers/dotnet/roslyn3.11/cs/System.Text.Json.SourceGeneration.dllsystem.runtime.compilerservices.unsafe.6.0.0/lib/netstandard2.0/System.Runtime.CompilerServices.Unsafe.dllsystem.text.encodings.web.8.0.0/lib/netstandard2.0/System.Text.Encodings.Web.dllmicrosoft.bcl.asyncinterfaces.8.0.0/lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll

AssetLabelをつける
System.Text.Json.SourceGeneratoin.dllはSourceGeneratorを利用しているため、RoslynAnalyzerというAsset Labelをつける必要があります。これはUnityが.csprojではなく.asmdefを利用している都合上の設定です。
System.Text.Json.SourceGenerator.asmdefのAsset LabelにRoslynAnalyzerと記述してあげます。
