You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Unity/VS开发的Hololens2独立应用内实现Simulation Control Mode切换?

在Hololens2应用内切换Control Mode的C#实现方案

一、直接通过UWP感知API实现切换

Hololens的Control Mode(Simulation/Default)核心是控制感知框架的运行状态,你可以直接在Unity/Visual Studio构建的UWP应用中调用官方感知模拟API实现切换,无需依赖Device Portal。

实现代码

先确保Unity项目已启用UWP平台支持,并在Player Settings中勾选Spatial Perception权限,再添加以下C#脚本:

using UnityEngine;
using Windows.Perception.Simulation;
using Windows.UI.Input.Spatial;

public class HoloLensControlMode : MonoBehaviour
{
    // 切换至Simulation模式:停止传感器、手势/场景识别
    public void EnableSimulationMode()
    {
        // 禁用所有空间交互源(手势、眼动等输入)
        var interactionManager = SpatialInteractionManager.GetForCurrentView();
        foreach (var source in interactionManager.GetDetectedSources())
        {
            source.IsEnabled = false;
        }

        // 启动感知模拟管理器,接管系统传感器输入
        var simManager = PerceptionSimulationManager.GetDefault();
        if (simManager != null)
        {
            simManager.IsSimulationEnabled = true;
        }
    }

    // 切换回Default模式:恢复所有传感器功能
    public void EnableDefaultMode()
    {
        // 关闭感知模拟
        var simManager = PerceptionSimulationManager.GetDefault();
        if (simManager != null)
        {
            simManager.IsSimulationEnabled = false;
        }

        // 重新启用所有空间交互源
        var interactionManager = SpatialInteractionManager.GetForCurrentView();
        foreach (var source in interactionManager.GetDetectedSources())
        {
            source.IsEnabled = true;
        }
    }
}

权限配置

在Package.appxmanifest中添加以下权限(Unity中可通过Player Settings > Publishing Settings > Capabilities勾选):

  • Spatial Perception
  • 若找不到Perception Simulation选项,可手动编辑appxmanifest文件添加<Capability Name="perceptionSimulation"/>

二、Device Portal切换模式的原理

Device Portal本质通过本地REST API + 系统服务调用实现模式切换:

  1. 在Device Portal页面切换Control Mode时,前端会发送POST请求到Hololens本地的Device Portal服务端点(默认http://<设备IP>:10080/api/holographic/controlmode);
  2. Device Portal服务接收请求后,调用系统底层的PerceptionSimulationManager API,直接修改感知框架的运行状态;
  3. 系统感知服务收到指令后,停止/恢复传感器数据采集,同时禁用/启用手势识别、场景理解等依赖传感器的模块。

三、备选方案(WMI调用)

如果上述API无法满足需求,还可以通过WMI(Windows管理规范)接口调用系统服务,但需要应用拥有runFullTrust权限:

using System.Management;

public void SetControlMode(string targetMode)
{
    // targetMode 传入 "Simulation" 或 "Default"
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(
        "root\\cimv2", 
        "SELECT * FROM Win32_PerceptionControl"
    );

    foreach (ManagementObject obj in searcher.Get())
    {
        obj.InvokeMethod("SetControlMode", new object[] { targetMode });
    }
}

注意事项

  • 需在Package.appxmanifest中添加runFullTrust能力,Unity中在Player Settings的Capabilities里勾选Full Trust;
  • 仅适用于企业部署或拥有系统权限的应用。

内容的提问来源于stack exchange,提问作者marcoresk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 17:36:25