Unity Analyzers Now Open-Source

Our Visual Studio tool development team for Unity sees its mission in improving the productivity of Unity developers . In Visual Studio 2019, colleagues introduced Unity tools, a set of diagnostics and code fixes specific to Unity. Today, we are pleased to announce that Unity Analyzers have become Open Source . Details under the cut.



Unity analyzers


Visual Studio and Visual Studio for Mac are affiliated with Roslyn , our compiler infrastructure, to provide a fantastic C # programming experience. One of Roslyn's favorite features is the ability to programmatically prompt developers when using the API. Based on this experience, the analyzer discovers a code template and may propose replacing it with a more recommended template.

A typical example of the Unity API is the comparison of tags on game objects. Could you write

collision.gameObject.tag == "enemy";

To compare tags



But Unity offers the CompareTag method , which is more efficient, so we implemented CompareTag diagnostics that will detect this pattern and suggest using a more optimized method instead. On Windows, just press (CTRL +.) Or press (Alt-Enter) in Visual Studio for Mac to activate quick fixes, and you will be offered a preview of the change:



We currently have a dozen parsers that are in Unity tools, and more is being created right now.

Improving the default experience


The Roslyn team recently introduced anti-jammers. This feature allows us to programmatically suppress the default analyzer set that comes with Roslyn.

This is great for Unity developers because it allows our Tools for Unity team to remove warnings or suggestions for fixing code that are not related to Unity development.

A typical example is fields decorated with Unity SerializeField attributes., to highlight fields in the Unity Inspector. For example, without Unity analyzers, Visual Studio will offer to make the serialized field read-only, while we know that the Unity mechanism sets the value of this field. If you accepted this code patch, Unity would remove any link that you made in the Inspector for this field, which could break everything. By writing a suppressor, we can programmatically suppress this behavior, leaving it enabled for standard C # fields.

Already available


Today, Unity Analyzers are shipped as part of Tools for Unity and are included with Visual Studio and Visual Studio for Mac. Analyzers work in Visual Studio. This means that if you suppress the warning, you can still see it in the Unity error list. We are working to improve this for a future release.

Share your life hacks


The Tools for Unity team has a backlog of analyzers, code fixes and suppressors that we are working on, but we are always looking for new analyzers that will improve the C # programming experience for Unity developers. The project is easy to help. Just go to our README and suggest a new analyzer or even send a request to the repository.

See you on GitHub!

All Articles