如何将RoomPlan扫描硬编码到App中,实现无LiDAR设备的功能测试?
解决方案:在RoomPlan App中内置测试扫描数据
核心思路
先在有LiDAR的真实设备上捕获并归档一份CapturedRoom数据,将该归档文件作为资源嵌入App,在无LiDAR的设备/模拟器上直接加载这份内置资源,替代本地存储的加载逻辑。
步骤1:导出测试用的归档数据
在有LiDAR的设备上运行临时代码,将已捕获的CapturedRoom导出为归档文件:
// 临时代码:导出已捕获的房间数据到设备Documents目录 func exportTestRoom(_ room: CapturedRoom) { do { let archivedData = try NSKeyedArchiver.archivedData( withRootObject: room, requiringSecureCoding: true ) let docsDir = FileManager.default.urls( for: .documentDirectory, in: .userDomainMask ).first! let exportPath = docsDir.appendingPathComponent("TestRoom_01.archive") try archivedData.write(to: exportPath) print("测试数据已导出:\(exportPath)") } catch { print("导出失败:\(error.localizedDescription)") } }
运行后,通过Xcode的「Window > Devices and Simulators」找到对应设备,打开「Download Container」,解压后从AppData/Documents目录取出TestRoom_01.archive文件。
步骤2:将归档文件嵌入App
把取出的.archive文件拖入Xcode项目,在弹出的窗口中勾选Target Membership(确保文件被打包进App bundle)。
步骤3:修改加载逻辑,适配无LiDAR设备
3.1 检测设备是否支持LiDAR
func isLiDARAvailable() -> Bool { // 检测是否支持RoomPlan所需的LiDAR场景重建 return ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh) }
3.2 加载内置测试数据
func loadTestCapturedRoom() -> CapturedRoom? { guard let testFileURL = Bundle.main.url( forResource: "TestRoom_01", withExtension: "archive" ) else { print("未找到内置测试数据文件") return nil } do { let fileData = try Data(contentsOf: testFileURL) guard let room = try NSKeyedUnarchiver.unarchivedObject( ofClass: CapturedRoom.self, from: fileData ) else { print("测试数据解析失败") return nil } return room } catch { print("加载测试数据出错:\(error.localizedDescription)") return nil } }
3.3 整合加载逻辑
func loadCapturedRoom() -> CapturedRoom? { if isLiDARAvailable() { // 原有逻辑:从本地存储加载用户扫描的数据 let savedRoomURL = FileManager.default.urls( for: .documentDirectory, in: .userDomainMask ).first!.appendingPathComponent("UserSavedRoom.archive") do { let savedData = try Data(contentsOf: savedRoomURL) return try NSKeyedUnarchiver.unarchivedObject( ofClass: CapturedRoom.self, from: savedData ) } catch { print("加载用户数据失败:\(error.localizedDescription)") return nil } } else { // 无LiDAR设备:加载内置测试数据 return loadTestCapturedRoom() } }
注意事项
- 确保
CapturedRoom及其关联对象都符合NSCoding协议(RoomPlan框架的原生类已实现此协议,无需额外处理) - 导出的归档文件需与App的部署系统版本兼容(比如iOS 16+导出的文件在iOS 16+模拟器上可正常加载)
- 如需多份测试数据,重复步骤1导出不同的
.archive文件,在loadTestCapturedRoom中可通过参数指定加载目标文件
内容的提问来源于stack exchange,提问作者denniswave
相关产品推荐
相关产品推荐

