Contents

RealityView

A view that contains RealityKit content.

Declaration

@MainActor @preconcurrency struct RealityView<Content> where Content : View

Mentioned in

Overview

Use RealityView to display rich 3D RealityKit content in your app, including content you author in Reality Composer Pro. RealityView passes a structure that conforms to RealityViewContentProtocol to its make and update closures, which you can use to add and remove RealityKit entities to your view.

Here is a simple example showing how you can display a custom ModelEntity using RealityView:

struct ModelExample: View {
    var body: some View {
        RealityView { content in
            if let robot = try? await ModelEntity(named: "robot") {
                content.add(robot)
            }
            Task {
                // Asynchronously perform any additional work to configure
                // the content after the system renders the view.
            }
        }
    }
}

Note that the closure in the example above is async, and can be used to load contents from your app’s bundle or from any URL in the background. While your content is loading, RealityView will automatically display a placeholder view, which you can customize using the optional placeholder parameter.

You can also use the optional update closure on your RealityView to update your RealityKit content in response to changes in your view’s state. RealityView displays your RealityKit content inline in true 3D space, occupying the available space in your app’s 3D bounds. The RealityViewContent type on visionOS, and RealityViewCameraContent on other platforms represents the content of your RealityView.

If you want to run code every frame (to do animations or simulations), you can use a System or directly subscribe to the engine’s SceneEvents.Update:

RealityView { content in
   let entity = ModelEntity(mesh: .generateSphere(radius: 0.1))
   content.add(entity)
   _ = content.subscribe(to: SceneEvents.Update.self) { event in
       entity.position.y -= Float(event.deltaTime)
   }
}

RealityView has a flexible size by default, and does not size itself based on the RealityKit content it displays. For more advanced uses of RealityKit, such as subscribing to RealityKit events, performing coordinate conversions, or working with AR capabilities, refer to the RealityViewContentProtocol types.

Topics

Creating a reality view for visionOS

Creating a reality view for iOS and macOS

Inspecting the content within a reality view

Initializers

See Also

SwiftUI scene presentation