How to programmatically add a model in RealityKit

To programmatically add a model to a window, volume or space, use a RealityView, load the model and add it to the RealityView's content.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 08/22/2024 at 12:09
Last Updated on 08/26/2024 at 12:45
<p>Three flowers that represent SwiftUI, RealityKit and XCode side by side. Beneath them sits the text "Programmatically Add Content".</p>

To programmatically add a model to a window, volume or space, use a RealityView, load the model and add it to the RealityView's content.

Subscribe

The code for the demonstration detailed on this post can be found at the tutorials/immersive-spaces/position-scale-and-rotation-with-animation branch within the repository below.

View Repository
<p>A screenshot of XCode showing how to programmatically add content to an RealityView.</p>

Specifically, the code that adds content to a window, volume or space is found within the body, where it loads an entity (i.e. scene) and then adds it the element using content.add(scene).

This content parameter is gathered from the RealityView's closure (i.e. { content in ... }).

<p>A screenshot of the simulator showing the model in space.</p>

If you run similar code in the simulator, you will see a result similar to the one above.

Looking to learn more about SwiftUI, Swift, RealityKit, Design and Technology?

Search our blog to learn more about Swift, SwiftUI, RealityKit, design and technology.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe