Canvatorium Visio Lab 5016
Select an entity from a scene on tap.
Use SpatialTapGesture with targetedToAnyEntity to select entities in a RealityView scene.
struct Lab5016: View {
@State var selected: String = "Tap Something"
var tap: some Gesture {
SpatialTapGesture()
.targetedToAnyEntity()
.onEnded { value in
selected = value.entity.name
}
}
var body: some View {
RealityView { content, attachments in
if let scene = try? await Entity(named: "5016SelectEntity", in: realityKitContentBundle) {
content.add(scene)
}
//Position the attachment somewhere we can see it
if let attachmentEntity = attachments.entity(for: "SelectedLabel") {
attachmentEntity.position = [0.8, 1.5, -2]
attachmentEntity.scale = [2,2,2]
content.add(attachmentEntity)
}
} attachments: {
Attachment(id: "SelectedLabel") {
Text(selected)
.font(.largeTitle)
.padding(18)
.background(.black)
.cornerRadius(12)
}
}
.gesture(tap)
}
}