Search code examples
swiftswift5realitykitreality-composer

SWIFT: Load different reality Scenes via Variable


So, i am trying to load different Scenes made in RealityComposer, depending on a variable.

What worked so far:

let SceneAnchor = try! Experience1.loadScene()
arView.scene.anchors.append(SceneAnchor)
return arView

Now i looked into apples Documentation and saw the possibility of:

    if let anchor = try? Entity.loadAnchor(named: "Scene") {
        arView.scene.addAnchor(anchor)
    }

where i thought i could just change "Scene" to "Scene(myVar)" But once i have more than one scene in my file the first solution doesnt work anymore and the second one doesnt work as well.

What am i missing?

I also looked into working with filenames and was able to make an array of all my .reality Files and Store them in an Array, so i thought i could recall that via the index, but arrayName[1].loadScene() doesnt seem to work either, eventhough i can print the filenames to console.

Thanks in advance :)


Solution

  • The fact is that Reality Composer creates a separate static method for each scene load. The name of such method is load+scene name. So, if you have 2 scenes in your Exprerience.xcproject with the names Scene and Scene1, then you have 2 static methods

    let scene = Experience.loadScene()
    let scene1 = Experience.loadScene1()
    

    Unfortunately it is not possible to use the scene name as a parameter, so, you need to use the switch statement in your app to select the appropriate method.