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

使用RealityView时预览正常,Vision Pro模拟器无内容显示的问题

问题:RealityView在Vision Pro模拟器中无内容显示,但SwiftUI预览正常

问题背景

使用RealityView开发时,代码在SwiftUI预览中可正常运行(仅日志未显示),但在Vision Pro模拟器启动后,volumetric窗口内无任何内容显示。

应用代码

import SwiftUI

@main
struct LogicVisionApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }.windowStyle(.volumetric)
    }
}

ContentView代码

import SwiftUI
import RealityKit
import RealityKitContent

struct ContentView: View {
    init() {
        // 注册组件
        DraggableComponent.registerComponent()
        [snip...]
        
        // 注册系统
        TransmittableStaticInitSystem.registerSystem()
        [snip...]
        
        NSLog("I am running?!")
    }
    
    var body: some View {
        RealityView { content in
            // 添加测试实体
            let testWire = try! Entity.load(
                    named: "Wire",
                    in: realityKitContentBundle)
            content.add(testWire)

            [snip...]

            NSLog("I am running 2?!")
        }
        .gesture(DragGesture(coordinateSpace: .global)
            .targetedToEntity(
                where: QueryPredicate<Entity>.has(DraggableComponent.self))
            .onChanged { value in
                value.entity.position = value.convert(
                    value.location3D,
                    from: .global,
                    to: value.entity.parent!)
            }
        )
    }
}

#Preview {
    ContentView()
}

运行现象

  • SwiftUI预览中功能正常,仅日志未输出;
  • Vision Pro模拟器启动应用后,volumetric窗口无内容,但控制台输出以下日志:
    • "I am running?!"
    • "I am running 2?!"
    • 系统更新正常运行的日志
    • 仅在第一帧检测到含对应组件的实体,后续帧无相关日志(疑似实体已消失)

解决方向

  1. 检查实体初始位置与可见性
    实体加载后可能处于相机视野之外,手动设置实体到相机前方区域测试:

    testWire.position = [0, 0, -1] // 放置在相机前方1米处
    

    同时确认实体的isEnabled属性为true,没有被意外禁用。

  2. 排查自定义系统/组件的影响
    日志显示实体仅在第一帧存在,大概率是自定义系统(比如TransmittableStaticInitSystem)在运行时移除了实体或修改了其层级/可见性。暂时注释自定义系统的注册代码,测试实体是否能正常显示,逐步定位问题系统。

  3. 验证资源加载是否正常
    虽然用了try!,但可能存在"Wire"模型不存在或格式错误的情况。替换为默认实体测试:

    let testEntity = Entity()
    testEntity.components.set(ModelComponent(mesh: .generateSphere(radius: 0.1), materials: [SimpleMaterial(color: .red, isMetallic: false)]))
    testEntity.position = [0,0,-1]
    content.add(testEntity)
    

    如果默认实体能显示,说明是模型资源的问题。

  4. 启用相机控制调整视角
    Volumetric窗口的初始相机视角可能看不到实体,添加相机控件手动调整:

    RealityView { content in
        // ... 添加实体代码 ...
    }
    .cameraControls()
    
  5. 检查自定义组件注册逻辑
    确保DraggableComponent等自定义组件符合RealityKit要求:继承Component协议,正确实现静态registerComponent()方法,没有注册逻辑错误导致实体异常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 16:57:50