Unity プロジェクトで Roslyn アナライザーとルールセットファイルを使用して、コードのスタイルや品質、その他の問題を検査します。
You can use existing analyzer libraries to inspect your code, as well as write your own analyzers to promote the best practices or conventions within your organization. This page explains how to use Roslyn analyzers in an empty Unity Project.
To use an existing Roslyn analyzer library, install the ErrorProne.NET.CoreAnalyzers library from NuGet inside your project. Then open the .csproj
file. There are three .dll files included in the project as analyzers:
<ItemGroup>
<Analyzer Include="packages\ErrorProne.NET.CoreAnalyzers.0.1.2\analyzers\dotnet\cs\ErrorProne.NET.Core.dll" />
<Analyzer Include="packages\ErrorProne.NET.CoreAnalyzers.0.1.2\analyzers\dotnet\cs\ErrorProne.Net.CoreAnalyzers.dll" />
<Analyzer Include="packages\ErrorProne.NET.CoreAnalyzers.0.1.2\analyzers\dotnet\cs\RuntimeContracts.dll" />
</ItemGroup>
Move the three .dll files into the Assets
folder (or any folder nested in the Assets
folder) in your Unity project. In the Editor, select the .dll file. In the Plugin Inspector, under Select platforms for plugin, disable Any Platform. Under Include platforms, disable Editor and Standalone platforms. Then, give all of the DLLs a new label called RoslynAnalyzer
.
To test that everything works correctly, save a file named RethrowError.cs
with the following code in your project’s Assets
folder. This code causes ErrorProne.NET
to raise warnings about incorrect exception propagation and suspicious exception handling:
using System;
using UnityEngine;
public class RethrowError : MonoBehaviour
{
void Update()
{
try
{
DoSomethingInteresting();
}
catch (Exception e)
{
Debug.Log(e.Message);
throw e;
}
}
private void DoSomethingInteresting()
{
throw new System.NotImplementedException();
}
}
これを実行すると、コンソールウィンドウに以下の 2 つの警告が表示されます。
Assets\RethrowError.cs(14,23): warning EPC12: Suspicious exception handling: only e.Message is observed in exception block.
Assets\RethrowError.cs(15,19): warning ERP021: Incorrect exception propagation. Use throw; instead.
プロジェクトのアナライザーが発するさまざまな警告やエラーを処理する方法を独自のルールで定義するには、ルールセットファイルを作成します。カスタムルールセットの作成方法の詳細については、Microsoft の Visual Studio のドキュメント ルール セットをカスタマイズする を参照してください。
In the Assets
root folder, place a ruleset file named Default.ruleset
. The rules you define in Default.ruleset
apply to all predefined assemblies (for example Assembly-CSharp.dll
), as well as all assemblies that are built using .asmdef
files.
To override the rules in Default.ruleset
for a predefined assembly, create a .ruleset
file in the root folder with the name [PredefinedAssemblyName].ruleset
. For example, the rules in Assembly-CSharp.ruleset
apply to the code in Assembly-CSharp.dll
. Only these .ruleset
files are permitted inside the root folder:
Default.Ruleset
Assembly-CSharp.ruleset
Assembly-CSharp-firstpass.ruleset
Assembly-CSharp-Editor.ruleset
Assembly-CSharp-Editor-firstpass.ruleset
Unity でルールセットファイルをテストするには、以下を行います。
Assets
フォルダーの中に Subfolder
という名前のサブフォルダーを作成します。Subfolder
内で以下を行います。
.asmdef
ファイルを作成します。RethrowError.cs
の複製を保存します。Assets
の中に Default.ruleset
ファイルを作成します。<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="New Rule Set" Description=" " ToolsVersion="10.0">
<Rules AnalyzerId="ErrorProne.NET.CodeAnalyzers" RuleNamespace="ErrorProne.NET.CodeAnalyzers">
<Rule Id="ERP021" Action="Error" />
<Rule Id="EPC12" Action="None" />
</Rules>
</RuleSet>
Default.ruleset
ファイルでは、以下のルールが定義されています。
EPC12
の抑制。疑わしい例外処理についての警告です。ERP021
の発生。不正な例外伝播に関する警告です。プロジェクトにルールセットファイルを追加した後、ルールを適用するアセンブリ内にあるスクリプトをすべて再インポートします。これを行うと、Unity は新しいルールセットファイルを使用してアセンブリを再コンパイルします。再コンパイルの後、コンソールウィンドウに以下の 2 つのメッセージが表示されます。
Assets\Subfolder\RethrowError.cs(15,19): error ERP021: Incorrect exception propagation. Use throw; instead.
Assets\RethrowError.cs(15,19): error ERP021: Incorrect exception propagation. Use throw; instead.
Unity は Default.ruleset
で定義されたルールを Assets/RethrowError.cs
と Assets/Subfolder/RethrowError.cs
の両方に適用していることに注目してください。
Assets/Subfolder
に .ruleset
ファイルを作成し、任意の名前を付けます (この例では Hello.ruleset
)。
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="New Rule Set" Description=" " ToolsVersion="10.0">
<Rules AnalyzerId="ErrorProne.NET.CodeAnalyzers" RuleNamespace="ErrorProne.NET.CodeAnalyzers">
<Rule Id="ERP021" Action="Info" />
<Rule Id="EPC12" Action="Info" />
</Rules>
</RuleSet>
この新しい Hello.ruleset
ファイルは、Unity に EPC12
と ERP021
の両方を警告やエラーとして扱わずに、コンソールに出力するように指示します。
Unity がプロジェクトを再度コンパイルすると、コンソールウィンドウに以下のメッセージが表示されます。
Assets\Subfolder\RethrowError.cs(14,23): info EPC12: Suspicious exception handling: only e.Message is observed in exception block.
Assets\Subfolder\RethrowError.cs(15,19): info ERP021: Incorrect exception propagation. Use throw; instead.
Assets\RethrowError.cs(15,19): error ERP021: Incorrect exception propagation. Use throw; instead.
Default.ruleset
のルールは Assets\RethrowError.cs
にはまだ適用されますが、Assets\Subfolder\RethrowError.cs
には適用されません。Hello.ruleset
のルールがそれをオーバーライドするためです。
許可されているすべてのルールセットのアクションファイルの詳細については、Visual Studio のドキュメント コード分析ルールセットエディターを使用する を参照してください。
以下は、よく使われる Roslyn アナライザーライブラリの Github リポジトリへのリンクです。
2020.2 に Roslyn アナライザーの互換性を追加ニューイン20202
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.