How to trigger functions to save and load worldMap data while using RealityKit, ARKit and SwiftUI?

414 Views Asked by At

I’m building an augmented reality app with RealityKit, ARKit and SwiftUI. The content view has a ZStack that contains the ARView and the Swift views layered over it.

More specifically the ARView is there as ARContentView()— a swift view that wraps the ARViewContainer(), which in itself represents the ARView in RealityKit.

I’m adding photos of the code below.

contentView with ZStack that includes layered UI Views and ARContentView

The goal is for the user to be able to save and load the worldMap data so as to persist the AR experience after they close and reopen their app.

The problem is that I’m trying to trigger the functions to save and to load the world map data by clicking on buttons in the SwiftUI views, and these buttons have to trigger the functions to save and load world map data in the ARView.

I can trigger the code to save the worldMap data from within the ARView by adding gestures to the view (eg swiping up), as seen below, but I need to actually link these functions from within the Content View buttons.

WHAT IS REALLY CONFUSING ME :

Because the ARView is already defined by Apple and I can only add functionality to it by extending it and adding new functions, I can’t add a binding variable to it that keeps up with the state of events in the Content View (since stored variables can’t be added to extensions).

an example of one of the functions that uses the current AR session to interact with the world map object

How can I trigger these different ARView dependent actions within the parent SwiftUI view that contains the ARContentView in it’s ZStack?

Likewise … The question remains the other way around, when an event takes place in the ARView, how can I send the returned info back to the SwiftUI content view so it can make a change in its state variables and trigger an animation? (this could be a separate question but the basis for the problem is still connecting between these two types of views)

The ARViewContainer struct that comes for free with initiating a new Augmented Reality RealityKit project has a couple of functions - makeUIView() and updateUIView() baked into them, but I haven’t found any tutorials on how to use the updateUIView() function.

Image of ARContainerView taking in a var for construction plus the functions within it

I’m wondering if it the updateUIView() function within ARContainerView struct is there to solve the issue I’m having of connectivity between these layers, if so how to use it, and if not how else to solve the problem ?

Please let me know if anything is unclear. Thanks for the help.

1

There are 1 best solutions below

0
On

When you need to coordinate between your SwiftUI view and the UIViewRepresentable that houses your ARView, you use a ARViewCoordinator and ARSessionDelegate to pass the data between them. When I have time I will load up an example or you can check out Reality School on Youtube for a tutorial.