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

Detecting a UI element is over another UI element

Discussion in 'UGUI & TextMesh Pro' started by jtsmith1287, Dec 8, 2014.

  1. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    I have an icon that's controlled by the player and the camera is parented to it. The icon moves over a map and if the icon is over a particular region/point I want to interact with it. I've googled a little bit and pondered on it for a few days while working on other stuff but haven't thought of a what I feel is a reasonable way of handling this.

    I'm essentially curious if there's a way of doing this *without* adding a 2D collider to my UI elements, since this forces me to also put 2D rigidbodies on them. It just seems over kill. If it isn't going to be a hit on memory or performance than I'll go ahead and do it with ray casting and 2 colliders.
     
  2. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    maybe RectTransformUtility.RectangleContainsScreenPoint or something in there?
     
  3. CaoMengde777

    CaoMengde777

    Joined:
    Nov 5, 2013
    Posts:
    813
    did you figure this out?? i cant find a way to do so..

    i even did try 2d rigidbody and 2d collider, and it doesnt work on UI elements???
     
  4. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,181
    If you do:

    EventSystem.current.RaycastAll it will return a list of raycast results sorted into the hit order. So you will get a list of all things under the point :)
     
    rakkarage likes this.
  5. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Does that work with UI elements, or will a rigidbody/collider need to be attached?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This will work on anything that has an appropriate raycaster in the scene. So on UI elements as long as you have a graphics raycaster (normally on the canvas). And colliders as long as you have a physics raycaster (usually on the camera).