Search Unity

10 ways to speed up your programming workflows in Unity with Visual Studio 2019

July 14, 2020 in Engine & platform | 11 min. read
Topics covered
Share

Is this article helpful for you?

Thank you for your feedback!

Visual Studio 2019 offers world-class debugging, and lots of new tools and customization options so that you can set up your coding environment exactly the way you want it. It also comes with a range of powerful features for working with C# in Unity that helps you write and refactor code quicker than before. In this blog post, we will take a look at 10 tips on some of these features which may speed up your workflow too.

Our Unity evangelist Arturo Nereu and Abdullah Hamed, program manager for Visual Studio Tools for Unity at Microsoft, recently hosted a Unite Now session sharing tips and tricks on how to get the most out of Visual Studio 2019 when developing in Unity.

This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.

This post shows a quick overview of some of these tips. We also added links directly to those sections from the talk as well as other related content, if you want to dig deeper.

Tip 1: Attach the Debugger to Unity and Play

Using Console.Log is an easy and quick way to help debug your project utilizing Unity’s console view. However, Visual Studio offers a more efficient way which becomes increasingly valuable as your project gets more complex. In Visual Studio, simply click the Attach to Unity button in the Visual Studio menu. This creates a connection between the two applications so that you can insert breakpoints and step through your code, while being in Play mode in Unity. You can also click Attach to Unity and play to start the execution without leaving your IDE. The beauty here is that it allows you to inspect the state of your code flow and values of the properties etc. at runtime. While this may seem trivial, being able to pause the execution at any time during gameplay and step through to inspect the specific state of the game and values of your properties in that exact frame, is a very powerful tool when debugging.

Another handy option when working with breakpoints is that you can insert your own conditions with related actions such as a conditional expression which has to evaluate as true before applying in your debug flow.

Tip 2: Get performance insights and suggestions for best practices

Visual Studio 2019 introduces Unity Analyzers. An analyzer works by detecting a code pattern and can offer to replace it with a more recommended pattern. Unity Analyzers are a collection of Unity-specific code diagnostics and code fixes that are open source and available on GitHub. Analyzers can provide you with a better understanding of Unity-specific diagnostics or simply help your project by removing general C# diagnostics that don’t apply to Unity projects. An example could be a simple conditional statement where you need to check if the GameObject has a specific tag to apply a certain behavior to it.

if(collision.gameObject.tag == "enemy")
{
// Logic being applied to enemy GO
}

The analyzer would be able to analyze your code, will detect the pattern and offer to use the more optimized method instead. In this case, the analyzer would suggest the CompareTag method which is more efficient.

if(collision.gameObject.CompareTag("enemy"))
{
	// Logic being applied to enemy GO
}

While the above example represents a minor optimization tweak with no significant impact in a single script attached to a single GameObject, this may be different for a large scale project with 1000s of GameObjects with the script attached. It’s the sum of all parts when looking into performance optimization and Analyzers can make it easy to help you identify and improve your performance simply by reducing the unneeded overhead by optimizing the code syntax. 

You can also find a list of the analyzers here and if you are interested in learning more visit this blog post or jump directly to this part of the Unite Now talk.

Tip 3: Use the Task List as a followup checklist

A common challenge when creating your scripts is the need to come back at a later point and revisit the code. That might be a result of implementing code snippets which eventually will need refactoring for better performance later but serves the current needs as you are f.x. testing out game mechanics. Visual Studio has a handy feature to keep track of this called Task List which allows you to track code comments that use tokens such as TODO and HACK, or even custom tokens. You can also manage shortcuts that take you directly to a predefined location in code. To create a task for later simply add the token in your code: 

// TODO: Change the collision detection once new colliders are ready

The Task List window, which you can access from under View in the menu, gives you an overview of all the tasks you tagged and links you to those specific parts of the code in just one click.

Adding a comment followed by the // TODO: in your code makes it easy to “tag” action items you want to follow up on at a later point using the Task List window as shown in the above on the right-hand side.

As the list of action items in your project grows, you can even configure your own custom tokens in the task list and assign priorities and organizing for your refactoring process effectively. To customize your Task List tokens: go to Tools > Options.

See the full example in the Unite Now session here.

Tip 4: Use snippets to speed up your workflows

Code snippets are small blocks of reusable code that can be inserted in a code file using a right-click menu (context menu) command or a combination of hotkeys. They typically contain commonly used code blocks such as try-finally or if-else blocks, but you can also use them to insert entire classes or methods. In short, they offer you a handy way to save a lot of time by creating the boilerplate code for you.

To surround your code with a snippet such as a namespace or region, press CTRL + K + S. That allows you to apply the snippet as demonstrated in the video below:

You can find a step-by-step walkthrough of creating your own code snippets in Microsoft’s documentation (Visual Studio, Visual Studio for Mac).

Tip 5: Rename all variables in just a few clicks

A common workflow when you are refactoring your code is renaming your variables to more descriptive and accurate names. Changing it one place obviously means you also have to fix all references to that variable. However, Visual Studio offers an easy shortcut to do this in one operation. Simply highlight the name of the variable you want to rename and right-click (or use the keyboard shortcut CTRL + R) and then rename the variable. Select preview changes to review the implications of the change before applying.

You can use the same tip for changing the classes of your script but remember you have to rename the C# file accordingly to avoid the compilation errors. Learn more about the class renaming flow this part of the Unite Now session.

Tip 6: Comment out your code with a keyboard shortcut

Commenting or uncommenting blocks of code is another common workflow when refactoring or debugging your code. It can be a time-consuming task when you do it one line at the time. Visual Studio, however, allows you to comment out entire blocks of code using a simple shortcut command: Ctrl+K+C and Ctrl+K+U for uncommenting it again. If you are on Mac, simply use CMD+K+C to comment out and CMD+K+U to remove the comments again.

Being able to comment out entire blocks quickly can be an efficient way to suppress specific game logic during your testing workflows.

Tip 7: Set up integration with GitHub in a few clicks

While Unity Collaborate makes it easy to save, share, and sync your project with others directly from Unity with a user-friendly visual interface, some teams and developers prefer using source control solutions such as GitHub. Working with GitHub for source control is now much easier with the Github for Unity plugin. The extension is completely open source and it allows you to view your project history, experiment in branches, craft a commit from your changes, and push your code to GitHub without leaving Unity. The GitHub authentication is embedded in Unity, including 2FA and with a click of a button, you can quickly initialize your game’s repository without having to use command-line instructions. It allows you to create a Unity specific .gitignore file so you don’t have to set it up manually. With Visual Studio 2019 also comes a new interface which makes it even easier to work with GitHub directly in the IDE.

To activate the new interface in Visual Studio: Go to Tools > Options > Environment > Preview features > New Git user experience.

You can also follow along with the video instructions from the Unite Now session, which shows a more in-depth walkthrough of getting started.

Tip 8: Collaborate remotely in real-time with Live Share

Live Share enables you to share your instance of Visual Studio directly with your teammate using just a link, allowing them to edit your code and collaborate on your project in real-time. You don’t have to clone a repo or set up the environment first in order to do the sharing. You both just need to have Visual Studio installed and then it’s as easy as clicking a button to create the Live Share session.

Visual Studio Live Share allows you to share your Visual Studio session and collaborate with others directly on the code in real time.

To get started simply select Live Share to generate a link to the parts of your code that you want to share with anyone that has Visual Studio or Visual Studio Code installed. A sharing session is created between you and your collaborators, allowing them to see your code without having to install anything except for the editor. It works almost instantly. 

You can learn more about Live Share from our Unite Session here, visit the Visual Studio product page or jump directly to the Quickstart guide here.

Tip 9: Create boilerplate code easily for Unity Messages

Remembering the signature of all the MonoBehaviour methods is tricky and while the Unity documentation will have you covered, Visual Studio provides a neat feature that allows you to look it up directly in the IDE. Simply click CTRL + Shift + M, search for the function you would like to implement, and filter through the search result to find the method. Select the checkbox and click Ok to insert the boilerplate code for the method directly in your code ready for you to use.

Tip 10: Keyboard shortcuts

Several of the above tips are available with handy shortcuts and at the end of the day, knowing those shortcuts may be the biggest timesaver of them all. So let’s wrap up the list with a summary of the keyboard shortcuts to these tips and a few more as a bonus.

WhatWindowsMac
Search your entire project for anything.Use CTRL+T CMD + .
Implement Unity MessagesCTRL + Shift + M CMD + Shift + M 
Comment out code blocksCTRL + K  / CTRL + CCMD + /
Uncomment blocks of codeCTRL + K  / CTRL + U CMD + /
Copy from clipboard historyCTRL + Shift + V
View task listCTRL + TNo default keybinding, but you can bind it.
Insert a surrounding snippet such as namespaceCTRL + K + S:No default keybinding, but you can bind it.
Renaming a variable while updating all referencesCTRL + RCMD +R
Compile the code CTRL+SHIFT+B CMD + Shift + B

Share your tips?

Visual Studio 2019 is packaged with features and there are so many customization options that can enhance your productivity working with Unity depending on your specific workflows. There’s too many to cover them all here. We hope that the few tips that we shared here will inspire you to dive in and that you’re finding the format useful. Let us know if you have any tips we didn't cover, and feel free to share them with the community in the comments. We’d also love to hear if you would like more tips and tricks on how to improve your workflows in Unity and if there are any topics, in particular, that you would like to have covered in a future blog post.

Got feedback on how Visual Studio could be improved?

We are constantly working on improving the workflows and our teams are working closely with Microsoft in terms of giving you the best IDE experience. Hence we would love to hear from you if you have any ideas or feedback. Feel free to ping John Miller at @jmillerdev, Senior Program Manager, Visual Studio Tools for Unity at Microsoft, or share your feedback with us in our Scripting forum.

July 14, 2020 in Engine & platform | 11 min. read

Is this article helpful for you?

Thank you for your feedback!

Topics covered