11// Copyright (c) Microsoft Corporation. All rights reserved.
22// Licensed under the MIT License. See LICENSE in the project root for license information.
33
4+ using System ;
45using System . Collections . Generic ;
56using UnityEngine ;
67using UnityEngine . VR . WSA . Persistence ;
@@ -24,6 +25,13 @@ private struct AnchorAttachmentInfo
2425 {
2526 public GameObject GameObjectToAnchor { get ; set ; }
2627 public string AnchorName { get ; set ; }
28+ public AnchorOperation Operation { get ; set ; }
29+ }
30+
31+ private enum AnchorOperation
32+ {
33+ Create ,
34+ Delete
2735 }
2836
2937 /// <summary>
@@ -93,7 +101,8 @@ public void AttachAnchor(GameObject gameObjectToAnchor, string anchorName)
93101 new AnchorAttachmentInfo
94102 {
95103 GameObjectToAnchor = gameObjectToAnchor ,
96- AnchorName = anchorName
104+ AnchorName = anchorName ,
105+ Operation = AnchorOperation . Create
97106 }
98107 ) ;
99108 }
@@ -105,20 +114,26 @@ public void AttachAnchor(GameObject gameObjectToAnchor, string anchorName)
105114 /// <param name="gameObjectToUnanchor">gameObject to remove the anchor from.</param>
106115 public void RemoveAnchor ( GameObject gameObjectToUnanchor )
107116 {
117+ if ( gameObjectToUnanchor == null )
118+ {
119+ Debug . LogError ( "Invalid GameObject" ) ;
120+ return ;
121+ }
122+
108123 // This case is unexpected, but just in case.
109124 if ( AnchorStore == null )
110125 {
111126 Debug . LogError ( "remove anchor called before anchor store is ready." ) ;
112127 return ;
113128 }
114129
115- var anchor = gameObjectToUnanchor . GetComponent < WorldAnchor > ( ) ;
116-
117- if ( anchor != null )
118- {
119- AnchorStore . Delete ( anchor . name ) ;
120- DestroyImmediate ( anchor ) ;
121- }
130+ anchorOperations . Enqueue (
131+ new AnchorAttachmentInfo
132+ {
133+ GameObjectToAnchor = gameObjectToUnanchor ,
134+ AnchorName = string . Empty ,
135+ Operation = AnchorOperation . Delete
136+ } ) ;
122137 }
123138
124139 /// <summary>
@@ -127,29 +142,56 @@ public void RemoveAnchor(GameObject gameObjectToUnanchor)
127142 /// <param name="anchorAttachmentInfo">Parameters for attaching the anchor.</param>
128143 private void DoAnchorOperation ( AnchorAttachmentInfo anchorAttachmentInfo )
129144 {
130- string anchorName = anchorAttachmentInfo . AnchorName ;
131- GameObject gameObjectToAnchor = anchorAttachmentInfo . GameObjectToAnchor ;
132-
133- if ( gameObjectToAnchor == null )
145+ switch ( anchorAttachmentInfo . Operation )
134146 {
135- Debug . Log ( "GameObject must have been destroyed before we got a chance to anchor it." ) ;
136- return ;
137- }
147+ case AnchorOperation . Create :
148+ string anchorName = anchorAttachmentInfo . AnchorName ;
149+ GameObject gameObjectToAnchor = anchorAttachmentInfo . GameObjectToAnchor ;
138150
139- // Try to load a previously saved world anchor.
140- WorldAnchor savedAnchor = AnchorStore . Load ( anchorName , gameObjectToAnchor ) ;
141- if ( savedAnchor == null )
142- {
143- // Either world anchor was not saved / does not exist or has a different name.
144- Debug . Log ( gameObjectToAnchor . name + " : World anchor could not be loaded for this game object. Creating a new anchor." ) ;
151+ if ( gameObjectToAnchor == null )
152+ {
153+ Debug . LogError ( "GameObject must have been destroyed before we got a chance to anchor it." ) ;
154+ break ;
155+ }
145156
146- // Create anchor since one does not exist.
147- CreateAnchor ( gameObjectToAnchor , anchorName ) ;
148- }
149- else
150- {
151- savedAnchor . name = anchorName ;
152- Debug . Log ( gameObjectToAnchor . name + " : World anchor loaded from anchor store and updated for this game object." ) ;
157+ // Try to load a previously saved world anchor.
158+ WorldAnchor savedAnchor = AnchorStore . Load ( anchorName , gameObjectToAnchor ) ;
159+ if ( savedAnchor == null )
160+ {
161+ // Either world anchor was not saved / does not exist or has a different name.
162+ Debug . LogWarning ( gameObjectToAnchor . name + " : World anchor could not be loaded for this game object. Creating a new anchor." ) ;
163+
164+ // Create anchor since one does not exist.
165+ CreateAnchor ( gameObjectToAnchor , anchorName ) ;
166+ }
167+ else
168+ {
169+ savedAnchor . name = anchorName ;
170+ Debug . Log ( gameObjectToAnchor . name + " : World anchor loaded from anchor store and updated for this game object." ) ;
171+ }
172+
173+ break ;
174+ case AnchorOperation . Delete :
175+ if ( AnchorStore == null )
176+ {
177+ Debug . LogError ( "Remove anchor called before anchor store is ready." ) ;
178+ break ;
179+ }
180+
181+ GameObject gameObjectToUnanchor = anchorAttachmentInfo . GameObjectToAnchor ;
182+ var anchor = gameObjectToUnanchor . GetComponent < WorldAnchor > ( ) ;
183+
184+ if ( anchor != null )
185+ {
186+ AnchorStore . Delete ( anchor . name ) ;
187+ DestroyImmediate ( anchor ) ;
188+ }
189+ else
190+ {
191+ Debug . LogError ( "Cannot get anchor while deleting" ) ;
192+ }
193+
194+ break ;
153195 }
154196 }
155197
0 commit comments