Search Unity

RaycastAll versus Raycast

Discussion in 'UGUI & TextMesh Pro' started by Tarrag, Oct 9, 2019.

  1. Tarrag

    Tarrag

    Joined:
    Nov 7, 2016
    Posts:
    215
    Hey all,

    Trying to know what UI objects were hit when mouseclicked.

    Raycast (as in m_GraphicRaycasterUI.Raycast(m_PointerEventDataUI, results) doesn't return what UI objects are hit.

    However EventSystem.current.RaycastAll(m_PointerEventDataUI, results); does. But I'd like to avoid the performance hit cos don't need to know 3D objects hit.

    Any tips much appreciatd :)

    Cheers, Sergio

    My UI hierarchy is:
    - Main Canvas (vertical layout group, Canvas and Graphic Raycaster components)
    - Header
    - Bottom Canvas (Canvas and Graphic Raycaster components)
    - Object with Horizontal Layout Group element
    - UI Object
    - UI Object (Vertical Layout Group)
    - UI Object
    - UI Object
    - UI Object​
    I don't get any results of what UI objects were hit (but I get the mouse click position) with:
    Code (CSharp):
    1. if(Input.GetMouseButtonDown(0))
    2.         {
    3.             m_PointerEventDataUI = new PointerEventData(m_EventSystemUI);
    4.             m_PointerEventDataUI.position = Input.mousePosition;
    5.             Debug.Log("Click at " + m_PointerEventDataUI.position);
    6.             List<RaycastResult> results = new List<RaycastResult>();
    7. GraphicRaycaster GraphicRaycasterUI=GetComponent<GraphicRaycaster>();//(from the Bottom Canvas)
    8.             GraphicRaycasterUI.Raycast(m_PointerEventDataUI, results);
    9.  
    10.             foreach (RaycastResult result in results)
    11.             {
    12.                     Debug.Log("Touching " + result);
    13.             }
    14.         }
    However I get all UI objects and 3D objects that were hit with this, but want to avoid the performance hit of detecting 3D objects hit cos don't need them:

    Code (CSharp):
    1. if(Input.GetMouseButtonDown(0))
    2.         {
    3.             m_PointerEventDataUI = new PointerEventData(m_EventSystemUI);
    4.          m_PointerEventDataUI.position = Input.mousePosition;
    5.             Debug.Log("Click at " + m_PointerEventDataUI.position);
    6.             List<RaycastResult> results = new List<RaycastResult>();
    7.             EventSystem.current.RaycastAll(m_PointerEventDataUI, results);
    8.  
    9.             foreach (RaycastResult result in results)
    10.             {
    11.                     Debug.Log("Touching " + result);
    12.             }
    13.         }
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231