Building Unity games with Visual Studio
Before you start writing any code, it is important to choose suitable development tools. Microsoft's Visual Studio is not only a widely used IDE but also the development environment that is installed by default when you install Unity on Windows or macOS:
While installing Visual Studio, Visual Studio Tools for Unity will also be installed. It is a free extension that provides support for writing and debugging C# in Unity.
If you do not install Visual Studio through the Unity Hub, please make sure you installed this extension. You can check it in the Visual Studio Installer:
After installing the Unity Editor and Visual Studio Community 2019, you can check the External Script Editor settings in the Preferences window of the Unity Editor:
In addition, you can also select other script editors by modifying this setting, such as Visual Studio Code and JetBrains Rider.
Then, we can create a new C# script file named NewBehaviourScript
in the Unity Editor and double-click to open it in Visual Studio:
As you can see in the preceding screenshot, there are two built-in methods in the script file by default, namely Start
and Update
. Visual Studio supports IntelliSense for Unity APIs, so we can write code quickly:
It is also very easy to debug your code in Visual Studio. In the preceding screenshot, I set a breakpoint inside the Start
method and clicked the Attach to Unity button in Visual Studio:
In order to run this code, I attach this script to a GameObject in the Scene and click on the Play button in the Unity Editor to run the game in the Game view.
Then, the debugger will stop at the breakpoint, and you can look at the current state of the game.