I create an Augmented Reality Project using Xcode's template.
Xcode creates a file called Experience.rcproject
.
This project contains a scene called Box
and a cube called Steel Cube
.
I add 3 more scenes to Experience.rcproject
, called alpha
, bravo
and delta
.
I run the project.
Xcode runs these two lines
// Load the "Box" scene from the "Experience" Reality File
let boxAnchor = try! Experience.loadBoxX(namedFile: "Ground")
// Add the box anchor to the scene
arView.scene.anchors.append(boxAnchor)
These lines load the Box scene from the Experience file.
Once this scene is loaded how do I switch to another scene alpha
, bravo
or delta
without having to load the whole thing?
The simplest approach in RealityKit to switch two or more scenes coming from Reality Composer is to use removeAll()
instance method, allowing you to delete all the anchors from array.
You can switch two scenes using asyncAfter(deadline:execute:)
method:
let boxAnchor = try! Experience.loadBox()
arView.scene.anchors.append(boxAnchor)
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.arView.scene.anchors.removeAll()
let sphereAnchor = try! Experience.loadSphere()
self.arView.scene.anchors.append(sphereAnchor)
}
Or you can switch two different RC scenes using a regular UIButton
:
@IBAction func loadNewSceneAndDeletePrevious(_ sender: UIButton) {
self.arView.scene.anchors.removeAll()
let sphereAnchor = try! Experience.loadSphere()
self.arView.scene.anchors.append(sphereAnchor)
}