From 668a3d2216c7ceb1a71aaf0b6f8b6a17ae26862d Mon Sep 17 00:00:00 2001 From: Travis Milne Date: Thu, 12 Mar 2020 14:56:02 -0400 Subject: [PATCH 1/2] FIX: Fixed interaction with dropdown UI --- .../Plugins/UI/InputSystemUIInputModule.cs | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs index 8f7ea56c4f..299fef48cf 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs @@ -157,19 +157,36 @@ private RaycastResult PerformRaycast(ExtendedPointerEventData eventData) // the first shot. if (eventData.pointerType == UIPointerType.Tracked && TrackedDeviceRaycaster.s_Instances.length > 0) { + //Store the closest result + RaycastResult raycastResult = default; + //Store the closest graphic's distance + float distance = float.MaxValue; + //The current TrackedDeviceRaycaster we are looking at + TrackedDeviceRaycaster trackedDeviceRaycaster; for (var i = 0; i < TrackedDeviceRaycaster.s_Instances.length; ++i) { - var trackedDeviceRaycaster = TrackedDeviceRaycaster.s_Instances[i]; + trackedDeviceRaycaster = TrackedDeviceRaycaster.s_Instances[i]; m_RaycastResultCache.Clear(); trackedDeviceRaycaster.PerformRaycast(eventData, m_RaycastResultCache); if (m_RaycastResultCache.Count > 0) { - var raycastResult = m_RaycastResultCache[0]; - m_RaycastResultCache.Clear(); - return raycastResult; + //Iterate through the list of result to determine the closest hit graphic + foreach (RaycastResult raycast in m_RaycastResultCache) + { + if (raycast.distance < distance) + { + //Update the closest distance + distance = raycast.distance; + //Store the result + raycastResult = raycast; + } + } } } - return default; + //Clear the cache as we are done with it + m_RaycastResultCache.Clear(); + //Return the closest result or default if none exist + return raycastResult; } // Otherwise pass it along to the normal raycasting logic. From 66c42ff5009a798c693e2181f1cfbf81c9805364 Mon Sep 17 00:00:00 2001 From: Travis Milne Date: Thu, 12 Mar 2020 16:44:19 -0400 Subject: [PATCH 2/2] Added additional check to determine canvas sorting order of hit objects, this covers the case of overlapping UI elements --- .../InputSystem/Plugins/UI/InputSystemUIInputModule.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs index 299fef48cf..c7b0fcc747 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModule.cs @@ -163,6 +163,10 @@ private RaycastResult PerformRaycast(ExtendedPointerEventData eventData) float distance = float.MaxValue; //The current TrackedDeviceRaycaster we are looking at TrackedDeviceRaycaster trackedDeviceRaycaster; + //Owning canvas of the new comparison + Canvas raycastCanvas; + //Owning canvas of current result + Canvas raycastResultCanvas; for (var i = 0; i < TrackedDeviceRaycaster.s_Instances.length; ++i) { trackedDeviceRaycaster = TrackedDeviceRaycaster.s_Instances[i]; @@ -173,7 +177,10 @@ private RaycastResult PerformRaycast(ExtendedPointerEventData eventData) //Iterate through the list of result to determine the closest hit graphic foreach (RaycastResult raycast in m_RaycastResultCache) { - if (raycast.distance < distance) + raycastCanvas = raycast.gameObject.GetComponentInParent(); + raycastResultCanvas = raycastResult.gameObject?.GetComponentInParent(); + //Update result if the the new raycast's owning canvas is of a higher sorting order OR if the sorting order is the same and the new raycast is closer + if (raycastResultCanvas == null || (raycastCanvas.sortingOrder > raycastResultCanvas.sortingOrder || (raycastCanvas.sortingOrder == raycastResultCanvas.sortingOrder && raycast.distance < distance))) { //Update the closest distance distance = raycast.distance; @@ -183,7 +190,6 @@ private RaycastResult PerformRaycast(ExtendedPointerEventData eventData) } } } - //Clear the cache as we are done with it m_RaycastResultCache.Clear(); //Return the closest result or default if none exist return raycastResult;