Hi @adizhol,
Are you trying to use attachments in a RealityView and share variables across the entity and attachment? If so, here's an example using an Observable class:
@Observable
class Model {
var count: Int = 0
}
struct ImmersiveView: View {
@State private var model: Model = Model()
var body: some View {
RealityView { content, attachments in
// Create the entity
let cube = ModelEntity(mesh: .generateBox(size: 0.1), materials: [SimpleMaterial(color: .green, isMetallic: true)])
cube.components.set(HoverEffectComponent())
cube.components.set(InputTargetComponent())
cube.generateCollisionShapes(recursive: true)
content.add(cube)
cube.setPosition([0, 0.75, -1], relativeTo: nil)
// add the attachment
if let attachment = attachments.entity(for: "tapAttachment") {
cube.addChild(attachment)
attachment.setPosition([0, 0.2, 0], relativeTo: cube)
}
} attachments: {
Attachment(id: "tapAttachment") {
HStack {
Text("Taps:")
// Use the observable model's count as the text in the attachment. this will update each time you tap the entity
Text(model.count.description)
}
.padding()
.glassBackgroundEffect()
}
}
.gesture(SpatialTapGesture().targetedToAnyEntity()
.onEnded({ value in
// update the tap count
model.count += 1
}))
}
}
If this is not what you were looking to do, can you please explain further?