From ebbd3104f6795a71914e4a27788b3f08e42c4782 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tobias=20Sch=C3=B6ner?= <tobias.stonehead@gmail.com>
Date: Wed, 11 Jan 2023 19:32:02 +0100
Subject: [PATCH] fix: hoverFact not displayed anymore if UI is in the way

---
 .../InteractionEngine/WorldFactInteraction.cs | 29 +++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/Assets/Scripts/InteractionEngine/WorldFactInteraction.cs b/Assets/Scripts/InteractionEngine/WorldFactInteraction.cs
index 62e89a5f..64d70e57 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
 }
 
-- 
GitLab