Skip to content

Commit f7fc373

Browse files
Stephen HodgsonStephen Hodgson
authored andcommitted
Cached GestureManager in GestureManipulator. Fixed minor formatting.
1 parent ea24458 commit f7fc373

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

Assets/HoloToolkit/Input/Scripts/GestureManipulator.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT License. See LICENSE in the project root for license information.
33

4-
using HoloToolkit.Unity;
54
using UnityEngine;
65

76
namespace HoloToolkit.Unity
@@ -25,49 +24,58 @@ public class GestureManipulator : MonoBehaviour
2524
public Vector3 handPositionScale = new Vector3(2.0f, 2.0f, 4.0f); // Default tuning values, expected to be modified per application
2625

2726
private Vector3 initialHandPosition;
27+
2828
private Vector3 initialObjectPosition;
2929

3030
private Interpolator targetInterpolator;
3131

32+
private GestureManager gestureManager;
33+
3234
private bool Manipulating { get; set; }
3335

34-
private void OnEnable()
36+
private void Start()
3537
{
36-
if (GestureManager.Instance != null)
38+
gestureManager = GestureManager.Instance;
39+
40+
if (gestureManager == null)
3741
{
38-
GestureManager.Instance.ManipulationStarted += BeginManipulation;
39-
GestureManager.Instance.ManipulationCompleted += EndManipulation;
40-
GestureManager.Instance.ManipulationCanceled += EndManipulation;
42+
Debug.LogError(string.Format("GestureManipulator on {0} could not find GestureManager instance, manipulation will not function", name));
4143
}
42-
else
44+
}
45+
46+
private void OnEnable()
47+
{
48+
if (gestureManager != null)
4349
{
44-
Debug.LogError(string.Format("GestureManipulator enabled on {0} could not find GestureManager instance, manipulation will not function", name));
50+
gestureManager.ManipulationStarted += BeginManipulation;
51+
gestureManager.ManipulationCompleted += EndManipulation;
52+
gestureManager.ManipulationCanceled += EndManipulation;
4553
}
4654
}
4755

4856
private void OnDisable()
4957
{
50-
if (GestureManager.Instance)
58+
if (gestureManager != null)
5159
{
52-
GestureManager.Instance.ManipulationStarted -= BeginManipulation;
53-
GestureManager.Instance.ManipulationCompleted -= EndManipulation;
54-
GestureManager.Instance.ManipulationCanceled -= EndManipulation;
60+
gestureManager.ManipulationStarted -= BeginManipulation;
61+
gestureManager.ManipulationCompleted -= EndManipulation;
62+
gestureManager.ManipulationCanceled -= EndManipulation;
5563
}
5664

5765
Manipulating = false;
5866
}
5967

6068
private void BeginManipulation()
6169
{
62-
if (GestureManager.Instance != null && GestureManager.Instance.ManipulationInProgress)
70+
if (gestureManager != null && gestureManager.ManipulationInProgress)
6371
{
6472
Manipulating = true;
6573

6674
targetInterpolator = gameObject.GetComponent<Interpolator>();
6775

6876
// In order to ensure that any manipulated objects move with the user, we do all our math relative to the camera,
6977
// so when we save the initial hand position and object position we first transform them into the camera's coordinate space
70-
initialHandPosition = Camera.main.transform.InverseTransformPoint(GestureManager.Instance.ManipulationHandPosition);
78+
initialHandPosition = Camera.main.transform.InverseTransformPoint(gestureManager.ManipulationHandPosition);
7179
initialObjectPosition = Camera.main.transform.InverseTransformPoint(transform.position);
7280
}
7381
}
@@ -79,12 +87,12 @@ private void EndManipulation()
7987

8088

8189
// Update is called once per frame
82-
void Update()
90+
private void Update()
8391
{
8492
if (Manipulating)
8593
{
8694
// First step is to figure out the delta between the initial hand position and the current hand position
87-
Vector3 localHandPosition = Camera.main.transform.InverseTransformPoint(GestureManager.Instance.ManipulationHandPosition);
95+
Vector3 localHandPosition = Camera.main.transform.InverseTransformPoint(gestureManager.ManipulationHandPosition);
8896
Vector3 initialHandToCurrentHand = localHandPosition - initialHandPosition;
8997

9098
// When performing a manipulation gesture, the hand generally only translates a relatively small amount.

0 commit comments

Comments
 (0)