AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
AttachToController.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 #if UNITY_WSA
6 #if UNITY_2017_2_OR_NEWER
7 using UnityEngine.XR.WSA.Input;
8 #else
9 using UnityEngine.VR.WSA.Input;
10 #endif
11 #endif
12 
13 namespace HoloToolkit.Unity.InputModule
14 {
19  {
20  public bool SetChildrenInactiveWhenDetached = true;
21 
22  [SerializeField]
23  protected Vector3 PositionOffset = Vector3.zero;
24 
25  [SerializeField]
26  protected Vector3 RotationOffset = Vector3.zero;
27 
28  [SerializeField]
29  protected Vector3 ScaleOffset = Vector3.one;
30 
31  [SerializeField]
32  protected bool SetScaleOnAttach = false;
33 
34  public bool IsAttached { get { return transform.parent == null; } }
35 
36  protected virtual void OnAttachToController() { }
37  protected virtual void OnDetachFromController() { }
38 
39  protected override void OnEnable()
40  {
41  SetChildrenActive(false);
42 
43  base.OnEnable();
44  }
45 
46  protected override void OnControllerFound()
47  {
48  // Parent ourselves under the element and set our offsets
49  transform.parent = ElementTransform;
50  transform.localPosition = PositionOffset;
51  transform.localEulerAngles = RotationOffset;
52 
53  if (SetScaleOnAttach)
54  {
55  transform.localScale = ScaleOffset;
56  }
57 
58  SetChildrenActive(true);
59 
60  // Announce that we're attached
61  OnAttachToController();
62  }
63 
64  protected override void OnControllerLost()
65  {
66  OnDetachFromController();
67 
68  SetChildrenActive(false);
69 
70  transform.parent = null;
71  }
72 
73  private void SetChildrenActive(bool isActive)
74  {
75  if (SetChildrenInactiveWhenDetached)
76  {
77  foreach (Transform child in transform)
78  {
79  child.gameObject.SetActive(isActive);
80  }
81  }
82  }
83 
84  protected void Reset()
85  {
86  // We want the default value of Handedness of Controller finders to be Unknown so it doesn't attach to random object.
87  // But we also want the Editor to start with a useful default, so we set a Left handedness on inspector reset.
88 #if UNITY_WSA && UNITY_2017_2_OR_NEWER
89  Handedness = InteractionSourceHandedness.Left;
90 #endif
91  }
92  }
93 }
Waits for a controller to be instantiated, then attaches itself to a specified element ...
override void OnControllerLost()
Override this method to act when the correct controller is actually lost. This provides similar funct...
override void OnControllerFound()
Override this method to act when the correct controller is actually found. This provides similar func...
ControllerFinder is a base class providing simple event handling for getting/releasing MotionControll...