你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

为 Unity 设置远程渲染

若要在 Unity 中启用 Azure 远程渲染 (ARR),可能需要某些项目配置。 我们还提供用于处理某些 Unity 特定方面的专用方法。

项目配置

使用 OpenXR 时,必须在 Unity OpenXR 设置中启用 Azure 远程渲染 功能。

Screenshot of the Unity Project Settings dialog. The Open XR subentry is selected in the list on the left. The Azure Remote Rendering Open XR feature is highlighted with a checked checkbox.

对于其他必需和建议的项目设置,请使用 Azure 远程渲染 Unity 包中包含的项目验证程序

  1. 从 Unity 编辑器工具栏中的远程渲染菜单中选择 ValidateProject 条目。
  2. 查看“项目验证程序”窗口是否存在错误并按需修复项目设置。

启动和关闭

若要初始化远程渲染,请使用 RemoteManagerUnity。 此类调用泛型 RenderingConnection,但已为你实现了特定于 Unity 的细节。 例如,Unity 使用特定坐标系统。 调用 RemoteManagerUnity.Initialize时,将设置正确的约定。 该调用还要求你提供用于显示远程渲染内容的 Unity 相机。

// initialize Azure Remote Rendering for use in Unity:
// it needs to know which camera is used for rendering the scene
RemoteUnityClientInit clientInit = new RemoteUnityClientInit(Camera.main);
RemoteManagerUnity.InitializeManager(clientInit);

若要关闭远程渲染,请调用 RemoteManagerStatic.ShutdownRemoteRendering()

在创建 RenderingSession 并将其选为主渲染会话后,必须将其注册到 RemoteManagerUnity

RemoteManagerUnity.CurrentSession = ...

完整示例代码

此代码示例演示了在 Unity 中初始化 Azure 远程渲染所需的所有步骤:

// initialize Remote Rendering
RemoteUnityClientInit clientInit = new RemoteUnityClientInit(Camera.main);
RemoteManagerUnity.InitializeManager(clientInit);

// create a frontend
SessionConfiguration sessionConfig = new SessionConfiguration();
// ... fill out sessionConfig ...
RemoteRenderingClient client = new RemoteRenderingClient(sessionConfig);

// start a session
CreateRenderingSessionResult result = await client.CreateNewRenderingSessionAsync(new RenderingSessionCreationOptions(RenderingSessionVmSize.Standard, 0, 30));
RenderingSession session = result.Session;

// let RemoteManagerUnity know about the session we want to use
RemoteManagerUnity.CurrentSession = session;

await session.ConnectAsync(new RendererInitOptions());

/// When connected, load and modify content

RemoteManagerStatic.ShutdownRemoteRendering();

便捷函数

会话状态事件

RemoteManagerUnity.OnSessionUpdate 在其会话状态更改时会发出相应的事件。有关详细信息,请参阅代码文档。

ARRServiceUnity

ARRServiceUnity 是一个可选组件,用于简化设置和会话管理。 它包含用于在编辑器中退出应用程序或播放模式时自动停止其会话的选项。 它在需要时自动续订会话租约。 它缓存会话属性之类的数据(请查看其 LastProperties 变量),并公开有关会话状态更改和会话错误的事件。

同时不能有多个 ARRServiceUnity 实例。 它的目的是通过实现一些常见功能让你更快地开始。 但是,对于较大的应用程序,最好你自己做这些事情。

有关如何设置和使用 ARRServiceUnity 的示例,请参阅教程:查看远程渲染的模型

后续步骤