Dynamically Loading USDZ Objects from an Array into a fully immersive Scene using Reality Kit

Hello, I am currently working on a project where I am creating a bookstore visualization with racks and shelves(Full immersive view). I have an array of names, each representing a USDZ object that is present in my working directory. Here’s the enum I am trying to iterate over:

enum AssetName: String, Codable, Hashable, CaseIterable {
    case book1 = "B1"
    case book2 = "B2"
    case book3 = "B3"
    case book4 = "B4"
}

and the code for adding objects I wrote:

import SwiftUI
import RealityKit

struct LocalAssetRealityView: View {
    let assetName: AssetName
    var body: some View {
        RealityView { content in
            if let asset = try? await ModelEntity(named: assetName.rawValue) {
                content.add(asset)
            }
        }
    }
}

Now I get the error, when I try to add multiple objects on Button click:

Unable to present another Immersive Space when one is already requested or connected

please suggest any solutions. Also suggest if anything can be done to add positions for the objects as well programatically.

Looks like you are creating multiple RealityViews rather than adding content to the existing one. If you move the iteration inside the RealtyView, it should work.

Dynamically Loading USDZ Objects from an Array into a fully immersive Scene using Reality Kit
 
 
Q