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 Microsoft . MixedReality . WorldLocking . Core ;
54using System . Collections ;
65using System . Collections . Generic ;
76using System . IO ;
87using UnityEngine ;
98
10- namespace Microsoft . MixedReality . WorldLocking . Examples
9+ namespace Microsoft . MixedReality . WorldLocking . Core
1110{
1211 /// <summary>
1312 /// Script to use an independent AlignmentManager to align a specific subtree, independent of the rest of the scene.
@@ -30,6 +29,20 @@ namespace Microsoft.MixedReality.WorldLocking.Examples
3029 public class AlignSubtree : MonoBehaviour
3130 {
3231 #region Inspector fields
32+
33+ [ SerializeField ]
34+ [ Tooltip ( "Collect all SpacePins from this subtree to manage." ) ]
35+ private bool collectFromTree = true ;
36+
37+ /// <summary>
38+ /// Collect all SpacePins from this subtree to manage.
39+ /// </summary>
40+ public bool CollectFromTree { get { return collectFromTree ; } set { collectFromTree = value ; } }
41+
42+ [ SerializeField ]
43+ [ Tooltip ( "Explicit list of Space Pins to manage." ) ]
44+ private List < SpacePin > ownedPins = new List < SpacePin > ( ) ;
45+
3346 [ SerializeField ]
3447 [ Tooltip ( "File name for saving to and loading from. Defaults to gameObject's name. Use forward slash '/' for subfolders." ) ]
3548 private string saveFileName = "" ;
@@ -85,6 +98,8 @@ public string SaveFileName
8598 /// </summary>
8699 private AlignmentManager alignmentManager = null ;
87100
101+ private bool needLoad = true ;
102+
88103 #endregion Internal members
89104
90105 #region Public APIs
@@ -114,6 +129,71 @@ public bool Load()
114129 }
115130 return false ;
116131 }
132+
133+ /// <summary>
134+ /// Explicitly add a pin to the owned pins list.
135+ /// </summary>
136+ /// <param name="pin">THe pin to add.</param>
137+ /// <returns>True if added, false if it was already there.</returns>
138+ public bool AddOwnedPin ( SpacePin pin )
139+ {
140+ if ( ! ownedPins . Contains ( pin ) )
141+ {
142+ ownedPins . Add ( pin ) ;
143+ return true ;
144+ }
145+ return false ;
146+ }
147+
148+ /// <summary>
149+ /// Remove a specific pin from the owned pins list.
150+ /// </summary>
151+ /// <param name="pin">The pin to remove.</param>
152+ /// <returns>True if removed, else false (probably not found).</returns>
153+ public bool RemoveOwnedPin ( SpacePin pin )
154+ {
155+ return ownedPins . Remove ( pin ) ;
156+ }
157+
158+ /// <summary>
159+ /// Clear the entire list of owned space pins.
160+ /// </summary>
161+ /// <remarks>
162+ /// This removes all pins in the list, whether added dynamically or added in the inspector.
163+ /// </remarks>
164+ public void ClearOwnedPins ( )
165+ {
166+ ownedPins . Clear ( ) ;
167+ }
168+
169+ /// <summary>
170+ /// This should be called whenever pins are added to the owned list.
171+ /// </summary>
172+ /// <remarks>
173+ /// It's only necessary to call this when adding pins to the owned list dynamically
174+ /// from script. It is called from OnEnable for all pins added in the inspector or
175+ /// collected from the scene graph subtree.
176+ /// </remarks>
177+ public void ClaimPinOwnership ( )
178+ {
179+ CheckInternalWiring ( ) ;
180+ if ( CollectFromTree )
181+ {
182+ var spacePins = GetComponentsInChildren < SpacePin > ( ) ;
183+ foreach ( var pin in spacePins )
184+ {
185+ AddOwnedPin ( pin ) ;
186+ }
187+ }
188+ foreach ( var pin in ownedPins )
189+ {
190+ pin . AlignmentManager = alignmentManager ;
191+ }
192+ if ( AutoSave )
193+ {
194+ needLoad = true ;
195+ }
196+ }
117197 #endregion Public APIs
118198
119199 #region Internal AlignmentManager management
@@ -158,6 +238,8 @@ private void Update()
158238 {
159239 Debug . Assert ( alignmentManager != null ) ;
160240
241+ CheckLoad ( ) ;
242+
161243 var wltMgr = WorldLockingManager . GetInstance ( ) ;
162244 Debug . Assert ( alignmentManager != wltMgr . AlignmentManager ) ;
163245
@@ -169,23 +251,23 @@ private void Update()
169251 subTree . SetGlobalPose ( lockedFromPinned ) ;
170252 }
171253
254+ private void CheckLoad ( )
255+ {
256+ if ( needLoad )
257+ {
258+ needLoad = false ;
259+ Load ( ) ;
260+ }
261+ }
262+
172263 /// <summary>
173264 /// Check that all internal wiring is complete. Assign our independent alignmentManager
174265 /// to all space pins beneath us.
175266 /// Load state from previous session if available and so configured.
176267 /// </summary>
177268 private void OnEnable ( )
178269 {
179- CheckInternalWiring ( ) ;
180- var spacePins = GetComponentsInChildren < SpacePin > ( ) ;
181- foreach ( var pin in spacePins )
182- {
183- pin . AlignmentManager = alignmentManager ;
184- }
185- if ( AutoSave )
186- {
187- Load ( ) ;
188- }
270+ ClaimPinOwnership ( ) ;
189271 }
190272
191273 /// <summary>
0 commit comments