RealityKit – 无法加载 ARView(未找到)
RealityKit – Cannot load ARView (found nil)
我试图在 VC 中打开 ARView 和 Experience.rcproject 文件,但问题是我的屏幕黑屏。为什么?
import RealityKit
import UIKit
import ARKit
class ArVC: UIViewController, ARSCNViewDelegate {
lazy var sceneView: ARSCNView = {
let sceneView = ARSCNView()
sceneView.delegate = self
return sceneView
}()
var arView: ARView = {
let arView = ARView()
let boxAnchor = try! Experience.loadBox()
arView.scene.anchors.append(boxAnchor)
return arView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .clear
}
func createView() {
self.view.addSubview(sceneView)
self.view.addSubview(self.sceneView)
self.sceneView.snp.makeConstraints { make in
make.centerY.centerX.equalToSuperview()
}
view.subviews.forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
}
self.sceneView.addSubview(self.arView)
self.arView.snp.makeConstraints { make in
make.centerX.centerY.equalToSuperview()
}
}
}
还有哪些其他选项可以以编程方式设置 ARView?谢谢。
这段测试代码可以解决问题。如果您从头开始构建 AR 项目(通过选择简单的 iOS 应用程序模板,而不是 ARKit 模板),请确保您在 [=19] 中添加了 Privacy - Camera Usage Description
和 Required device capabilities (Item 0 = ARKit)
=]info.plist 文件.
import RealityKit
import UIKit
class ViewController: UIViewController {
var arView: ARView = {
let arView = ARView(frame: .zero)
let boxAnchor = try! Experience.loadBox()
arView.scene.anchors.append(boxAnchor)
return arView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .red
self.view.addSubview(self.arView)
NSLayoutConstraint.activate([
arView.topAnchor.constraint(equalTo: self.view.topAnchor),
arView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
arView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
arView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])
self.view.subviews.forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
}
}
}
我还注意到你添加了两次 sceneView
。
我试图在 VC 中打开 ARView 和 Experience.rcproject 文件,但问题是我的屏幕黑屏。为什么?
import RealityKit
import UIKit
import ARKit
class ArVC: UIViewController, ARSCNViewDelegate {
lazy var sceneView: ARSCNView = {
let sceneView = ARSCNView()
sceneView.delegate = self
return sceneView
}()
var arView: ARView = {
let arView = ARView()
let boxAnchor = try! Experience.loadBox()
arView.scene.anchors.append(boxAnchor)
return arView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .clear
}
func createView() {
self.view.addSubview(sceneView)
self.view.addSubview(self.sceneView)
self.sceneView.snp.makeConstraints { make in
make.centerY.centerX.equalToSuperview()
}
view.subviews.forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
}
self.sceneView.addSubview(self.arView)
self.arView.snp.makeConstraints { make in
make.centerX.centerY.equalToSuperview()
}
}
}
还有哪些其他选项可以以编程方式设置 ARView?谢谢。
这段测试代码可以解决问题。如果您从头开始构建 AR 项目(通过选择简单的 iOS 应用程序模板,而不是 ARKit 模板),请确保您在 [=19] 中添加了 Privacy - Camera Usage Description
和 Required device capabilities (Item 0 = ARKit)
=]info.plist 文件.
import RealityKit
import UIKit
class ViewController: UIViewController {
var arView: ARView = {
let arView = ARView(frame: .zero)
let boxAnchor = try! Experience.loadBox()
arView.scene.anchors.append(boxAnchor)
return arView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .red
self.view.addSubview(self.arView)
NSLayoutConstraint.activate([
arView.topAnchor.constraint(equalTo: self.view.topAnchor),
arView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
arView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor),
arView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor)
])
self.view.subviews.forEach {
[=10=].translatesAutoresizingMaskIntoConstraints = false
}
}
}
我还注意到你添加了两次 sceneView
。