AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
CanvasEditorExtension.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 
5 using UnityEditor;
6 using UnityEngine;
7 
8 namespace HoloToolkit.Unity
9 {
13  [CustomEditor(typeof(Canvas))]
14  public class CanvasEditorExtension : Editor
15  {
16  private const string DialogText = "Hi there, we noticed that you've changed this canvas to use WorldSpace.\n\n" +
17  "In order for the InputManager to work properly with uGUI raycasting we'd like to update this canvas' " +
18  "WorldCamera to use the FocusManager's UIRaycastCamera.\n";
19 
20  private Canvas canvas;
21 
22  private void OnEnable()
23  {
24  canvas = (Canvas)target;
25  }
26 
27  public override void OnInspectorGUI()
28  {
29  EditorGUI.BeginChangeCheck();
30  base.OnInspectorGUI();
31 
32  // We will only ask if we have a focus manager in our scene.
33  if (EditorGUI.EndChangeCheck() && FocusManager.Instance)
34  {
36  bool removeHelper = false;
37 
38  // Update the world camera if we need to.
39  if (canvas.isRootCanvas && canvas.renderMode == RenderMode.WorldSpace && canvas.worldCamera != FocusManager.Instance.UIRaycastCamera)
40  {
41  if (EditorUtility.DisplayDialog("Attention!", DialogText, "OK", "Cancel"))
42  {
43  canvas.worldCamera = FocusManager.Instance.UIRaycastCamera;
44  }
45  else
46  {
47  removeHelper = true;
48  }
49  }
50 
51  // Add the Canvas Helper if we need it.
52  if (canvas.isRootCanvas && canvas.renderMode == RenderMode.WorldSpace && canvas.worldCamera == FocusManager.Instance.UIRaycastCamera)
53  {
54  var helper = canvas.gameObject.EnsureComponent<CanvasHelper>();
55  helper.Canvas = canvas;
56  }
57 
58  // Reset the world canvas if we need to.
59  if (canvas.isRootCanvas && canvas.renderMode != RenderMode.WorldSpace && canvas.worldCamera == FocusManager.Instance.UIRaycastCamera)
60  {
61  // Sets it back to MainCamera default.
62  canvas.worldCamera = null;
63  removeHelper = true;
64  }
65 
66  // Remove the helper if we don't need it.
67  if (removeHelper)
68  {
69  // Remove the helper if needed.
70  var helper = canvas.GetComponent<CanvasHelper>();
71  if (helper != null)
72  {
73  DestroyImmediate(helper);
74  }
75  }
76  }
77  }
78  }
79 }
Canvas Canvas
The canvas this helper script is targeting.
Definition: CanvasHelper.cs:18
static void AssertIsInitialized()
Definition: Singleton.cs:49
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
Helper class to assign the UIRaycastCamera when creating a new canvas object and assigning the world ...
Helper class for setting up canvases for use in the MRTK.
Definition: CanvasHelper.cs:13
Focus manager is the bridge that handles different types of pointing sources like gaze cursor or poin...
Definition: FocusManager.cs:16