Rico-E
Rico-E

Reputation: 1844

Let Unity show StyleCop errors

I have a project which is developed with Unity3D. Now I want to add StyleCop to this project.

I have added this awesome package which shows the warnings already in VisualStudio (this is a great benefit over StyleCop Classic). But now I want to see the warnings in Unity and let the build fail. To let the build fail if there are any warnings, I have already added a csc.rsp file with -warnaserror+, but unfortunately, I don't have any warnings in Unity.

Is it possible to get the StyleCop-warnings to the Unity-Editor?

Unity-Version 2019.3.0b7 if that matters.

Edit: Another view of the same issue would be:

How to install an analyzer to roslyn?

Unfortunately I the options to install a roslyn analyzer seem to be to install it as nuget package or as Visual Studio extension. But since both ways integrate the errors into Visual Studio, I need another way to add an analyzer to roslyn, which lets the compiler issue errors which then get displayed in Unity3D.

Edit2: To be absolutely clear: I already can do UnityEngine.Debug.LogError and print every StyleCop error to the UnityConsole. But I want StyleCop to be integrated in the compilation pipeline of unity. So that I cannot press play anymore.

Upvotes: 1

Views: 560

Answers (1)

Simon
Simon

Reputation: 667

Here is a solution if you don't mind to write some little pieces of code.

  • Unity has an editor log file, it records every warning during your build. Like this:

    Assets/SomeScript.cs(134,33): warning CS0618: UnityEngine.Random.RandomRange(float, float) is obsolete: Use Random.Range instead.

  • Unity has a callback IPreprocessBuildWithReport.OnPreprocessBuild , which has the following description:

    Implement this interface to receive a callback before the build is started.

    You can start to monitor the log changes from then on.

  • Unity also has a callback when a build is end, by adding a PostProcessBuildAttribute to a specific function. Here is the description:

    Add this attribute to a method to get a notification just after building the player.

    [Update]

  • Now you've recorded if there are warnings in last build, you can make a flag in memory or in file system, then you add few lines of code to check this flag, if it's true then stop the playing by using EditorApplication.ExitPlaymode()

Upvotes: 0

Related Questions