Search Unity

Unity UI GraphicRaycaster Not working (GraphicRaycaster.Raycast(PointerEventData, List<RaycastResult>)

Discussion in 'UGUI & TextMesh Pro' started by im7akash_, Jul 4, 2022.

  1. im7akash_

    im7akash_

    Joined:
    May 21, 2022
    Posts:
    2
    Issue:
    Raycast()
    function of
    GraphicsRaycaster
    doesn't seem to be working on non interactable objects*.

    * It works for buttons which are set to interactable, and it doesn't work with button which aren't interactable. Also, It doesn't with any other UI element.

    Issue Details:
    • I am trying to get the UI elements via graphics raycast.
    • It doesn't with any other UI element expect buttons which are marked as interactable.

    More Context:
    • I was trying to build a popup which closes when clicked outside of popup area.
    • I have got that functionality working on individual panel.
    • But, for nested Panels (Panel buttons -> opens -> another Panel with Button) I need to get the UI element which is clicked. Then it could be checked whether that UI element is descendent of the current open Panel element or not.
    I'm attaching 2 code files here -

    1. GetUiElementRaycast : MonoBehaviour
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class GetUiElementRaycast : MonoBehaviour
    8. {
    9.     public GameObject currentSelectedUI;
    10.     public GraphicRaycaster gRaycast;
    11.  
    12.     private void Awake() {
    13.         gRaycast = GetComponent<GraphicRaycaster>();
    14.     }
    15.  
    16.     private void Update() {
    17.         getUiElement();
    18.     }
    19.  
    20.     void getUiElement(){
    21.         PointerEventData pEventData = new PointerEventData(EventSystem.current);
    22.         pEventData.position = DebugMousePosition.getPointerPosition();
    23.         List<RaycastResult> results = new List<RaycastResult>();
    24.        
    25.         gRaycast.Raycast(pEventData, results); // EventSystem.current.RaycastAll(pEventData,results); <-- Even Tried this
    26.  
    27.         if(pEventData.selectedObject){
    28.             Debug.Log(pEventData.selectedObject+"");
    29.         }
    30.  
    31.         currentSelectedUI = pEventData.selectedObject;
    32.     }
    33. }
    34.  
    2. snippet of DebugMousePosition.getPointerPosition()
    Code (CSharp):
    1. public static Vector3 getPointerPosition(){
    2.         Vector3 mousePos = pointerPositionInputAction.ReadValue<Vector2>();
    3.         mousePos.z = Camera.main.nearClipPlane;
    4.         return Camera.main.ScreenToWorldPoint(mousePos);
    5.     }
    Scene Setup:
    upload_2022-7-4_18-13-50.png
    upload_2022-7-4_18-13-33.png
    upload_2022-7-4_18-14-17.png

    Any guidance is greatly appreciated!
     

    Attached Files: