问题描述
我使用Xcode的模板创建一个增强现实项目.
I create an Augmented Reality Project using Xcode's template.
Xcode创建一个名为Experience.rcproject
的文件.
Xcode creates a file called Experience.rcproject
.
该项目包含一个名为Box
的场景和一个名为Steel Cube
的立方体.
This project contains a scene called Box
and a cube called Steel Cube
.
我向Experience.rcproject
添加了3个场景,分别称为alpha
,bravo
和delta
.
I add 3 more scenes to Experience.rcproject
, called alpha
, bravo
and delta
.
我运行项目.
Xcode运行这两行
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)
这些行从Experience文件加载Box场景.
These lines load the Box scene from the Experience file.
加载此场景后,如何在不必加载整个场景的情况下切换到另一个场景alpha
,bravo
或delta
?
Once this scene is loaded how do I switch to another scene alpha
, bravo
or delta
without having to load the whole thing?
推荐答案
RealityKit中切换来自Reality Composer的两个或多个场景的最简单方法是使用removeAll()
实例方法,允许您从数组中删除所有锚点
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.
您可以使用asyncAfter(deadline:execute:)
方法切换两个场景:
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)
}
或者您可以使用常规的UIButton
切换两个不同的RC场景:
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)
}
这篇关于RealityKit –从同一Reality Composer项目加载另一个场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!