Build for the web with ASP.NET (For Unity developers)
So, you have learned C# through Unity. It was a fun journey, and you enjoy it. You might have thought what else can I do with C#. Well, this blog post is an introduction to how you can leverage your knowledge of C# using .NET 5 and build dynamic web apps. I will explain what is ASP.NET Core, what is different between coding for Unity and other .NET workloads Web development terminology. I will finally end the post with example of how a Unity developer can use ASP.NET Core.
What is ASP.NET Core
ASP.NET Core is a free, cross-platform, open-source framework for building web apps and services in .NET 5 using C# that you already know. You can think of ASP.NET Core as Unity Engine running on a server.
Razor Pages
Razor Pages provides a simple, clean, and lightweight way to create dynamic web content using C#. You can think of a Razor page as a Prefab for ASP.NET Core. It's the fastest and easiest way to create dynamic web pages on the server.
Blazor
Blazor gives you the ability to create web apps with ASP.NET Core that run on the browser using WebAssembly instead of the server using C#. If you've heard of Angular or React, you can think of it as a similar concept, but you can write your browser code in C# instead of JavaScript.
Differences between Unity and other .NET workflows
Visual Studio IDE usage
When developing in Unity, the center of your world, where you spend most of your time, is the Unity editor. You are not using Visual Studio IDE for building, profiling, and editing the UI. When developing .NET apps, Visual Studio IDE will be the center of your world. You will use it to code, build, debug, design UI, manage files, access code repositories, manage plugins (NuGet packages), work with databases, and interact with cloud services.
NuGet Packages
In the .NET ecosystem, there are "Plugins" that are called NuGet Packages. You can install and manage these packages in your project by using the NuGet Package Manager. This is very similar to the new Unity Package manager, or the Unity Asset Store.
Constructor
In object-oriented programming outside of Unity, there are methods in a class that have the same name as the class. It's a method that runs as soon as an object is instantiated. We call those methods constructors. You can think of this class as the Awake() function in Unity. When you use the New operator to instantiate an object, you are really calling the constructor. Unity does some work under the hood with the constructor of MonoBehavior, thus they created the Awake() and Start() and other methods to make it easier not to mess with the constructor.
Script 2: A class in .NET
CLI
Command Line Interface (CLI) is a way to interact with a computer with entering text commands. When developing with .NET 5, sometimes you need the flexibility of typing commands into a command line interface. Visual Studio has an integrated terminal that makes this a little easier, since you're not required to switch back and forth between Visual Studio and the terminal.
Web development terminology for Unity developers
HTML
Hypertext Markup Language (HTML) is language that is used to identify elements in a web page, and set basic properties to them. It includes paragraphs, headings, bullet points, links to images, etc.
CSS
Cascading Style Sheets (CSS) is a language that describes how HTML elements should look like. For example, how big texts should look like in paragraphs.
JSON
JSON (JavaScript Object Notation) is an easy way to bundle up information to send between the browser and server. This is often used to let people interact with the web page without reloading the whole page.
JavaScript
JavaScript is a lightweight flexible language that browsers can run locally on a user's machine.
IIS
Internet Information Service (IIS) is a web server for Windows developed by Microsoft. Web servers are software that run web apps on a remote server. ASP.NET Core can be hosted in IIS for production deployments, but when you're developing your application it can run self-hosted, so there's nothing else you need to set up. ASP.NET Core runs on Windows, Mac, Linux, containers like Docker, and in all cloud hosting environments.
Cookies
Imagine cookies as a persistent storage for a web app. This about it as PlayerPrefs in Unity.
MVC
Model View Controller (MVC) is a framework for developing applications where you separate the data layer that interacts with databases (Models), the layer that generates the web pages for the browser to render (Views), and the logic that manages the communication and processing between the Models and Views (Controllers). It's an alternative to Razor Pages that gives you a more formal code separation if that fits your development style better.
Usages for ASP.NET Core for Unity developers
There are multiple reasons why you, a Unity developer, would use ASP.NET. Here are some of them.
Build your game services
You could build your own game services by building RESTful APIs for your game. You can build an API to authenticate users, create cloud saves, leaderboards, and much more. If you are using ready to use game services like Microsoft Azure PlayFab, you can still use ASP.NET Core to manage the communication between your game and the cloud services.
Build web and mobile apps
You could also build a web app for your game where your players can check scores, leaderboards, game news, and communicate with other players outside of the game. You can even use the same ASP.NET Core server with a multi-platform mobile app using Xamarin.
Build you own website
There are many ready to use website builders out there, but what if you wanted to build your own from scratch? You can use ASP.NET Core 5 to build your site and host up to 10 ASP.NET Core website on Microsoft Azure for free.
Get Started in Visual Studio IDE
1. To get started with ASP.NET in Visual Studio IDE or Visual Studio for Mac, Just create a new project by selecting File -> New -> Project.
2. Create a new ASP.NET Core Web Application and select Next.
3. Select ASP.NET Core Web Application then select Next.
4. Choose a name for the Application and where you want to save it.
5. Select .NET 5.0 as the Target Framework then select Create.
6. You will see Visual Studio with a solution hierarchy that looks like this.
7. Press Ctrl+F5 to run the application without the debugger. Select yes on dialogues to accept the IIS SSL certificate.
8. A browser window should open up with you application. It's that simple to get started.