Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Detect all the differents UI elements under mouse

Discussion in 'Scripting' started by _HAL_9000, Aug 7, 2017.

  1. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48
    Hello everyones !
    I am currently developing a RTS like project where I have to be able to select objects by two ways :
    - Click on it to select a single object.
    - Drag to select multiple objects.
    Both methods are implemented so far, but I would like to be able to get the list of all the UI elements under my mouse when I click. So I could know if the user actually mean to click on UI or a gameObject.

    I can't just check if the user clicked on GUI or not because when the user drag to select multiple object I draw a square from the moment he clicked, so the user always click on GUI..

    So far I tried using GraphicRaycaster.Raycast like :

    Code (CSharp):
    1.         PointerEventData    ped = new PointerEventData (null);
    2.  
    3.         ped.position = Input.mousePosition;
    4.         List<RaycastResult> results = new List<RaycastResult> ();
    5.  
    6.         gr.Raycast (ped, results);
    But results.Count was always equals to 0.

    Thanks for your help ! :D
     
  2. _HAL_9000

    _HAL_9000

    Joined:
    Jun 26, 2015
    Posts:
    48