22// Licensed under the MIT License. See LICENSE in the project root for license information.
33
44using UnityEngine ;
5- using UnityEngine . VR . WSA ;
6- using UnityEngine . VR . WSA . Persistence ;
75
86namespace HoloToolkit . Unity
97{
@@ -24,144 +22,84 @@ public partial class TapToPlace : MonoBehaviour
2422 public string SavedAnchorFriendlyName = "SavedAnchorFriendlyName" ;
2523
2624 /// <summary>
27- /// Keeps track of anchors stored on local device .
25+ /// Manages persisted anchors.
2826 /// </summary>
29- WorldAnchorStore anchorStore = null ;
27+ private WorldAnchorManager anchorManager ;
3028
3129 /// <summary>
32- /// Locally saved wold anchor.
30+ /// Controls spatial mapping. In this script we access spatialMappingManager
31+ /// to control rendering and to access the physics layer mask.
3332 /// </summary>
34- WorldAnchor savedAnchor ;
35-
36- bool placing = false ;
37-
38- void Start ( )
39- {
40- WorldAnchorStore . GetAsync ( AnchorStoreReady ) ;
41- }
33+ private SpatialMappingManager spatialMappingManager ;
4234
4335 /// <summary>
44- /// Called when the local anchor store is ready .
36+ /// Keeps track of if the user is moving the object or not .
4537 /// </summary>
46- /// <param name="store"></param>
47- void AnchorStoreReady ( WorldAnchorStore store )
48- {
49- anchorStore = store ;
38+ private bool placing ;
5039
51- // Try to load a previously saved world anchor.
52- savedAnchor = anchorStore . Load ( SavedAnchorFriendlyName , gameObject ) ;
53- if ( savedAnchor == null )
54- {
55- // Either world anchor was not saved / does not exist or has a different name.
56- Debug . Log ( gameObject . name + " : " + "World anchor could not be loaded for this game object. Creating a new anchor." ) ;
57-
58- // Create anchor since one does not exist.
59- CreateAnchor ( ) ;
60- }
61- else
62- {
63- Debug . Log ( gameObject . name + " : " + "World anchor loaded from anchor store and updated for this game object." ) ;
64- }
65- }
66-
67- // Called by GazeGestureManager when the user performs a tap gesture.
68- void OnSelect ( )
40+ private void Start ( )
6941 {
70- if ( SpatialMappingManager . Instance != null )
42+ // Make sure we have all the components in the scene we need.
43+ anchorManager = WorldAnchorManager . Instance ;
44+ if ( anchorManager == null )
7145 {
72- // On each tap gesture, toggle whether the user is in placing mode.
73- placing = ! placing ;
74-
75- // If the user is in placing mode, display the spatial mapping mesh.
76- if ( placing )
77- {
78- SpatialMappingManager . Instance . DrawVisualMeshes = true ;
79-
80- Debug . Log ( gameObject . name + " : " + "Removing existing world anchor if any." ) ;
81-
82- // Remove existing world anchor when moving an object.
83- DestroyImmediate ( gameObject . GetComponent < WorldAnchor > ( ) ) ;
84-
85- // Delete existing world anchor from anchor store when moving an object.
86- if ( anchorStore != null )
87- {
88- anchorStore . Delete ( SavedAnchorFriendlyName ) ;
89- }
90- }
91- // If the user is not in placing mode, hide the spatial mapping mesh.
92- else
93- {
94- SpatialMappingManager . Instance . DrawVisualMeshes = false ;
95-
96- // Add world anchor when object placement is done.
97- CreateAnchor ( ) ;
98- }
46+ Debug . LogError ( "This script expects that you have a WorldAnchorManager component in your scene." ) ;
9947 }
100- else
101- {
102- Debug . Log ( "TapToPlace requires spatial mapping. Try adding SpatialMapping prefab to project." ) ;
103- }
104- }
105-
106- private void CreateAnchor ( )
107- {
108- // NOTE: It's good practice to ensure your parent hierarchy or root game object does not already have a World Anchor.
109- // You can handle this in a way that works best for your application.
110- // For example: gameObject.transform.root.GetComponent<WorldAnchor>();
11148
112- // Add the world anchor component when done moving an object.
113- WorldAnchor anchor = gameObject . AddComponent < WorldAnchor > ( ) ;
114- if ( anchor . isLocated )
49+ spatialMappingManager = SpatialMappingManager . Instance ;
50+ if ( spatialMappingManager == null )
11551 {
116- SaveAnchor ( anchor ) ;
52+ Debug . LogError ( "This script expects that you have a SpatialMappingManager component in your scene." ) ;
11753 }
118- else
119- {
120- anchor . OnTrackingChanged += Anchor_OnTrackingChanged ;
121- }
122- }
12354
124- private void SaveAnchor ( WorldAnchor anchor )
125- {
126- // Save the anchor to persist holograms across sessions.
127- if ( anchorStore . Save ( SavedAnchorFriendlyName , anchor ) )
55+ if ( anchorManager != null && spatialMappingManager != null )
12856 {
129- Debug . Log ( gameObject . name + " : " + "World anchor saved successfully." ) ;
57+ anchorManager . AttachAnchor ( this . gameObject , SavedAnchorFriendlyName ) ;
13058 }
13159 else
13260 {
133- Debug . LogError ( gameObject . name + " : " + "World anchor save failed." ) ;
61+ // If we don't have what we need to proceed, we may as well remove ourselves.
62+ Destroy ( this ) ;
13463 }
13564 }
13665
137- private void Anchor_OnTrackingChanged ( WorldAnchor self , bool located )
66+ // Called by GazeGestureManager when the user performs a tap gesture.
67+ public void OnSelect ( )
13868 {
139- if ( located )
69+ // On each tap gesture, toggle whether the user is in placing mode.
70+ placing = ! placing ;
71+
72+ // If the user is in placing mode, display the spatial mapping mesh.
73+ if ( placing )
14074 {
141- Debug . Log ( gameObject . name + " : " + "World anchor located successfully." ) ;
75+ spatialMappingManager . DrawVisualMeshes = true ;
76+
77+ Debug . Log ( gameObject . name + " : Removing existing world anchor if any." ) ;
14278
143- SaveAnchor ( self ) ;
144- self . OnTrackingChanged -= Anchor_OnTrackingChanged ;
79+ anchorManager . RemoveAnchor ( gameObject ) ;
14580 }
81+ // If the user is not in placing mode, hide the spatial mapping mesh.
14682 else
14783 {
148- Debug . LogError ( gameObject . name + " : " + "World anchor failed to locate." ) ;
84+ spatialMappingManager . DrawVisualMeshes = false ;
85+ // Add world anchor when object placement is done.
86+ anchorManager . AttachAnchor ( gameObject , SavedAnchorFriendlyName ) ;
14987 }
15088 }
15189
152- void Update ( )
90+ private void Update ( )
15391 {
154- // If the user is in placing mode,
155- // update the placement to match the user's gaze.
156- if ( placing )
92+ // If the user is in placing mode,
93+ // update the placement to match the user's gaze.
94+ if ( placing )
15795 {
15896 // Do a raycast into the world that will only hit the Spatial Mapping mesh.
15997 var headPosition = Camera . main . transform . position ;
16098 var gazeDirection = Camera . main . transform . forward ;
16199
162100 RaycastHit hitInfo ;
163101 if ( Physics . Raycast ( headPosition , gazeDirection , out hitInfo ,
164- 30.0f , SpatialMappingManager . Instance . LayerMask ) )
102+ 30.0f , spatialMappingManager . LayerMask ) )
165103 {
166104 // Move this object to where the raycast
167105 // hit the Spatial Mapping mesh.
0 commit comments