I know that all swiftUI views and buttons can react to where the user is looking in visionOS, but I'd like to do the same thing with entities in RealityKit, so users know what they are looking at. I can see that in some games downloaded from the store, 3d buttons seem to be reacting and glowing, I'm just not sure how they are doing it. Are these attached swiftUI views or is there a way to register entities for focus?
when you create your entity, just add a HoverEffectComponent to it. The entity also has to have both a CollisionComponent and InputTargetComponent.
let collisionShape = ShapeResource.generateBox(width: w, height: h, depth: d)
entity.components.set(HoverEffectComponent())
entity.components.set(InputTargetComponent())
entity.components.set(CollisionComponent(shapes:[collisionShape], isStatic: true))
you don't have to add a gesture for this to work. But it only works if these 3 components are present.
I found your question while trying to figure this out myself. The answer was in this video, which is worthwhile to watch in full: https://developer.apple.com/videos/play/wwdc2023/10080/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With