本文介绍了RealityKit –从同一Reality Composer项目加载另一个场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用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个场景,分别称为alphabravodelta.

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.

加载此场景后,如何在不必加载整个场景的情况下切换到另一个场景alphabravodelta?

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项目加载另一个场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 07:41