@@ -19,21 +19,24 @@ namespace Microsoft.MixedReality.Toolkit
1919 /// </summary>
2020 public class MixedRealitySceneContent : MonoBehaviour
2121 {
22- public enum AlignmentType
22+ private enum AlignmentType
2323 {
2424 AlignWithExperienceScale ,
2525 AlignWithHeadHeight
2626 }
2727
2828 [ SerializeField ]
2929 [ Tooltip ( "Select this if the container should be placed in front of the head on app launch in a room scale app." ) ]
30- public AlignmentType alignmentType = AlignmentType . AlignWithExperienceScale ;
31-
32- private Vector3 contentPosition = Vector3 . zero ;
30+ private AlignmentType alignmentType = AlignmentType . AlignWithExperienceScale ;
3331
32+ [ SerializeField ]
3433 [ Tooltip ( "Optional container object reference. If null, this script will move the object it's attached to." ) ]
3534 private Transform containerObject = null ;
3635
36+ private Vector3 contentPosition = Vector3 . zero ;
37+ private const uint MaxEditorFrameWaitCount = 5 ;
38+ private Coroutine initializeSceneContentWithDelay ;
39+
3740 private void Awake ( )
3841 {
3942 if ( containerObject == null )
@@ -42,14 +45,36 @@ private void Awake()
4245 }
4346
4447 // Init the content height on non-XR platforms
45- StartCoroutine ( InitializeSceneContentWithDelay ( ) ) ;
48+ initializeSceneContentWithDelay = StartCoroutine ( InitializeSceneContentWithDelay ( ) ) ;
49+ }
50+
51+ private void OnDestroy ( )
52+ {
53+ if ( initializeSceneContentWithDelay != null )
54+ {
55+ StopCoroutine ( initializeSceneContentWithDelay ) ;
56+ }
4657 }
4758
48- // Not waiting a frame often caused the camera's position to be incorrect at this point. This seems like a Unity bug.
59+ // Not waiting often caused the camera's position to be incorrect at this point. This seems like a Unity bug.
60+ // Editor takes a little longer to init.
4961 private IEnumerator InitializeSceneContentWithDelay ( )
5062 {
51- yield return null ;
63+ if ( Application . isEditor )
64+ {
65+ for ( int i = 0 ; i < MaxEditorFrameWaitCount ; i ++ )
66+ {
67+ yield return null ;
68+ }
69+ }
70+ else
71+ {
72+ yield return null ;
73+ }
74+
5275 InitializeSceneContent ( ) ;
76+
77+ initializeSceneContentWithDelay = null ;
5378 }
5479
5580
@@ -77,8 +102,8 @@ public void InitializeSceneContent()
77102#if UNITY_2020_1_OR_NEWER
78103 XRSubsystemHelpers . InputSubsystem != null && XRSubsystemHelpers . InputSubsystem . GetTrackingOriginMode ( ) . HasFlag ( TrackingOriginModeFlags . Floor ) ;
79104#elif UNITY_2019_1_OR_NEWER
80- #pragma warning disable 0618
81105 ( XRSubsystemHelpers . InputSubsystem != null && XRSubsystemHelpers . InputSubsystem . GetTrackingOriginMode ( ) . HasFlag ( TrackingOriginModeFlags . Floor ) ) ||
106+ #pragma warning disable 0618
82107 ( XRDevice . isPresent && XRDevice . GetTrackingSpaceType ( ) == TrackingSpaceType . RoomScale ) ;
83108#pragma warning restore 0618
84109#else
@@ -97,11 +122,6 @@ public void InitializeSceneContent()
97122
98123 containerObject . position = contentPosition ;
99124 }
100- else
101- {
102- contentPosition = Vector3 . zero ;
103- containerObject . position = contentPosition ;
104- }
105125 }
106126
107127 if ( alignmentType == AlignmentType . AlignWithHeadHeight )
0 commit comments