ARKit开发中ARPlaneAnchor的extent属性已弃用,如何适配?
解决ARPlaneAnchor的
extent属性在iOS 16及以上被弃用的问题 iOS 16开始,ARKit的ARPlaneAnchor弃用了extent属性,取而代之的是geometry属性(类型为ARPlaneGeometry),这个新属性不仅包含平面的尺寸信息,还提供了平面的网格拓扑结构,能更精准地匹配检测到的真实平面。
你可以直接用geometry的width和height来替代原来的extent.x和extent.z,修改后的代码如下:
guard let planeAnchor = anchor as? ARPlaneAnchor else { return } // iOS 16+ 获取平面尺寸 let planeGeometry = planeAnchor.geometry let plane = SCNPlane(width: CGFloat(planeGeometry.width), height: CGFloat(planeGeometry.height))
如果你的应用需要兼容iOS 16以下的版本,可以通过版本判断来兼容新旧API:
guard let planeAnchor = anchor as? ARPlaneAnchor else { return } var planeWidth: CGFloat var planeHeight: CGFloat if #available(iOS 16.0, *) { let planeGeometry = planeAnchor.geometry planeWidth = CGFloat(planeGeometry.width) planeHeight = CGFloat(planeGeometry.height) } else { planeWidth = CGFloat(planeAnchor.extent.x) planeHeight = CGFloat(planeAnchor.extent.z) } let plane = SCNPlane(width: planeWidth, height: planeHeight)
需要注意的是,ARPlaneGeometry的width对应原extent.x(平面X轴方向的长度),height对应原extent.z(平面Z轴方向的长度),和你原来的使用逻辑完全一致,不需要调整尺寸的对应关系。
内容的提问来源于stack exchange,提问作者NhlanhlaNkosi
相关产品推荐
相关产品推荐

