Skip to content
Snippets Groups Projects
Commit ebbd3104 authored by Tobias Schöner's avatar Tobias Schöner
Browse files

fix: hoverFact not displayed anymore if UI is in the way

parent 81a68513
No related branches found
No related tags found
No related merge requests found
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI; using UnityEngine.UI;
/// <summary> /// <summary>
...@@ -29,10 +30,12 @@ void LateUpdate() ...@@ -29,10 +30,12 @@ void LateUpdate()
private void UpdateDisplay() private void UpdateDisplay()
{ {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 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; lastHit = null;
Destroy(currentDisplay); // nothing was hit -> destroy currentDisplay if it exists Destroy(currentDisplay);
return; return;
} }
...@@ -43,6 +46,7 @@ private void UpdateDisplay() ...@@ -43,6 +46,7 @@ private void UpdateDisplay()
// should never happen, if the layerMask is set up correctly // 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); //Debug.LogError("WorldFactInteraction Raycast collided with object in factLayerMask, that did not contain a FactObject script: " + hit.transform.gameObject.name);
lastHit = null; lastHit = null;
Destroy(currentDisplay);
return; return;
} }
...@@ -71,6 +75,27 @@ private static void ChangeImageAlpha(Image img, float alpha) ...@@ -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); 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 #endregion Helper
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment