{"id":228281,"date":"2020-02-11T10:17:19","date_gmt":"2020-02-11T18:17:19","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/visualstudio\/?p=228281"},"modified":"2020-02-11T10:43:55","modified_gmt":"2020-02-11T18:43:55","slug":"making-our-unity-analyzers-open-source","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/visualstudio\/making-our-unity-analyzers-open-source\/","title":{"rendered":"Making our Unity Analyzers Open-Source\u00a0"},"content":{"rendered":"<p>Here at the Visual Studio Tools for Unity team our mission is to improve the productivity of <a href=\"https:\/\/www.unity.com\">Unity<\/a> developers. In Visual Studio 2019 we\u2019ve introduced our Unity Analyzers, a collection of Unity specific code diagnostics and code fixes. Today we\u2019re excited to make our <a href=\"https:\/\/github.com\/microsoft\/microsoft.unity.analyzers\">Unity Analyzers Open-Source<\/a>.<\/p>\n<h2>Unity Analyzers<\/h2>\n<p>Visual Studio and Visual Studio for Mac rely on <a href=\"https:\/\/github.com\/dotnet\/roslyn\">Roslyn<\/a>, 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.<\/p>\n<p>A common example that is specific to the Unity API is how you compare tags on your game objects. You could write<\/p>\n<pre class=\"toolbar:2 lang:c# decode:true \" title=\"snippet\">collision.gameObject.tag == \"enemy\";<\/pre>\n<p>to compare tags<\/p>\n<p><img decoding=\"async\" width=\"550\" height=\"123\" class=\"wp-image-228282\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/02\/word-image.png\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/02\/word-image.png 550w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/02\/word-image-300x67.png 300w\" sizes=\"(max-width: 550px) 100vw, 550px\" \/><\/p>\n<p>But Unity offers a <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/GameObject.CompareTag.html\">CompareTag<\/a> method that is more efficient, so we implemented a <a href=\"https:\/\/github.com\/microsoft\/Microsoft.Unity.Analyzers\/blob\/master\/doc\/UNT0002.md\">CompareTag<\/a> 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\u2019ll be prompted by a preview of the change:<\/p>\n<p><img decoding=\"async\" width=\"647\" height=\"303\" class=\"wp-image-228283\" src=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/02\/word-image-1.png\" srcset=\"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/02\/word-image-1.png 647w, https:\/\/devblogs.microsoft.com\/visualstudio\/wp-content\/uploads\/sites\/4\/2020\/02\/word-image-1-300x140.png 300w\" sizes=\"(max-width: 647px) 100vw, 647px\" \/><\/p>\n<p>We currently have a <a href=\"https:\/\/github.com\/microsoft\/Microsoft.Unity.Analyzers\/blob\/master\/doc\/index.md\">dozen analyzers<\/a> that are shipping in the Tools for Unity, with more being written right now.<\/p>\n<h2>Improving the Default Experience<\/h2>\n<p>Recently the Roslyn team introduced analyzer suppressors. This feature allows us to programmatically suppress the default set of analyzers that Roslyn ships.<\/p>\n<p>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.<\/p>\n<p>A common example is for fields decorated with Unity\u2019s <a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/SerializeField.html\">SerializeField<\/a> 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 <em>readonly<\/em> 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.<\/p>\n<h2>Available now<\/h2>\n<p>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\u2019s error list. We\u2019re working on improving this for a future release.<\/p>\n<h2>Bring your tips and tricks<\/h2>\n<p>The Tools for Unity team has a backlog of analyzers, code fixes and suppressors that we\u2019re working on, but we\u2019re 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.<\/p>\n<p>See you on GitHub!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Making our Unity Analyzers in visual studio Open-Source\u00a0<\/p>\n","protected":false},"author":641,"featured_media":228283,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1195,472,561,155],"tags":[5,179,5608,182,12,452],"class_list":["post-228281","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cross-platform","category-gaming","category-open-source","category-visual-studio","tag-csharp","tag-game-development","tag-roslyn","tag-unity","tag-visual-studio","tag-visual-studio-for-mac"],"acf":[],"blog_post_summary":"<p>Making our Unity Analyzers in visual studio Open-Source\u00a0<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/228281","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/users\/641"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/comments?post=228281"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/posts\/228281\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media\/228283"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/media?parent=228281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/categories?post=228281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/visualstudio\/wp-json\/wp\/v2\/tags?post=228281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}