Making our Unity Analyzers Open-Source

Jb

February 11th, 2020

Here at the Visual Studio Tools for Unity team our mission is to improve the productivity of Unity developers. In Visual Studio 2019 we’ve introduced our Unity Analyzers, a collection of Unity specific code diagnostics and code fixes. Today we’re excited to make our Unity Analyzers Open-Source.

Unity Analyzers

Visual Studio and Visual Studio for Mac rely on Roslyn, our compiler infrastructure, to deliver a fantastic C# programming experience. One of my favorite features of Roslyn is the ability to programmatically guide developers when using an API. At the core of this experience, an analyzer detects a code pattern, and can offer to replace it with a more recommended pattern.

A common example that is specific to the Unity API is how you compare tags on your game objects. You could write

collision.gameObject.tag == "enemy";

to compare tags

But Unity offers a CompareTag method that is more efficient, so we implemented a CompareTag diagnostic that will detect this pattern and offer to use the more optimized method instead. On Windows just press (CTRL+.) or press (Alt-Enter) on Visual Studio for Mac to trigger the Quick Fixes, and you’ll be prompted by a preview of the change:

We currently have a dozen analyzers that are shipping in the Tools for Unity, with more being written right now.

Improving the Default Experience

Recently the Roslyn team introduced analyzer suppressors. This feature allows us to programmatically suppress the default set of analyzers that Roslyn ships.

This is great for Unity developers, because it allows the Tools for Unity team to remove warnings or code fix suggestions that do not apply to Unity development.

A common example is for fields decorated with Unity’s SerializeField attributes to light-up the fields in the Unity Inspector. For instance, without the Unity Analyzers, Visual Studio would offer to make a serialized field readonly while we know the Unity engine is setting the value of this field. If you were to accept that code fix, Unity would remove any association you set in the Inspector for this field, which could break things. By writing a suppressor, we can programmatically suppress this behavior while keeping it enabled for standard C# fields.

Available now

Today, the Unity Analyzers are being shipped as part of the Tools for Unity and are enabled on Visual Studio and Visual Studio for Mac. The analyzers are running inside Visual Studio, meaning that if you suppress a warning you might still see it in Unity’s error list. We’re working on improving this for a future release.

Bring your tips and tricks

The Tools for Unity team has a backlog of analyzers, code fixes and suppressors that we’re working on, but we’re always on the lookout for new analyzers that would improve the C# programming experience of Unity developers. The project is easy to get started with. Just head to our README and suggest a new analyzer or even submit a PR to the repository.

See you on GitHub!