AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
BaseUsable.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 abstract class BaseUsable : MonoBehaviour
17  {
18 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
19  [SerializeField]
20  private InteractionSourceHandedness handedness = InteractionSourceHandedness.Unknown;
21 
22  private UseStateEnum state;
23 
24 
25  public UseStateEnum UseState
26  {
27  get { return state; }
28  }
29 #endif
30 
31  protected virtual void UseStart()
32  {
33  //Child do something
34  }
35 
36  protected virtual void UseEnd()
37  {
38  //Child do something
39  }
40 
41  //Subscribe GrabStart and GrabEnd to InputEvents for Grip
42 
43  protected virtual void OnEnable()
44  {
45 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
46  InteractionManager.InteractionSourcePressed += UseInputStart;
47  InteractionManager.InteractionSourceReleased += UseInputEnd;
48 #endif
49  }
50 
51  protected virtual void OnDisable()
52  {
53 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
54  InteractionManager.InteractionSourcePressed -= UseInputStart;
55  InteractionManager.InteractionSourceReleased -= UseInputEnd;
56 #endif
57  }
58 
59 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
60  private void UseInputStart(InteractionSourcePressedEventArgs obj)
61  {
62  if (handedness == InteractionSourceHandedness.Unknown || handedness == obj.state.source.handedness)
63  {
64 
65  if (GetComponent<BaseGrabbable>().GrabState == GrabStateEnum.Single)
66  {
67  state = UseStateEnum.Active;
68  UseStart();
69  }
70  }
71  }
72 
73  private void UseInputEnd(InteractionSourceReleasedEventArgs obj)
74  {
75  if (obj.state.source.handedness == handedness)
76  {
77  state = UseStateEnum.Inactive;
78  UseEnd();
79  }
80  }
81 #endif
82  }
83 }
A usable object is one that can be "used" or activated while being grabbed/carried A gun and a remote...
Definition: BaseUsable.cs:16