I have this simple RealityKit and SwiftUI code taken from the template and while the build can run on Simulator just fine, the RealityKit scene won't show on the SwiftUI's Preview.
This is the code
struct ContentView : View {
var body: some View {
ARViewContainer().edgesIgnoringSafeArea(.all)
}
}
struct ARViewContainer: UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let boxAnchor = try! Experience.loadBox()
arView.environment.background = .color(.systemBackground)
arView.scene.anchors.append(boxAnchor)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {}
}
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
It'll be nice if preview window can show what I'm doing in the RealityKit scene rather than have to run on Simulator all the time if I make any changes.
Is there a way to do it? Or is this how the preview supposed to be?
Use this solution until Apple engineers fix this bug. Paste ARViewContainer()
into preview's body instead of ContentView()
, and then temporarily replace ContentView body's content with EmptyView()
:
import SwiftUI
import RealityKit
struct ARViewContainer : UIViewRepresentable {
func makeUIView(context: Context) -> ARView {
let arView = ARView(frame: .zero)
let boxScene = try! Experience.loadBox()
arView.scene.anchors.append(boxScene)
return arView
}
func updateUIView(_ view: ARView, context: Context) { }
}
struct ARViewContainer_Preview : PreviewProvider {
static var previews: some View {
ARViewContainer().ignoresSafeArea()
}
}
struct ContentView : View {
var body: some View { EmptyView() }
}
Xcode 14.3, target iOS 16.4.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With