HoloLens, Unity, .NET standard and .NET core. How do they work together?

Are you developing mixed reality apps or games with Unity? Have you wondered which build setting to choose when creating apps for mobile or HoloLens? After reading this article, you have learned what .NET core and .NET standard are, where it comes from and why it is important for the future of building apps and games with Unity.

If you are a Microsoft developer –working with .NET and C#– you are familiar with the .NET framework and probably have heard of .NET core and .NET standard. For other readers, like Unity or Xamarin developers, .NET might be a thing you choose in a drop-down just so you can build without errors. In order to explain the difference between all .NET versions, I have to take you back in time.

.NET history

I saw .NET for the first time at Tech-ed Amsterdam in 2000 where Don Box held an awesome talk comparing .NET with entering the matrix. The story was: You developers can now write in Visual Basic or C#, which is compiled to a common intermediate language (CIL), that runs on something called the common language runtime (CLR). Let me break that down for you.

The CLR is an execution engine that takes care of memory management, exception handling, garbage collection and thread management. Before .NET, a program written in C++ would be compiled directly to machine code for a specific operating system. In .NET a program written in C# would be compiled to CIL and during execution, a just-in-time (JIT) compiler compiles the CIL code to platform-specific machine code. As long as there is a .NET stack for a specific platform, your code would run everywhere.

I remember we got an alpha version of .NET and we could play with C# ourselves. After writing VB code for years, this was really good. C# is a fantastic language. Even that first version already had so much good features.

.NET 1.0

.net 1

Image: The first .NET was built for Windows

Microsoft officially released .NET 1.0 in 2002 and it could be used in Windows Forms Applications and ASP.NET. Soon after the first release, Microsoft created a second .NET stack, the .NET Compact Framework.

.net 2

Image: Microsoft added a second .NET stack

This would run on Windows CE and everyone with a Windows Mobile Phone or iPAQ could now create apps for their small devices. Microsoft now had two .NET stacks, one that targets Windows and one that targets mobile devices. You can imagine they overlapped but where also different to support platform specific things. In 2018 we are used to a shared development environment with platform specific features like printers, controllers, touch screens, sensors, big screens, small screens. But in 2003 this concept was quite new to me.

.NET Micro framework

Microsoft created a third stack, called the .NET Micro Framework. This would target small devices that we now call IoT. The Micro Framework would again be a subset of the .NET Framework and some other features.

.net 3

Image: a third .NET stack

By now it would not be possible to write C# code and share it 100% between all the .NET stacks.

Windows Presentation Foundation/Everywhere

In 2007 Microsoft created a subset of WPF and released Silverlight. Silverlight 1.1 included yet another version of the .NET Framework, allowing us to create C# or Visual Basic programs that run in a browser on Windows or Mac and later natively on Windows Phone 7.

Windows 8 and 10

Do you remember Metro style apps? These were the modern apps you could write for Windows 8(.1) and discover in the store. And yes, .NET APIs for Windows 8.x Store apps is another .NET stack made for these kind of apps.

.net 4

Image: Another .NET Stack for Modern apps (a.k.a. Store apps, a.k.a. Metro apps, a.k.a. Universal Application Platform apps.

Microsoft also created a special runtime to run these apps. They called it WinRT, or the Windows Runtime. Anybody who bought a SurfaceRT in 2012 remembers this name. By now the supported languages also included C++ and JavaScript.

.net 5

Image: Another stack, .NET for UWP

For Windows 10 UWP apps, another .NET stack was created, the so called .NET APIs for UWP.

Too many stacks, too much differences

Developers knew that it was impossible to write one codebase and run it in all these different stacks without code changes. We had to rely on compiler directives and hiding platform specific libraries behind interfaces. A big problem was that .NET was always a monolithic framework, impossible to implement partially.

.NET Core

In 2016 Microsoft released .NET Core, which is an open-source, modular rewrite of .NET, with high compatibility to .NET. The fact that it is open source and modular, makes it much easier for third-party developers to add a stack for their plaform, like Android or iOS. And that is where it gets interesting to us Unity developers!

.NET Standard

.NET Standard is the formal specification of .NET Apis. .NET Core is the implementation that follows that specification. In 2017 .NET Core 2.0 came out. This version follows the .NET Standard 2.0 specification.

.net core and standard

Image: .NET Core 2.0 and .NET Standard 2.0 go hand in hand

Look at this Compatibility matrix to check which .NET Standard version is compatible with what implementations. For HoloLens developers, it is important to know that Unity creates a Universal Windows Platform app that can be deployed to the Lens. Let’s see how that works.

Unity Scripting

Unity’s player settings shows these configuration settings:

The scripting runtime version determines what .NET version you are going to target. To be able to use .NET Standard, choose .NET 4.x here. Unity now offers to use the IL2CPP scripting backend. The scripting backend is the framework that is used to run the scripts in. IL2CPP offers .NET Standard 2.0 support and is the one you should choose here. Api Compatibility level can be .NET 4.x or .NET standard 2.0 and I would recommend to choose .NET Standard 2.0 because it is smaller than .NET 4.x.

This is what it looks like in Unity:

.net core and standard

As a bonus Unity promises to add support for .NET Standard 2.0 on all platforms that Unity supports

Small recap

HoloLens (which runs Windows 10) runs Universal Windows Applications. To create these apps, you can use Visual Studio or Unity and target the Universal Windows Platform. As of RS4 both Windows 10 and HoloLens are capable to consume .NET Standard 2.0 and .NET Core 2.0 libraries. That means you can write one .NET Standard library and without code changes run in in the Unity Editor and on the HoloLens and any other platform that Unity wrote an IL2CPP scripting backend for.

IL2CPP

IL2CPP is a scripting backend that takes the Intermediate Language created from C# and the used libraries and converts it to C++. It then creates a native binary for the targeted platform.

Conclusion

Microsoft and Unity have worked on fantastic improvements during the last years. On the one side we can now take 20-year-old tried and tested .NET Api’s (like Sockets), take their .NET Core implementation and by leveraging IL2CPP, use it on Windows, OSX, HoloLens, XBOX One and (although I have not tested this) on iOS, Android or any other platform on the following list?

platforms

Image: By Unity supported platforms.

I hope this article gave you a little insight of the history of .NET and how it is important if you are developing apps and games with Visual Studio, Unity and HoloLens. If you want to test it yourself, have a look at this NET Standard in Unity 3D tutorial:

Grüsse aus Berlin!
Loek