Version: 2019.4
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 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.

Ограничения

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 General
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961