How to analyze Unity projects with PVS-Studio
- Checking the project with PVS-Studio plugins for IDE
- How to check a project from the command line
- Issues that may occur during analysis
Unity projects written in C# can be analyzed by PVS-Studio from the console or IDE. The Unity project you intend to check should be built successfully so that the analysis results are as correct and complete as possible.
You can download the analyzer distribution from this page.
Before you start working with PVS-Studio, you need to enter the license data. You can read more about entering a license here.
Checking the project with PVS-Studio plugins for IDE
How to open the Unity project in Visual Studio and JetBrains Rider
Before you start working with the Unity project code, choose your preferred script editor in the Unity settings. Use the "External Script Editor" parameter on the "External Tools" tab in the "Preferences" window. To open the window, use the "Edit" -> "Preferences" menu option in the Unity editor:
After that, you can open the Unity project in the IDE — use the "Assets" -> "Open C# Project" option in the Unity editor:
You can also open the generated Unity .sln file in the IDE. If there is no .sln file, you need to perform the actions described above in the Unity editor. That way the .sln file will be generated.
PVS-Studio and Visual Studio
You can analyze a Unity project in Visual Studio. For more information on how to use the PVS-Studio plugin, see the documentation.
The analysis can be performed for the following elements:
- the whole Unity project (.sln);
- .csproj projects generated by Assembly Definition;
- individual .cs files.
You can run the analysis from the PVS-Studio plugin submenu via the "Extensions" menu on the top menu bar:
Also, it is possible to start the analysis from the "Solution Explorer" window via the context menu by clicking on the solution element.
The analysis results appear in the "PVS-Studio" window:
PVS-Studio and JetBrains Rider
You can analyze a Unity project from JetBrains Rider. The capabilities of PVS-Studio plugin for JetBrains Rider and its capabilities are similar to those for the Visual Studio plugin. Detailed instructions on how to use the plugin for JetBrains Rider are available at the link.
You can run the analysis from the main menu:
How to check a project from the command line
You can run PVS-Studio from the command line. This way of running PVS-Studio is more convenient if you want to integrate the project analysis into your CI/CD infrastructure.
A guide on how to use the CLI version of PVS-Studio is available here.
You can check the entire solution (sln), individual projects (csproj) or some set of source code files. In the latter case, the path to the .sln/.csproj file is also required.
To analyze C# projects on Windows, use "PVS-Studio_Cmd.exe ", and on Unix-like systems — "pvs-studio-dotnet".
Example of launching PVS-Studio via the CLI on Windows:
PVS-Studio_Cmd.exe -t D:\UnityProjects\MyProject\MyProject.sln
Example of running PVS-Studio via the CLI on Unix-like systems:
pvs-studio-dotnet -t ./MyUnityProject.sln
By default, the analysis report is saved in the following formats:
- plog on Windows;
- json on Linux and macOS.
You can open reports in the PVS-Studio's plugins for IDEs.
Note. Rider does not support reports in the plog format. In addition, the conversion of plog reports is not supported under Linux and macOS.
Below is the PVS-Studio plugin's menu for Visual Studio:
It is possible to convert reports into a more convenient format.
To convert the reports, use the CLI utulity — PlogConverter. The name of the utility depends on the platform. It is "PlogConverter.exe " for Windows platform and "plog-converter" for Unix-like systems. To get more detailed information about the utility, see the documentation.
Example of converting a report into HTML format on Windows (should be entered in one line):
PlogConverter.exe D:\UnityProjects\MyProject\MyProject.plog
-t FullHtml
-o D:\UnityProjects\MyProject\
Example of converting a report into HTML format on Unix-like systems:
plog-converter –t fullhtml ./ ./MyProject.plog
These commands convert the report into HTML format, which is convenient for viewing in a browser and for mailing. Example of a report in the FullHtml format:
Issues that may occur during analysis
The solution file has two projects named "UnityEngine.UI"
If the "Player projects" option is enabled in the C# project generation settings, the Unity editor generates a solution file with duplicate project names. When analyzing a project with PVS-Studio, you can get the error of the following type: The solution file has two projects named "UnityEngine.UI". To perform the analysis, this option must be disabled.
After that, you may need to regenerate project files. The "Regenerate project files" button below is right for this.
Once the duplicates from the sln-file are gone, the analysis should work normally. This problem with duplicate project names is described in more detail in following article.
Do not modify project files during analysis
During the analysis, we recommend that you do not perform any actions that may modify the sln/csproj files or source code files. For example, actions such as changing or creating Assembly Definitions and Assembly Definition References, or creating new scripts.