From e374f601231a8f388974a44107073f004a7cef4d Mon Sep 17 00:00:00 2001 From: Brian Kircher Date: Tue, 28 Apr 2020 11:13:46 -0700 Subject: [PATCH] Fix MapInteractionHandler script. Explicitly unregister and deregister InputSystem handlers. --- .../Common/Scripts/MapInteractionHandler.cs | 100 +++++++++++------- 1 file changed, 59 insertions(+), 41 deletions(-) diff --git a/SampleProject/Assets/Microsoft.Maps.Unity.Examples/Common/Scripts/MapInteractionHandler.cs b/SampleProject/Assets/Microsoft.Maps.Unity.Examples/Common/Scripts/MapInteractionHandler.cs index 8e92cae8..cd2a9d9d 100644 --- a/SampleProject/Assets/Microsoft.Maps.Unity.Examples/Common/Scripts/MapInteractionHandler.cs +++ b/SampleProject/Assets/Microsoft.Maps.Unity.Examples/Common/Scripts/MapInteractionHandler.cs @@ -41,52 +41,16 @@ public class MapInteractionHandler : MonoBehaviour, IMixedRealityPointerHandler, _mapRenderer = GetComponent(); } - public void OnPointerClicked(MixedRealityPointerEventData eventData) + private void OnEnable() { - } - - public void OnPointerDown(MixedRealityPointerEventData eventData) - { - if (CoreServices.InputSystem.FocusProvider.TryGetFocusDetails(eventData.Pointer, out FocusDetails focusDetails) && - focusDetails.Object == gameObject) + if (CoreServices.InputSystem != null) { - _panningPointer = eventData.Pointer; - _startingPointInLocalSpace = focusDetails.PointLocalSpace; - _startingPointInMercatorSpace = - _mapRenderer.TransformLocalPointToMercatorWithAltitude( - _startingPointInLocalSpace, - out _startingAltitudeInMeters, - out _startingMercatorScale); - _currentPointInLocalSpace = _startingPointInLocalSpace; - _startingMapCenterInMercator = _mapRenderer.Center.ToMercatorPosition(); - - eventData.Use(); + CoreServices.InputSystem.RegisterHandler>(this); + CoreServices.InputSystem.RegisterHandler(this); } } - public void OnPointerDragged(MixedRealityPointerEventData eventData) - { - if (_panningPointer == eventData.Pointer) - { - eventData.Use(); - } - } - - public void OnPointerUp(MixedRealityPointerEventData eventData) - { - _panningPointer = null; - } - - public void OnInputChanged(InputEventData eventData) - { - if (eventData.MixedRealityInputAction.Description == "Zoom Map") - { - _currentZoomValue = eventData.InputData; - eventData.Use(); - } - } - - void Update() + private void Update() { var isInteractingWithMap = _panningPointer != null; if (isInteractingWithMap && @@ -170,6 +134,60 @@ public class MapInteractionHandler : MonoBehaviour, IMixedRealityPointerHandler, } } + private void OnDisable() + { + if (CoreServices.InputSystem != null) + { + CoreServices.InputSystem.UnregisterHandler>(this); + CoreServices.InputSystem.UnregisterHandler(this); + } + } + + public void OnPointerClicked(MixedRealityPointerEventData eventData) + { + } + + public void OnPointerDown(MixedRealityPointerEventData eventData) + { + if (CoreServices.InputSystem.FocusProvider.TryGetFocusDetails(eventData.Pointer, out FocusDetails focusDetails) && + focusDetails.Object == gameObject) + { + _panningPointer = eventData.Pointer; + _startingPointInLocalSpace = focusDetails.PointLocalSpace; + _startingPointInMercatorSpace = + _mapRenderer.TransformLocalPointToMercatorWithAltitude( + _startingPointInLocalSpace, + out _startingAltitudeInMeters, + out _startingMercatorScale); + _currentPointInLocalSpace = _startingPointInLocalSpace; + _startingMapCenterInMercator = _mapRenderer.Center.ToMercatorPosition(); + + eventData.Use(); + } + } + + public void OnPointerDragged(MixedRealityPointerEventData eventData) + { + if (_panningPointer == eventData.Pointer) + { + eventData.Use(); + } + } + + public void OnPointerUp(MixedRealityPointerEventData eventData) + { + _panningPointer = null; + } + + public void OnInputChanged(InputEventData eventData) + { + if (eventData.MixedRealityInputAction.Description == "Zoom Map") + { + _currentZoomValue = eventData.InputData; + eventData.Use(); + } + } + private static Vector3 DynamicExpDecay(Vector3 from, Vector3 to, float halfLife) { return Vector3.Lerp(from, to, DynamicExpCoefficient(halfLife, Vector3.Distance(to, from)));