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. Dismiss Notice

Feature Request OnMouseDown smarter priorities

Discussion in 'UI Toolkit' started by Cind13, Nov 4, 2020.

  1. Cind13

    Cind13

    Joined:
    Jul 4, 2017
    Posts:
    5
    Hello Unity Forum!

    The OnMouseDown Callback (and similar mouse events) can not only be used by UI-Elements, but also by game objects with a collider. However, if multiple colliders overlap with the mouse position when the mouse button is pressed, only one game object gets OnMouseDown callbacks called.

    In a 2D Game Setting, Colliders/GameObjects are considered by Z-axis order and when it comes to a draw, it is seemingly random which object gets the call. (Creation/Edit order of the game objects in the editor seems to have something to do with it)

    However, in my opinion, a very big use case that a lot of developers will intuitively go for with OnMouseDown is giving game objects with sprite renderers a 2DCollider to make them clickable. In this specific scenario Z-axis order will always result in draws (z typically being 0). However, there already exists a very explicit order on the game objects: the rendering order.

    My request/proposal is:

    Extend the usage of GetRayIntersection that currently determines which game object receives the callback on mouse clicks, so that draws (that are currently resolved randomly anyway) favor game objects with sprite renderers, with them being ordered like with rendering (layer + order in layer). Basically, in a 2D Game a Mouse Click should hit the frontmost sprite, since users typically click what they can see.

    Thank you for your Time!
     
    DMorock likes this.
  2. broots

    broots

    Joined:
    Dec 20, 2019
    Posts:
    54
    I'd like to echo this, even within the UI elements framework itself it currently appears impossible to layer objects on top of eachother as the top object will always "consume" the click. Additionally, supporting things like alpha-clickthrough or even simply replicating the "raycast target" bool in UGUI seems basically impossible as it stands currently.

    Cheers.
     
    Cind13 and unitydungeonmas like this.
  3. unitydungeonmas

    unitydungeonmas

    Joined:
    Sep 6, 2020
    Posts:
    37
    i am also trying to do this where I use a mouse to "shoot" at the screen using
    Mouse.current.position.ReadValue() and the camera, but only if it doesn't hit any UI.. but right now it always triggers both
     
    Cind13 likes this.