6 #if UNITY_WSA && UNITY_2017_2_OR_NEWER 22 [Tooltip(
"The stabilizer, if any, used to smooth out controller ray data.")]
25 [Tooltip(
"The cursor, if any, which should follow the selected pointer.")]
28 [Tooltip(
"If true, search for a cursor if one isn't explicitly set.")]
30 private bool searchForCursorIfUnset =
true;
31 public bool SearchForCursorIfUnset {
get {
return searchForCursorIfUnset; }
set { searchForCursorIfUnset = value; } }
33 [Tooltip(
"If true, always select the best pointer available (OS behavior does not auto-select).")]
35 private bool autoselectBestAvailable =
false;
36 public bool AutoselectBestAvailable {
get {
return autoselectBestAvailable; }
set { autoselectBestAvailable = value; } }
38 [Tooltip(
"The line pointer prefab to use, if any.")]
40 private GameObject linePointerPrefab = null;
49 private bool pointerWasChanged;
51 private bool addedInputManagerListener;
58 #region MonoBehaviour Implementation 68 AddInputManagerListenerIfNeeded();
70 ConnectBestAvailablePointer();
73 private void OnEnable()
77 AddInputManagerListenerIfNeeded();
81 private void OnDisable()
83 RemoveInputManagerListenerIfNeeded();
88 #region Input Event Handlers 93 if (autoselectBestAvailable && SupportsPointingRay(eventData))
95 ConnectBestAvailablePointer();
101 if (IsInputSourcePointerActive && inputSourcePointer.InputIsFromSource(eventData))
103 ConnectBestAvailablePointer();
114 HandleInputAction(eventData);
121 private void AddInputManagerListenerIfNeeded()
123 if (!addedInputManagerListener)
126 addedInputManagerListener =
true;
130 private void RemoveInputManagerListenerIfNeeded()
132 if (addedInputManagerListener)
135 addedInputManagerListener =
false;
139 private void FindCursorIfNeeded()
141 if ((Cursor == null) && searchForCursorIfUnset)
143 Debug.LogWarningFormat(
145 "Cursor hasn't been explicitly set on \"{0}.{1}\". We'll search for a cursor in the hierarchy, but" 146 +
" that comes with a performance cost, so it would be best if you explicitly set the cursor.",
151 Cursor[] foundCursors = FindObjectsOfType<Cursor>();
153 if ((foundCursors == null) || (foundCursors.Length == 0))
155 Debug.LogErrorFormat(
this,
"Couldn't find cursor for \"{0}.{1}\".", name, GetType().Name);
157 else if (foundCursors.Length > 1)
159 Debug.LogErrorFormat(
161 "Found more than one ({0}) cursors for \"{1}.{2}\", so couldn't automatically set one.",
169 Cursor = foundCursors[0];
176 if (currentPointer != newPointer)
178 if (currentPointer != null)
183 currentPointer = newPointer;
185 if (newPointer != null)
196 Debug.Assert(currentPointer != null,
"No Pointer Set!");
198 if (IsGazePointerActive)
200 DetachInputSourcePointer();
204 private void ConnectBestAvailablePointer()
209 for (var i = 0; i < inputSources.Count; i++)
211 if (SupportsPointingRay(inputSources[i]))
213 AttachInputSourcePointer(inputSources[i]);
214 bestPointer = inputSourcePointer;
219 if (bestPointer == null)
224 SetPointer(bestPointer);
233 if (SupportsPointingRay(eventData))
235 if (IsInputSourcePointerActive && inputSourcePointer.InputIsFromSource(eventData))
237 pointerWasChanged =
false;
241 AttachInputSourcePointer(eventData);
242 SetPointer(inputSourcePointer);
243 pointerWasChanged =
true;
248 if (IsGazePointerActive)
250 pointerWasChanged =
false;
258 pointerWasChanged =
true;
262 if (pointerWasChanged)
282 private bool SupportsPointingRay(
IInputSource inputSource, uint sourceId)
297 private void AttachInputSourcePointer(
IInputSource inputSource, uint sourceId)
299 inputSourcePointer.InputSource = inputSource;
300 inputSourcePointer.InputSourceId = sourceId;
301 inputSourcePointer.RayStabilizer = ControllerPointerStabilizer;
302 inputSourcePointer.OwnAllInput =
false;
303 inputSourcePointer.ExtentOverride = null;
304 inputSourcePointer.PrioritizedLayerMasksOverride = null;
309 if (interactionInputSource == null)
315 if (linePointerPrefab == null)
321 if (instantiatedPointerLine == null)
323 instantiatedPointerLine = Instantiate(linePointerPrefab).GetComponent<
PointerLine>();
326 inputSourcePointer.PointerRay = instantiatedPointerLine;
331 #if UNITY_WSA && UNITY_2017_2_OR_NEWER 333 instantiatedPointerLine.ChangeHandedness((InteractionSourceHandedness)handedness);
338 private void DetachInputSourcePointer()
340 if (instantiatedPointerLine != null)
342 Destroy(instantiatedPointerLine.gameObject);
346 private bool IsInputSourcePointerActive
348 get {
return (currentPointer == inputSourcePointer); }
351 private bool IsGazePointerActive