Version: 2020.1
Windows
Windows 全般

Integrating Unity into Windows and UWP applications

This page describes how to integrate the Unity Runtime Library into Windows and Universal Windows Platform (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 Rendering, AR 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-ins to work properly.

  • Unity as a Library added in 2019.3.NewIn20193
Windows
Windows 全般