admin管理员组

文章数量:1279021

I want to create virtual objects on tap.

I know that I can use the gesture call to react to gestures, but I don't see how I can access the RealityViewContent from there in order to add it to the scene or subscribe to collision events.

RealityView { content in 
  ... 
}.gesture(TapGesture().targetedToAnyEntity().onEnded{ event in 
  let o = create_a_square() 

  // I want to do this:
  content.add(o);
  content.subscribe(to: CollisionEvents.Began.self, on: o) ..
  
}

I want to create virtual objects on tap.

I know that I can use the gesture call to react to gestures, but I don't see how I can access the RealityViewContent from there in order to add it to the scene or subscribe to collision events.

RealityView { content in 
  ... 
}.gesture(TapGesture().targetedToAnyEntity().onEnded{ event in 
  let o = create_a_square() 

  // I want to do this:
  content.add(o);
  content.subscribe(to: CollisionEvents.Began.self, on: o) ..
  
}
Share Improve this question edited Feb 25 at 10:16 jonrsharpe 122k30 gold badges267 silver badges474 bronze badges asked Feb 25 at 10:15 DarkTrickDarkTrick 3,5293 gold badges30 silver badges54 bronze badges 1
  • The cube sample code from apple has a really good example. I don't remember which one it is but it is the ones were you do a spatial tap gesture and a cube gets added to the scene – lorem ipsum Commented Feb 26 at 12:15
Add a comment  | 

1 Answer 1

Reset to default 0

For adding / removing entities in gestures (or other locations):

Apple's sample code adds an entity** to content, stores it in an environment variable add uses rootEntity.addChild and rootEntity.removeChild later for accessing.

** In the SceneReconstruction example by Apple it's called model. Other common names might be rootEntity.

For accessing content to subscribe to events:

You can go with the same logic

本文标签: visionosHow to access RealityViewContent from gesture handlersStack Overflow