AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SwapVolume.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 UnityEngine;
6 
7 namespace HoloToolkit.Unity.Examples.Utilities
8 {
13  public class SwapVolume : MonoBehaviour, IInputClickHandler
14  {
15  [SerializeField]
16  private GameObject hideThisObject = null;
17 
18  [SerializeField]
19  private GameObject spawnThisPrefab = null;
20 
21  [SerializeField]
22  private bool updateSolverTargetToClickSource = true;
23 
24  private SolverHandler solverHandler;
25  private GameObject spawnedObject;
26 
27  private void Awake()
28  {
29  // This example script depends on both GameObjects being properly set.
30  if (hideThisObject == null || spawnThisPrefab == null)
31  {
32  Destroy(gameObject);
33  }
34  }
35 
36  private void Start()
37  {
38  spawnedObject = Instantiate(spawnThisPrefab, hideThisObject.transform.position, hideThisObject.transform.rotation);
39  spawnedObject.SetActive(false);
40  solverHandler = spawnedObject.GetComponent<SolverHandler>();
41  }
42 
43  public void OnInputClicked(InputClickedEventData eventData)
44  {
45  if (spawnedObject.activeSelf)
46  {
47  spawnedObject.SetActive(false);
48  hideThisObject.SetActive(true);
49  }
50  else
51  {
52  spawnedObject.SetActive(true);
53 
54  if (updateSolverTargetToClickSource && solverHandler != null)
55  {
56  InteractionInputSource interactionInputSource = eventData.InputSource as InteractionInputSource;
57 
58  if (interactionInputSource != null)
59  {
60  InteractionSourceInfo sourceKind;
61  if (interactionInputSource.TryGetSourceKind(eventData.SourceId, out sourceKind))
62  {
63  switch (sourceKind)
64  {
65  case InteractionSourceInfo.Controller:
66  Handedness handedness;
67  if (interactionInputSource.TryGetHandedness(eventData.SourceId, out handedness))
68  {
69  if (handedness == Handedness.Right)
70  {
71  solverHandler.TrackedObjectToReference = SolverHandler.TrackedObjectToReferenceEnum.MotionControllerRight;
72  }
73  else
74  {
75  solverHandler.TrackedObjectToReference = SolverHandler.TrackedObjectToReferenceEnum.MotionControllerLeft;
76  }
77  }
78  break;
79  default:
80  Debug.LogError("The click event came from a device that isn't tracked. Nothing to attach to! Use a controller to select an example.");
81  break;
82  }
83  }
84  }
85  }
86 
87  hideThisObject.SetActive(false);
88  }
89 
90  eventData.Use(); // Mark the event as used, so it doesn't fall through to other handlers.
91  }
92 
93  private void OnDestroy()
94  {
95  Destroy(spawnedObject);
96  Destroy(hideThisObject);
97  }
98  }
99 }
Input source for gestures and interaction source information from the WSA APIs, which gives access to...
TrackedObjectToReferenceEnum TrackedObjectToReference
uint SourceId
The id of the source the event is from, for instance the hand id.
This class is used in the SolverExamples scene, used to swap between active solvers and placeholder s...
Definition: SwapVolume.cs:13
IInputSource InputSource
The source the input event originates from.
Interface to implement to react to simple click input.
bool TryGetHandedness(uint sourceId, out Handedness handedness)
Describes an input event that involves a tap.
void OnInputClicked(InputClickedEventData eventData)
Definition: SwapVolume.cs:43
override bool TryGetSourceKind(uint sourceId, out InteractionSourceInfo sourceKind)