Book Image

Game Development with Unity for .NET Developers

By : Jiadong Chen
Book Image

Game Development with Unity for .NET Developers

By: Jiadong Chen

Overview of this book

Understand what makes Unity the world’s most widely used real-time 3D development platform and explore its powerful features for creating 3D and 2D games, as well as the Unity game engine and the Microsoft Game Dev, including the Microsoft Azure Cloud and Microsoft Azure PlayFab services, to create games. You will start by getting acquainted with the Unity editor and the basic concepts of Unity script programming with C#. You'll then learn how to use C# code to work with Unity's built-in modules, such as UI, animation, physics, video, and audio, and understand how to develop a game with Unity and C#. As you progress through the chapters, you'll cover advanced topics such as the math involved in computer graphics and how to create a custom render pipeline in Unity with the new Scriptable Render Pipeline, all while optimizing performance in Unity. Along the way, you'll be introduced to Microsoft Game Dev, Azure services, and Azure PlayFab, and using the Unity3D PlayFab SDK to access the PlayFab API. By the end of this Unity book, you'll have become familiar with the Unity engine and be ready to develop your own games while also addressing the performance issues that you could encounter in the development process.
Table of Contents (16 chapters)
1
Part 1: Basic Unity Concepts
4
Part 2: Using C# Scripts to Work with Unity's Built-In Modules
9
Part 3: Advanced Scripting in Unity

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:

Figure 1.52 – Visual Studio Installer

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:

Figure 1.53 – Installing Visual Studio Tools for Unity

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:

Figure 1.54 – The External Script Editor settings

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:

Figure 1.55 – IntelliSense for the Unity APIs

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:

Figure 1.56 – Debugging your code

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:

Figure 1.57 – Clicking the Attach to Unity button

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.

Figure 1.58 – The debugger stopping at the breakpoint

Then, the debugger will stop at the breakpoint, and you can look at the current state of the game.