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!

  • @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!

Add a Comment

Accepted Reply

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).

Replies

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)
Post not yet marked as solved Up vote reply of J0hn Down vote reply of J0hn
  • Many thx @J0hn. With content.add(panelEntity) I get the attachment on screen visualized! In the wwdc2023 video and code example they don't use this and apply parentEntity.addChild(panelEntity) instead - but this doesn't work for me. So content.add seems to be must-have?

    PS: I want to pin an attachment (text label) to an entity so that the label automatically moves with the entity and thought addChild would do this.

  • @FrankZielen

    Hmm, Yeah addChild should work for that.

    Content.add will add the attachment View relative to the 0,0,0 position of the realityView.

    If you add it to a parent entity it will be added relative to the 0,0,0 position of that entity. Is it possible its inside the parent entity?

  • @J0hn First I thought the child would be hidden by the parent (do you mean this with "inside"?) but I have repositioned it without any success. Then I tried to change scale factor in case it should be too small - same result. However, the child is added in the children collection. The parent entity is embedded in a scene created by Reality Composer Pro. Maybe this is the problem?

Add a Comment

attachments are optionals so you have to unwrap them:

   if let panelEntity = attachments.entity(for: "panel") {
     		content.add(panelEntity)
 	 }
  • Adding via content.add works but in wwdc2023 code example they just use addChild which does not work for me. But I want to bind the attachment to the parent entity so that they e.g. move together when I reposition the parent.

  • Then before you add the panelEntity you have to set the Position relative the parent entity

    panelEntity.setPosition((SIMD3(x: attachmentOffsetX, y: attachmentOffsetY, z: attachmentOffsetZ)), relativeTo: parentEntity)

  • But then panelEntity is still not tied to parentEntity, i.e. when I move parentEntity panelEntity stays fixed in the room.

Add a Comment

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).