我有两个ViewControllers包含一个ARView。守则如下:
import UIKit
import RealityKit
import ARKit
class fvBoat: UIViewController, ARSessionDelegate {
@IBOutlet var arView: ARView!
let fvBoatAnchor = try! Vard.loadFvBoatScene()
var imageAnchorToEntity: [ARImageAnchor: AnchorEntity] = [:]
override func viewDidLoad() {
super.viewDidLoad()
fvBoatAnchor.generateCollisionShapes(recursive: true)
let fvBoat = fvBoatAnchor.fvBoatObject as? Entity & HasCollision
arView.installGestures(for: fvBoat!)
arView.scene.addAnchor(fvBoatAnchor)
arView.session.delegate = self
}
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
anchors.compactMap { $0 as? ARImageAnchor }.forEach {
let anchorEntity = AnchorEntity()
let modelEntity = fvBoatAnchor.fvBoatObject!
anchorEntity.addChild(modelEntity)
arView.scene.addAnchor(anchorEntity)
anchorEntity.transform.matrix = $0.transform
imageAnchorToEntity[$0] = anchorEntity
}
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
anchors.compactMap { $0 as? ARImageAnchor }.forEach {
let anchorEntity = imageAnchorToEntity[$0]
anchorEntity?.transform.matrix = $0.transform
}
}
func installGestures(on object:ModelEntity){
object.generateCollisionShapes(recursive: true)
arView.installGestures([.rotation,.scale], for: object)
}
}两个视图控制器都具有与上面相同的代码。
每当我使用一个当前模式索引导航到下一个ARview时,我的框架就会显著下降。如何确保在进入下一个ARview时关闭ARview会话?
也尝试过添加这个函数,但不确定为什么它不能工作.
func leaveScene() {
arView?.session.pause()
arView?.removeFromSuperview()
arView = nil
}发布于 2022-03-14 09:56:45
不是所有需要关闭的东西你都关掉了。
func leaveScene() {
arView?.session.pause()
arView?.session.delegate = nil
arView?.scene.anchors.removeAll()
arView?.removeFromSuperview()
arView?.window?.resignKey()
arView = nil
}但是arView不会从内存中被释放。
发布于 2022-03-14 15:43:21
这为我解决了问题。使用@Andy的代码,我添加了leaveScene函数并将其调用为使用来自UIbutton的发送事件:
@IBAction func leaveScene(_ sender: UIbutton) {
leaveScene()
}让我的代码看起来像这样。
import UIKit
import RealityKit
import ARKit
class fvBridge: UIViewController, ARSessionDelegate {
@IBOutlet var arView: ARView!
let fvBridgeAnchor = try! Experience.loadFvBridgeScene()
var imageAnchorToEntity: [ARImageAnchor: AnchorEntity] = [:]
override func viewDidLoad() {
super.viewDidLoad()
fvBridgeAnchor.generateCollisionShapes(recursive: true)
let fvBridge = fvBridgeAnchor.fvBridgeObject as? Entity & HasCollision
arView.installGestures(for: fvBridge!)
arView.scene.addAnchor(fvBridgeAnchor)
arView.session.delegate = self
}
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
anchors.compactMap { $0 as? ARImageAnchor }.forEach {
let anchorEntity = AnchorEntity()
let modelEntity = fvBridgeAnchor.fvBridgeObject!
anchorEntity.addChild(modelEntity)
arView.scene.addAnchor(anchorEntity)
anchorEntity.transform.matrix = $0.transform
imageAnchorToEntity[$0] = anchorEntity
}
}
func session(_ session: ARSession, didUpdate anchors: [ARAnchor]) {
anchors.compactMap { $0 as? ARImageAnchor }.forEach {
let anchorEntity = imageAnchorToEntity[$0]
anchorEntity?.transform.matrix = $0.transform
}
}
func installGestures(on object:ModelEntity){
object.generateCollisionShapes(recursive: true)
arView.installGestures([.rotation,.scale], for: object)
}
func leaveScene() {
arView?.session.pause()
arView?.session.delegate = nil
arView?.scene.anchors.removeAll()
arView?.removeFromSuperview()
arView?.window?.resignKey()
arView = nil
}
@IBAction func leaveScene(_ sender: Any) {
leaveScene()
}
}https://stackoverflow.com/questions/71459891
复制相似问题