Version: 2020.2
Windows
Windows General

Integrating Unity into Windows and UWP applications

This page describes how to integrate the Unity Runtime Library into Windows and Universal Windows PlatformAn IAP feature that supports Microsoft’s In App Purchase simulator, which allows you to test IAP purchase flows on devices before publishing your application. More info
See in Glossary
(UWP) applications using the Unity as a Library feature.

You can use this feature to include Unity-powered features, such as 3D/2D Real-Time RenderingThe process of drawing graphics to the screen (or to a render texture). By default, the main camera in Unity renders its view to the screen. More info
See in Glossary
, ARAugmented Reality (AR) uses computer graphics or video composited on top of a live video feed to augment the view and create interaction with real and virtual objects.
See in Glossary
Experience, 3D model interaction, or 2D mini-games, into your application. The Unity Runtime Library exposes controls to manage when and how to load, activate, and unload content within the application.

How it works for UWP

The UWP XAML project output type makes it easy to create a UWP project that embeds Unity. The generated Visual Studio project is a general XAML UWP project with a SwapChainPanel setup to load the Unity Project. You can extend or replace this Project with any other non-Unity application business logic.

When using a SwapchainPanel, you might want to render over other elements. This enables you to render a small object with a transparent background on top of other non-Unity application content. To do this, enable the PlayerSettings.WSA-transparentSwapchain option.

You can unload the Unity engine to reclaim resources when Unity is loaded in-process with Application.Unload.

How it works for Windows

On Windows, you can embed Unity into your application in two different ways.

The simplest option is to launch Unity as an external process from your application, and specify a window in which Unity will initialize and render with the -parentHWND command line argument.

The second option is to embed Unity within your existing process. To do this, build Unity into a UnityPlayer.dll, which can be loaded directly by any Win32 application. The entry point signature is:

extern "C" UNITY_API int UnityMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd);

You can use lpCmdLine to pass any command line arguments to Unity to control resolution, job threads, parent HWND, etc. This enables you to run Unity within your process. When Unity is not in view, you can use Application.Unload to reclaim resources it uses.

Limitations

When hosted by another application, Unity doesn’t control the runtime lifecycle, so it might not work in all scenarios. Known limitations include:

  • You can’t load more than one instance of the Unity runtime, or integrate more than one Unity runtime.
  • You might need to adapt your native and managed plug-insA set of code created outside of Unity that creates functionality in Unity. There are two kinds of plug-ins you can use in Unity: Managed plug-ins (managed .NET assemblies created with tools like Visual Studio) and Native plug-ins (platform-specific native code libraries). More info
    See in Glossary
    to work properly.

  • Unity as a Library added in 2019.3.NewIn20193
Windows
Windows General