AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
DropCube.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 HoloToolkit.Unity;
5 using UnityEngine;
6 
7 #if UNITY_WSA
8 #if UNITY_2017_2_OR_NEWER
9 using UnityEngine.XR.WSA.Input;
10 #else
11 using UnityEngine.VR.WSA.Input;
12 #endif
13 #endif
14 
15 namespace HoloToolkit.Examples.SpatialMappingComponent
16 {
20  public class DropCube : MonoBehaviour
21  {
22 #if UNITY_WSA
23  private GestureRecognizer recognizer;
24 
25  private void Start()
26  {
27  recognizer = new GestureRecognizer();
28  recognizer.SetRecognizableGestures(GestureSettings.Tap);
29 #if UNITY_2017_2_OR_NEWER
30  recognizer.Tapped += Recognizer_Tapped;
31 #else
32  recognizer.TappedEvent += Recognizer_Tapped;
33 #endif
34  recognizer.StartCapturingGestures();
35  }
36 
37  private void OnDestroy()
38  {
39 #if UNITY_2017_2_OR_NEWER
40  recognizer.Tapped -= Recognizer_Tapped;
41 #else
42  recognizer.TappedEvent -= Recognizer_Tapped;
43 #endif
44  }
45 
46  private void Recognizer_Tapped(
47 #if UNITY_2017_2_OR_NEWER
48  TappedEventArgs obj
49 #else
50  InteractionSourceKind source, int tapCount, Ray headRay
51 #endif
52  )
53  {
54  // Create a cube
55  var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
56  // Make the cube smaller
57  cube.transform.localScale = Vector3.one * 0.3f;
58  // Start to drop it in front of the camera
59  cube.transform.position = CameraCache.Main.transform.position + CameraCache.Main.transform.forward;
60  // Apply physics
61  cube.AddComponent<Rigidbody>();
62  }
63 #endif
64  }
65 }
The purpose of this class is to provide a cached reference to the main camera. Calling Camera...
Definition: CameraCache.cs:12
Simple test script for dropping cubes with physics to observe interactions
Definition: DropCube.cs:20
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