AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
ControllerFinder.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_WSA && UNITY_2017_2_OR_NEWER
7 using UnityEngine.XR.WSA.Input;
8 #endif
9 
10 namespace HoloToolkit.Unity.InputModule
11 {
15  public abstract class ControllerFinder : MonoBehaviour
16  {
18  {
19  get { return element; }
20  set { element = value; }
21  }
22 
23  [SerializeField]
25 
26 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
27  public InteractionSourceHandedness Handedness
28  {
29  get { return handedness; }
30  set
31  {
32  // We need to refresh which controller we're attached to if we switch handedness.
33  if (handedness != value)
34  {
35  handedness = value;
36  RefreshControllerTransform();
37  }
38  }
39  }
40 
41  [SerializeField]
42  private InteractionSourceHandedness handedness = InteractionSourceHandedness.Unknown;
43 #endif
44 
45  public Transform ElementTransform { get; private set; }
46 
48 
49  protected virtual void OnEnable()
50  {
51 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
53  {
54  // The motion controller visualizer singleton could not be found.
55  return;
56  }
57 #endif
58 
59  // Look if the controller has loaded.
60  RefreshControllerTransform();
61 
62 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
63  MotionControllerVisualizer.Instance.OnControllerModelLoaded += AddControllerTransform;
64  MotionControllerVisualizer.Instance.OnControllerModelUnloaded += RemoveControllerTransform;
65 #endif
66  }
67 
68  protected virtual void OnDisable()
69  {
70 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
72  {
73  MotionControllerVisualizer.Instance.OnControllerModelLoaded -= AddControllerTransform;
74  MotionControllerVisualizer.Instance.OnControllerModelUnloaded -= RemoveControllerTransform;
75  }
76 #endif
77  }
78 
79  protected virtual void OnDestroy()
80  {
81 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
83  {
84  MotionControllerVisualizer.Instance.OnControllerModelLoaded -= AddControllerTransform;
85  MotionControllerVisualizer.Instance.OnControllerModelUnloaded -= RemoveControllerTransform;
86  }
87 #endif
88  }
89 
94 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
95  public void ChangeHandedness(InteractionSourceHandedness newHandedness)
96  {
97  if (newHandedness != handedness)
98  {
99  Handedness = newHandedness;
100  }
101  }
102 #endif
103 
107  protected virtual void TryAndAddControllerTransform()
108  {
109 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
110  // Look if the controller was already loaded. This could happen if the
111  // GameObject was instantiated at runtime and the model loaded event has already fired.
113  {
114  // The motion controller visualizer singleton could not be found.
115  return;
116  }
117 
118  MotionControllerInfo newController;
119  if (MotionControllerVisualizer.Instance.TryGetControllerModel(handedness, out newController))
120  {
121  AddControllerTransform(newController);
122  }
123 #endif
124  }
125 
126  protected virtual void AddControllerTransform(MotionControllerInfo newController)
127  {
128 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
129  if (newController.Handedness == handedness && !newController.Equals(ControllerInfo))
130  {
131  Transform elementTransform;
132  if (!newController.TryGetElement(element, out elementTransform))
133  {
134  Debug.LogError("Unable to find element of type " + element + " under controller " + newController.ControllerParent.name + "; not attaching.");
135  return;
136  }
137 
138  ControllerInfo = newController;
139  // Update ElementTransform for consumption
140  ElementTransform = elementTransform;
141 
142  OnControllerFound();
143  }
144 #endif
145  }
146 
147  protected virtual void RemoveControllerTransform(MotionControllerInfo oldController)
148  {
149 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
150  if (oldController.Handedness == handedness)
151  {
152  ResetControllerTransform();
153  }
154 #endif
155  }
156 
157  protected virtual void RefreshControllerTransform()
158  {
159 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
160  if (ControllerInfo != null)
161  {
162  ResetControllerTransform();
163  }
164 
165  TryAndAddControllerTransform();
166 #endif
167  }
168 
174  protected virtual void OnControllerFound() { }
175 
181  protected virtual void OnControllerLost() { }
182 
187  private void ResetControllerTransform()
188  {
189  OnControllerLost();
190 
191  ControllerInfo = null;
192  ElementTransform = null;
193  }
194  }
195 }
static bool ConfirmInitialized()
Awake and OnEnable safe way to check if a Singleton is initialized.
Definition: Singleton.cs:69
virtual void TryAndAddControllerTransform()
Allows the object to change which controller it tracks, based on handedness.
This script spawns a specific GameObject when a controller is detected and animates the controller po...
virtual void RemoveControllerTransform(MotionControllerInfo oldController)
This script keeps track of the GameObjects for each button on the controller. It also keeps track of ...
virtual void OnControllerLost()
Override this method to act when the correct controller is actually lost. This provides similar funct...
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
virtual void AddControllerTransform(MotionControllerInfo newController)
virtual void OnControllerFound()
Override this method to act when the correct controller is actually found. This provides similar func...
bool TryGetElement(ControllerElementEnum element, out Transform elementTransform)
ControllerFinder is a base class providing simple event handling for getting/releasing MotionControll...
static bool IsInitialized
Returns whether the instance has been initialized or not.
Definition: Singleton.cs:58