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

求助:VisionOS 2.0模拟器无法接收GCControllerDidConnect通知

VisionOS 2.0模拟器无法接收GCControllerDidConnect通知的排查与解决

在VisionOS 2.0模拟器中无法触发GCControllerDidConnect通知,导致无法实现游戏手柄支持,但该功能在VisionOS 1.2模拟器中可正常工作。已尝试添加.handlesGameControllerEvents(matching: .gamepad)修饰符、配置Info.plist相关键值等操作,但通知仍未触发。

以下是针对性的排查和解决步骤:

1. 调整通知注册时机

当前代码在RealityView的内容闭包中注册通知,可能因生命周期时机问题导致注册不及时。建议将通知注册移至视图初始化或onAppear回调中,确保在视图加载早期完成注册:

import SwiftUI
import GameController
import RealityKit
import RealityKitContent

struct ImmersiveView: View {
    init() {
        // 在初始化时注册通知,确保时机更早
        NotificationCenter.default.addObserver(
            forName: NSNotification.Name.GCControllerDidConnect,
            object: nil,
            queue: .main) { _ in
                print("Handling GCControllerDidConnect notification")
            }
    }

    var body: some View {
        RealityView { content in
            if let immersiveContentEntity = try? await Entity(named: "Immersive", in: realityKitContentBundle) {
                content.add(immersiveContentEntity)
            }
        }
        .handlesGameControllerEvents(matching: .gamepad)
        .onAppear {
            // 主动启动无线控制器扫描,确保已连接手柄被检测到
            GCController.startWirelessControllerDiscovery()
        }
    }
}

2. 直接监听控制器连接状态

除了通知,可通过GCController.controllers()获取当前已连接的控制器,同时结合通知监听新连接的设备,避免遗漏初始已连接的手柄:

struct ImmersiveView: View {
    var body: some View {
        RealityView { content in
            if let immersiveContentEntity = try? await Entity(named: "Immersive", in: realityKitContentBundle) {
                content.add(immersiveContentEntity)
            }
        }
        .handlesGameControllerEvents(matching: .gamepad)
        .onAppear {
            // 检查当前已连接的控制器
            for controller in GCController.controllers() {
                print("已连接控制器:\(controller)")
                setupController(controller)
            }
            
            // 注册连接通知
            NotificationCenter.default.addObserver(
                forName: .GCControllerDidConnect,
                object: nil,
                queue: .main) { notification in
                    guard let controller = notification.object as? GCController else { return }
                    print("新控制器已连接:\(controller)")
                    setupController(controller)
                }
            
            GCController.startWirelessControllerDiscovery()
        }
    }
    
    private func setupController(_ controller: GCController) {
        // 在这里配置手柄按键映射、输入监听等逻辑
        if let gamepad = controller.extendedGamepad {
            // 处理Extended Gamepad的输入事件
            gamepad.buttonA.pressedChangedHandler = { button, value, pressed in
                if pressed {
                    print("Button A pressed")
                }
            }
        }
    }
}

3. 验证Info.plist配置准确性

确保Info.plist中的配置完全符合要求:

  • 添加GCSupportsControllerUserInteraction(类型为Boolean),设置为YES
  • 添加GCSupportedGameControllers(类型为Array),添加字符串元素Extended Gamepad

4. 检查模拟器手柄设置

在VisionOS 2.0模拟器中,确认已开启手柄转发选项:

  1. 点击模拟器顶部菜单的Hardware
  2. 选择Game Controller -> Send Game Controller To Device

5. 测试真机环境

模拟器可能存在特定版本的兼容性问题,建议在VisionOS 2.0真机上测试,确认问题是否仅存在于模拟器中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 01:22:37