AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
Grabber.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.Examples.Grabbables
11 {
16  public class Grabber : BaseGrabber
17  {
18  [SerializeField]
19  private LayerMask grabbableLayers = ~0;
20 
21 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
22  [SerializeField]
23  private InteractionSourcePressType pressType = InteractionSourcePressType.None;
24 #endif
25 
27  protected override void OnEnable()
28  {
29  base.OnEnable();
30 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
31  InteractionManager.InteractionSourcePressed += InteractionSourcePressed;
32  InteractionManager.InteractionSourceReleased += InteractionSourceReleased;
33 #endif
34  }
35 
36  protected override void OnDisable()
37  {
38 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
39  InteractionManager.InteractionSourcePressed -= InteractionSourcePressed;
40  InteractionManager.InteractionSourceReleased -= InteractionSourceReleased;
41 #endif
42  base.OnDisable();
43  }
44 
45 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
46  private void InteractionSourcePressed(InteractionSourcePressedEventArgs obj)
47  {
48  if (obj.pressType == pressType && obj.state.source.handedness == handedness)
49  {
50  GrabStart();
51  }
52  }
53 
54  private void InteractionSourceReleased(InteractionSourceReleasedEventArgs obj)
55  {
56  if (obj.pressType == pressType && obj.state.source.handedness == handedness)
57  {
58  TrySetThrowableObject(GrabbedObjects.Count > 0 ? GrabbedObjects[0] : null, obj.state.sourcePose);
59  GrabEnd();
60  }
61  }
62 #endif
63 
68  protected virtual void OnTriggerEnter(Collider other)
69  {
70  Debug.Log("Entered trigger with " + other.name);
71  if (((1 << other.gameObject.layer) & grabbableLayers.value) == 0)
72  {
73  return;
74  }
75 
76  BaseGrabbable bg = other.GetComponent<BaseGrabbable>();
77  if (bg == null && other.attachedRigidbody != null)
78  {
79  bg = other.attachedRigidbody.GetComponent<BaseGrabbable>();
80  }
81 
82  if (bg == null)
83  {
84  return;
85  }
86 
87  Debug.Log("Adding contact");
88 
89  AddContact(bg);
90  }
91 
92  protected virtual void OnTriggerExit(Collider other)
93  {
94  Debug.Log("Exited trigger with " + other.name);
95  if (((1 << other.gameObject.layer) & grabbableLayers.value) == 0)
96  {
97  return;
98  }
99 
100  BaseGrabbable bg = other.GetComponent<BaseGrabbable>();
101  if (bg == null && other.attachedRigidbody != null)
102  {
103  bg = other.attachedRigidbody.GetComponent<BaseGrabbable>();
104  }
105 
106  if (bg == null)
107  {
108  return;
109  }
110 
111  Debug.Log("Removing contact");
112 
113  RemoveContact(bg);
114  }
115 
116 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
117  public bool TrySetThrowableObject(BaseGrabbable grabbable, InteractionSourcePose poseInfo)
118  {
119  if (grabbable == null)
120  {
121  return false;
122  }
123 
124  if (!grabbable.GetComponent<BaseThrowable>())
125  {
126  return false;
127  }
128 
129  if (!grabbable.GetComponent<Rigidbody>())
130  {
131  return false;
132  }
133 
134  Rigidbody rb = grabbable.GetComponent<Rigidbody>();
135  Debug.Log("name of our rb.center of mass ========= " + rb.name);
136  ControllerReleaseData controlReleaseData = grabbable.GetComponent<Rigidbody>().GetThrowReleasedVelocityAndAngularVelocity(rb.centerOfMass, poseInfo);
137 
138  //grabbable.GetComponent<BaseThrowable>().LatestControllerThrowVelocity = vel;
139  //grabbable.GetComponent<BaseThrowable>().LatestControllerThrowAngularVelocity = vel;
140 
141  grabbable.GetComponent<BaseThrowable>().LatestControllerThrowVelocity = controlReleaseData.Velocity;
142  grabbable.GetComponent<BaseThrowable>().LatestControllerThrowAngularVelocity = controlReleaseData.AngleVelocity;
143  return true;
144  }
145 #endif
146  }
147 }
The abstract class that defines the minimum amount of content for any throwable object Variables decl...
Intended usage: scripts that inherit from this can be attached to the controller, or any object with ...
Definition: BaseGrabber.cs:18
virtual void OnTriggerEnter(Collider other)
Controller grabbers find available grabbable objects via triggers
Definition: Grabber.cs:68
//Intended Usage// Attach a "grabbable_x" script (a script that inherits from this) to any object tha...
override void OnEnable()
Subscribe GrabStart and GrabEnd to InputEvents for GripPressed.
Definition: Grabber.cs:27
Extends its behaviour from BaseGrabber. This is non-abstract script that&#39;s actually attached to the g...
Definition: Grabber.cs:16