RealityView attachments do not show up in Vision Pro simulator

I have added an attachments closure in an RealityView as outlined in WWDC session "Enhance your spatial computing app with RealityKit" but it's not showing up - neither in Xcode preview window nor in Vision Pro simulator. I have used example code 1:1, however, I had to load the entity async with "try? await" to satisfy the compiler. Any help is appreciated, thx in advance!

Answered by FrankZielen in 762586022

Finally I got the solution (at least for my problem):

Let parentEntity be loaded to RealityView via make closure and panelEntity be included in the RealityView as attachment, e.g. representing a text label. Then you can either use

content.add(panelEntity)

to bring the attachment to live (as proposed by @J0hn), however, it's isolated and not tied to the parent. So if you want to link panel and parent (e.g. for automatically movement of the label with the parent) you need to use

parentEntity.addChild(panelEntity, preservingWorldTransform: true)

Here preservingWorldTransform was the key for me. Default setting is false and without this flag the panel was not shown on screen (at least in Xcode preview and Vision Pro simulator).

Attachments definitely work.

You "nominate" an attachment SwiftUI like so:

attachments: {
  Text("hello")
    .glassBackgroundEffect()
    .tag("panel") // <---------- NOTE THE TAG
 }

This closure can return different results as the state of your scene changes. So if you want an attachment to disappear, just stop returning it from here.

After an attachment is nominated, it needs to be added to the scene in the update method of RealityView.

First see if RealityKit has synthesized an entity for the attachment you provided:

update: { content, attachments  in
   let panelEntity = attachments.entity(for: "panel") // <------- NOTE THAT IT MATCHES THE NOMINATED TAG NAME. 

   // [...]
}

Once you have that entity you can transform it, plop add it onto another entity, or straight into the content view itself.

content.add(panelEntity)

attachments are optionals so you have to unwrap them:

   if let panelEntity = attachments.entity(for: "panel") {
     		content.add(panelEntity)
 	 }

@J0hn @PatrickDevX When I add the panelEntity (added as attachment) via parentEntity.addChild(panelEntity, preservingWorldTransform: true) the panelEntity becomes visible but it's not tied to the parentEntity and keeps fixed when parentEntity moves in space. When I set preservingWorldTransform to false (which is addChild default setting) the panelEntity does not show up as before.

@J0hn @PatrickDevX Sorry - all back! parentEntity.addChild(panelEntity, preservingWorldTransform: true) makes it work! This flag brings the attachment on screen and ties it to the parent!

Accepted Answer

Finally I got the solution (at least for my problem):

Let parentEntity be loaded to RealityView via make closure and panelEntity be included in the RealityView as attachment, e.g. representing a text label. Then you can either use

content.add(panelEntity)

to bring the attachment to live (as proposed by @J0hn), however, it's isolated and not tied to the parent. So if you want to link panel and parent (e.g. for automatically movement of the label with the parent) you need to use

parentEntity.addChild(panelEntity, preservingWorldTransform: true)

Here preservingWorldTransform was the key for me. Default setting is false and without this flag the panel was not shown on screen (at least in Xcode preview and Vision Pro simulator).

RealityView attachments do not show up in Vision Pro simulator
 
 
Q