AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SceneContentAdjuster.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using UnityEngine;
5 
6 #if UNITY_2017_2_OR_NEWER
7 using System.Collections;
8 using UnityEngine.XR;
9 #else
10 using UnityEngine.VR;
11 #endif
12 
13 namespace HoloToolkit.Unity.Boundary
14 {
26  public class SceneContentAdjuster : MonoBehaviour
27  {
28  private enum AlignmentType
29  {
30  AlignWithHeadHeight,
31  UsePresetPositions,
32  UsePresetXAndZWithHeadHeight
33  }
34 
35  [SerializeField]
36  [Tooltip("Optional container object reference. If null, this script will move the object it's attached to.")]
37  private Transform containerObject = null;
38 
39 #if UNITY_2017_2_OR_NEWER
40  [SerializeField]
41  [Tooltip("Select this if the container should be placed in front of the head on app launch in a room scale app.")]
42  private AlignmentType alignmentType = AlignmentType.AlignWithHeadHeight;
43 
44  [SerializeField]
45  [Tooltip("Use this to set the desired position of the container in a stationary app. This will be ignored if AlignWithHeadHeight is set.")]
46  private Vector3 stationarySpaceTypePosition = Vector3.zero;
47 
48  [SerializeField]
49  [Tooltip("Use this to set the desired position of the container in a room scale app. This will be ignored if AlignWithHeadHeight is set.")]
50  private Vector3 roomScaleSpaceTypePosition = Vector3.zero;
51 
52  private Vector3 contentPosition = Vector3.zero;
53 
54  private int frameWaitHack = 0;
55 #endif
56 
57  private void Awake()
58  {
59  if (containerObject == null)
60  {
61  containerObject = transform;
62  }
63 
64 #if UNITY_2017_2_OR_NEWER
65  // If no XR device is present, the editor will default to (0, 0, 0) and no adjustment is needed.
66  // This script runs on both opaque and transparent display devices, since the floor offset is based on
67  // TrackingSpaceType and not display type.
68  if (XRDevice.isPresent)
69  {
70  StartCoroutine(SetContentHeight());
71  return;
72  }
73 #endif
74  Destroy(this);
75  }
76 
77 #if UNITY_2017_2_OR_NEWER
78  private IEnumerator SetContentHeight()
79  {
80  if (frameWaitHack < 1)
81  {
82  // Not waiting a frame often caused the camera's position to be incorrect at this point. This seems like a Unity bug.
83  frameWaitHack++;
84  yield return null;
85  }
86 
87  if (alignmentType == AlignmentType.UsePresetPositions || alignmentType == AlignmentType.UsePresetXAndZWithHeadHeight)
88  {
89  if (XRDevice.GetTrackingSpaceType() == TrackingSpaceType.RoomScale)
90  {
91  containerObject.position = roomScaleSpaceTypePosition;
92  }
93  else if (XRDevice.GetTrackingSpaceType() == TrackingSpaceType.Stationary)
94  {
95  containerObject.position = stationarySpaceTypePosition;
96  }
97  }
98 
99  if (alignmentType == AlignmentType.AlignWithHeadHeight || alignmentType == AlignmentType.UsePresetXAndZWithHeadHeight)
100  {
101  contentPosition.x = containerObject.position.x;
102  contentPosition.y = containerObject.position.y + CameraCache.Main.transform.position.y;
103  contentPosition.z = containerObject.position.z;
104 
105  containerObject.position = contentPosition;
106  }
107  }
108 #endif
109  }
110 }
The purpose of this class is to provide a cached reference to the main camera. Calling Camera...
Definition: CameraCache.cs:12
static Camera Main
Returns a cached reference to the main camera and uses Camera.main if it hasn&#39;t been cached yet...
Definition: CameraCache.cs:20
Use this script on GameObjects you wish to be aligned in certain ways depending on the application sp...