AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SceneSettingsWindow.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 
6 using UnityEditor;
7 using UnityEditor.SceneManagement;
8 using UnityEngine;
9 using UnityEngine.EventSystems;
10 using UnityEngine.SceneManagement;
12 
13 namespace HoloToolkit.Unity
14 {
18  public class SceneSettingsWindow : AutoConfigureWindow<SceneSettingsWindow.SceneSetting>
19  {
24  private const string CameraPrefabGUID = "d29bc40b7f3df26479d6a0aac211c355";
25 
30  private const string InputSystemPrefabGUID = "3eddd1c29199313478dd3f912bfab2ab";
31 
36  private const string DefaultCursorPrefabGUID = "a611e772ef8ddf64d8106a9cbb70f31c";
37 
42  private const string SpatialMappingPrefabGUID = "2ed75ffdf9031c94e8bce4b3d17b9928";
43 
44  #region Nested Types
45 
46  public enum SceneSetting
47  {
48  AddMixedRealityCamera,
49  CameraToOrigin,
50  AddInputSystem,
51  AddDefaultCursor,
52  UpdateCanvases,
53  AddSpatialMapping
54  }
55 
56  #endregion // Nested Types
57 
58  #region Overrides / Event Handlers
59 
60  protected override void ApplySettings()
61  {
62  if (Values[SceneSetting.AddMixedRealityCamera])
63  {
64  if (CameraCache.Main != null)
65  {
66  DestroyImmediate(CameraCache.Main.gameObject.GetParentRoot());
67  }
68 
69  PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(CameraPrefabGUID)));
70  }
71 
72  if (Values[SceneSetting.CameraToOrigin])
73  {
74 
75  var mainCamera = CameraCache.Refresh(Camera.main);
76 
77  if (mainCamera == null)
78  {
79  Debug.LogWarning("Could not find a valid \"MainCamera\"! Unable to update camera position.");
80  }
81  else
82  {
83  mainCamera.transform.position = Vector3.zero;
84  }
85  }
86 
87  if (Values[SceneSetting.AddInputSystem])
88  {
89  var inputManager = FindObjectOfType<InputManager>();
90  if (inputManager != null)
91  {
92  DestroyImmediate(inputManager.gameObject);
93  }
94 
95  var eventSystems = FindObjectsOfType<EventSystem>();
96  foreach (var eventSystem in eventSystems)
97  {
98  DestroyImmediate(eventSystem.gameObject);
99  }
100 
101  var inputModules = FindObjectsOfType<StandaloneInputModule>();
102  foreach (var inputModule in inputModules)
103  {
104  DestroyImmediate(inputModule.gameObject);
105  }
106 
107  PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(InputSystemPrefabGUID)));
108  Values[SceneSetting.UpdateCanvases] = true;
109  }
110 
111  if (Values[SceneSetting.UpdateCanvases])
112  {
113  var focusManager = FindObjectOfType<FocusManager>();
114  if (focusManager != null)
115  {
116  FocusManager.Instance.UpdateCanvasEventSystems();
117  }
118 
119  var sceneCanvases = Resources.FindObjectsOfTypeAll<Canvas>();
120  foreach (Canvas canvas in sceneCanvases)
121  {
122  var helper = canvas.EnsureComponent<CanvasHelper>();
123  helper.Canvas = canvas;
124  }
125  }
126 
127  if (Values[SceneSetting.AddDefaultCursor])
128  {
129  var cursors = FindObjectsOfType<Cursor>();
130  foreach (var cursor in cursors)
131  {
132  DestroyImmediate(cursor.gameObject.GetParentRoot());
133  }
134 
135  PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(DefaultCursorPrefabGUID)));
136 
137  FindObjectOfType<InputManager>().GetComponent<SimpleSinglePointerSelector>().Cursor = FindObjectOfType<Cursor>();
138  }
139 
140  if (Values[SceneSetting.AddSpatialMapping])
141  {
142  var spatialMappingManagers = FindObjectsOfType<SpatialMappingManager>();
143  if (spatialMappingManagers.Length > 0)
144  {
145  Debug.LogWarning("There's already a SpatialMappingManager in the scene. Did not add the SpatialMapping prefab.");
146  }
147  else
148  {
149  PrefabUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath<GameObject>(AssetDatabase.GUIDToAssetPath(SpatialMappingPrefabGUID)));
150  }
151  }
152 
153  EditorSceneManager.MarkSceneDirty(SceneManager.GetActiveScene());
154 
155  Close();
156  }
157 
158  protected override void LoadSettings()
159  {
160  for (int i = 0; i <= (int)SceneSetting.UpdateCanvases; i++)
161  {
162  Values[(SceneSetting)i] = true;
163  }
164 
165  Values[SceneSetting.AddSpatialMapping] = false;
166  }
167 
168  protected override void OnGuiChanged()
169  {
170  }
171 
172  protected override void LoadStrings()
173  {
174  Names[SceneSetting.AddMixedRealityCamera] = "Add the Mixed Reality Camera Prefab";
175  Descriptions[SceneSetting.AddMixedRealityCamera] =
176  "Recommended\n\n" +
177  "Adds the Mixed Reality Camera Prefab to the scene.\n\n" +
178  "The prefab comes preset with all the components and options for automatically handling Occluded and Transparent Mixed Reality Applications.";
179 
180  Names[SceneSetting.CameraToOrigin] = "Move Camera to Origin";
181  Descriptions[SceneSetting.CameraToOrigin] =
182  "Recommended\n\n" +
183  "Moves the main camera to the world origin of the scene (0, 0, 0).\n\n" +
184  "<color=#ffff00ff><b>Note:</b></color> When a Mixed Reality application starts, the users head is the center of the world. By not having your Main Camera centered at " +
185  "the world origin (0, 0, 0) will result in GameObjects not appearing where they are expected. This option should remain checked unless you have alternative methods " +
186  "that explicitly deal with any apparent offset.";
187 
188  Names[SceneSetting.AddInputSystem] = "Add the Input Manager Prefab";
189  Descriptions[SceneSetting.AddInputSystem] =
190  "Recommended\n\n" +
191  "Adds the Input Manager Prefab to the scene.\n\n" +
192  "The prefab comes preset with all the components and options for automatically handling input for Mixed Reality Applications.\n\n" +
193  "<color=#ff0000ff><b>Warning!</b></color> This will remove and replace any currently existing Input Managers or Event Systems in your scene.";
194 
195  Names[SceneSetting.AddDefaultCursor] = "Add the Default Cursor Prefab";
196  Descriptions[SceneSetting.AddDefaultCursor] =
197  "Recommended\n\n" +
198  "Adds the Default Cursor Prefab to the scene.\n\n" +
199  "The prefab comes preset with all the components and options for automatically handling cursor animations for Mixed Reality Applications.\n\n" +
200  "<color=#ff0000ff><b>Warning!</b></color> This will remove and replace any currently existing Cursors in your scene.";
201 
202  Names[SceneSetting.UpdateCanvases] = "Update World Space Canvases";
203  Descriptions[SceneSetting.UpdateCanvases] =
204  "Recommended\n\n" +
205  "Updates all the World Space Canvases in the scene to use the Focus Managers UIRaycastCamera as its default event camera.\n\n" +
206  "<color=#ffff00ff><b>Note:</b></color> This also adds a CanvasHelper script to the canvas to aid in the scene transitions and instances where the camera does not " +
207  "initially exist in the same scene as the canvas.";
208 
209  Names[SceneSetting.AddSpatialMapping] = "Add the Spatial Mapping Prefab";
210  Descriptions[SceneSetting.AddSpatialMapping] =
211  "Adds the Spatial Mapping Prefab to the scene.\n\n" +
212  "The prefab comes preset with the components and options for bringing spatial mapping into your HoloLens application.\n\n" +
213  "<color=#ff0000ff><b>Warning!</b></color> This will remove and replace any currently existing Spatial Mapping Managers in your scene.";
214  }
215 
216  protected override void OnEnable()
217  {
218  base.OnEnable();
219 
220  minSize = new Vector2(350, 250);
221  maxSize = minSize;
222  }
223  #endregion // Overrides / Event Handlers
224  }
225 }
override void OnGuiChanged()
Called when a toggle has been flipped and a change has been detected.
override void LoadSettings()
Called when settings should be loaded.
Renders the UI and handles update logic for MixedRealityToolkit/Configure/Apply Mixed Reality Scene S...
Canvas Canvas
The canvas this helper script is targeting.
Definition: CanvasHelper.cs:18
static Camera Refresh(Camera newMain)
Set the cached camera to a new reference and return it
Definition: CameraCache.cs:35
The purpose of this class is to provide a cached reference to the main camera. Calling Camera...
Definition: CameraCache.cs:12
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
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
Helper class for setting up canvases for use in the MRTK.
Definition: CanvasHelper.cs:13
Object that represents a cursor in 3D space controlled by gaze.
Definition: Cursor.cs:11
Focus manager is the bridge that handles different types of pointing sources like gaze cursor or poin...
Definition: FocusManager.cs:16
Base class for auto configuration build windows.
override void ApplySettings()
Called when settings should be applied.
override void LoadStrings()
Called when string names and descriptions should be loaded.