diff --git a/Assets/Scripts/InteractionEngine/WorldFactInteraction.cs b/Assets/Scripts/InteractionEngine/WorldFactInteraction.cs index 62e89a5f61c04fc17a6caa49ccc01ee45544baeb..64d70e57e4f0c364a0cd447c8c71402646bc1a37 100644 --- a/Assets/Scripts/InteractionEngine/WorldFactInteraction.cs +++ b/Assets/Scripts/InteractionEngine/WorldFactInteraction.cs @@ -1,6 +1,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.EventSystems; using UnityEngine.UI; /// <summary> @@ -29,10 +30,12 @@ void LateUpdate() private void UpdateDisplay() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); - if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, factLayerMask)) // check if fact was hit + // if no fact was hit or pointer was over other UI + if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, factLayerMask) || WasOtherUIHit()) { + // destroy currentDisplay if it exists lastHit = null; - Destroy(currentDisplay); // nothing was hit -> destroy currentDisplay if it exists + Destroy(currentDisplay); return; } @@ -43,6 +46,7 @@ private void UpdateDisplay() // should never happen, if the layerMask is set up correctly //Debug.LogError("WorldFactInteraction Raycast collided with object in factLayerMask, that did not contain a FactObject script: " + hit.transform.gameObject.name); lastHit = null; + Destroy(currentDisplay); return; } @@ -71,6 +75,27 @@ private static void ChangeImageAlpha(Image img, float alpha) { img.color = new Color(img.color.r, img.color.g, img.color.b, alpha); } + + /// <summary> + /// Returns true if any UI other than currentDisplay was hit + /// </summary> + /// <returns></returns> + private bool WasOtherUIHit() + { + PointerEventData pointerData = new(EventSystem.current) + { + position = Input.mousePosition + }; + + List<RaycastResult> results = new(); + EventSystem.current.RaycastAll(pointerData, results); + + foreach (var res in results) + if (currentDisplay == null || !res.gameObject.transform.IsChildOf(currentDisplay.transform)) + return true; + + return false; + } #endregion Helper }