Home > Net >  Create segues in RealityKit
Create segues in RealityKit

Time:07-22

I want to add a segue in my Reality Kit project between a simple swiftUI page and the AR camera ContentView. Here is my SegueView, the swiftui page that has a button which I want to segue to the AR view.

struct SegueView: View {
    @State private var isActive: Bool = false
    var body: some View {
        NavigationLink(destination: ContentView(rootActive: $isActive), isActive: $isActive, label: {
            Text("See in AR")
        })
    }
}

Here is the code from my ContentView, which only renders a box:

struct ContentView : View {
    @Binding var rootActive: Bool
    var body: some View {
        return ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}

struct ARViewContainer: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        
        let arView = ARView(frame: .zero)
        
        // Load the "Box" scene from the "Experience" Reality File
        let anchor = AnchorEntity(plane: .horizontal)
        let box = ModelEntity(mesh: MeshResource.generateBox(size: 0.3), materials: [SimpleMaterial(color: .blue, isMetallic: true)])
        
        anchor.addChild(box)
        // Add the box anchor to the scene
        arView.scene.anchors.append(anchor)
        
        return arView
        
    }
    func updateUIView(_ uiView: ARView, context: Context) {} 
}

I followed this tutorial to create the segue, but it doesn't contain an AR view and I cannot find a tutorial that does. I also have to add the rootActive variable in the app delegate here:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Create the SwiftUI view that provides the window contents.
        let contentView = ContentView(rootActive: .constant(true))

        // Use a UIHostingController as window root view controller.
        let window = UIWindow(frame: UIScreen.main.bounds)
        window.rootViewController = UIHostingController(rootView: contentView)
        self.window = window
        window.makeKeyAndVisible()
        return true
    }

But since the tutorial doesn't have this step, I'm not entirely sure what to do. When I run the app, only the ContentView gets rendered. Does anyone know how I can create a segue where the SegueView is rendered first, and then when the navigation link "see in AR" is pressed, it goes to the ContentView?

Thanks!

CodePudding user response:

In ContentView, put NavigationLink inside NavigationView.

import SwiftUI
import RealityKit

struct ContentView: View {
    var body: some View {           
        NavigationView {
            ZStack {
                Color.black
                
                VStack {    
                    NavigationLink(destination: Reality().ignoresSafeArea()) {
                        Text("Go to ARView")
                            .foregroundColor(.cyan)
                            .font(.title3)
                    }                   
                }
            }.ignoresSafeArea()
        }
    }
}

ARView

struct Reality: UIViewRepresentable {
    
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        let model = ModelEntity(mesh: .generateSphere(radius: 0.2))
        let anchor = AnchorEntity(plane: .horizontal)
        anchor.addChild(model)
        arView.scene.anchors.append(anchor)
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) { }
}

P. S.

In project's Info tab add key Privacy - Camera Usage Description.

  • Related