Use Roslyn analyzers, source generators and ruleset files in Unity projects to inspect your code for style, quality, and other issues.
You can use existing analyzer libraries to inspect your code, and write your own analyzers to promote the best practices or conventions within your organization. This page explains how to use Roslyn analyzers and source generators in an empty Unity Project.
Note: Roslyn analyzers are only compatible with the IDEs that Unity publically supports, which are Visual Studio and JetBrains Rider.
For more information about how to write and use Roslyn analyzers, see Microsoft’s Analyzer Configuration and Get started with Roslyn analyzers documentation.
You can use source generators as an additional step in your script compilation process. You can use source generators to add new code while you compile your existing code. Like analyzers, you can use existing source generators or create your own.
Note: 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
, see Microsoft’s System.Text.Json Namespace documentation.
To set up a source generator using Visual Studio:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using System.Text;
namespace ExampleSourceGenerator
{
[Generator]
public class ExampleSourceGenerator : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
System.Console.WriteLine(System.DateTime.Now.ToString());
var sourceBuilder = new StringBuilder(
@"
using System;
namespace ExampleSourceGenerated
{
public static class ExampleSourceGenerated
{
public static string GetTestText()
{
return ""This is from source generator ");
sourceBuilder.Append(System.DateTime.Now.ToString());
sourceBuilder.Append(
@""";
}
}
}
");
context.AddSource("exampleSourceGenerator", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8));
}
public void Initialize(GeneratorInitializationContext context) { }
}
}
using UnityEngine;
public class HelloFromSourceGenerator : MonoBehaviour
{
static string GetStringFromSourceGenerator()
{
return ExampleSourceGenerated.ExampleSourceGenerated.GetTestText();
}
// Start is called before the first frame update
void Start()
{
var output = "Test";
output = GetStringFromSourceGenerator();
Debug.Log(output);
}
}
For more information about source generators, see Microsoft’s Source Generators documentation.
You can limit the scope of analyzers in your project by using assembly definitions, so that they only analyze certain portions of your code.
Unity applies analyzers to all assemblies in your project’s Assets folder, or in any subfolder whose parent folder doesn’t contain an assembly definition file. If an analyzer is in a folder that contains an assembly definition, or a subfolder of such a folder, the analyzer only applies to the assembly generated from that assembly definition, and to any other assembly that references it.
This means, for example, that a package can supply analyzers that only analyze code related to the package, which can help package users to use the package API correctly.
To view information such as the total execution time of your analyzers and source generators or the relative execution times of each analyzer or source generator, go to Preferences > Diagnostic Switches and enable EnableDomainReloadTimings. When enabled, the information is displayed in the console window.
Unity doesn’t support the installation of Roslyn Analyzers or source generators through NuGet directly. The below example uses the ErrorProne.NET.CoreAnalyzers library to demonstrate how to install Roslyn Analyzers and source generators from NuGet:
errorprone.net.coreanalyzers<version-number>\analyzers\dotnet\cd
. The required files should be in this folder, named ErrorProne.NET.Core.dll
, ErrorProne.Net.CoreAnalyzers.dll
, and RuntimeContracts.dll
.Unity recognizes the RoslynAnalyzer label and treats assets with this label as Roslyn Analyzers or source generators. When you assign the label to an analyzer, Unity recompiles scripts within the scope of the analyzer and analyzes the code in those scripts according to the rules in the analyzer. Any scripts that are within the same assembly definition as an analyzer are in that analyzer’s scope. For analyzers in the root level of the Assets folder, Unity considers all files in the project to be in scope. For more information about scope, see Analyzer scope above.
To test that your analyzers work correctly, follow the example below. If you have installed the analyzers correctly, the ErrorProne.NET analyzer raises warnings when it analyzes the code in the example.
Create a new script file named RethrowError.cs
. Copy the following code into this script and save the file:
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();
}
}
When you save the file, Unity recompiles the script and runs any applicable analyzers on the script’s code. When the ErrorProne.NET analyzer is correctly installed, it raises the following warnings in the Console window about the above code:
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.
To define your own rules on how to handle the various warnings and errors that the analyzers in your project raise, you can create a ruleset file. For more information on how to create a custom ruleset, see Microsoft’s Visual Studio documentation on how to create a custom rule set.
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
), and 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 allowed inside the root folder:
Default.Ruleset
Assembly-CSharp.ruleset
Assembly-CSharp-firstpass.ruleset
Assembly-CSharp-Editor.ruleset
Assembly-CSharp-Editor-firstpass.ruleset
Unity でルールセットファイルをテストするには、以下を行います。
RethrowError.cs
.<?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
ファイルでは、以下のルールが定義されています。
After you add the ruleset files to your project, reimport any script that lives in an assembly where the rules should apply. This forces Unity to recompile the assembly using the new ruleset files. After recompilation, you should see two messages in the Console window:
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.
Notice that Unity applies the rules defined in Default.ruleset to both Assets/RethrowError.cs
and Assets/Subfolder/RethrowError.cs
.
In Assets/Subfolder, create a .ruleset file, and give it any name you like (in this exampleHello.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>
This new Hello.ruleset
file tells Unity to print both EPC12 and ERP021 to the Console, without treating them as warnings or errors.
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 リポジトリへのリンクです。