Search Unity

IsPointerOverGameObject - Ignoring Some Objects

Discussion in 'UGUI & TextMesh Pro' started by JasonBricco, Nov 2, 2014.

  1. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    In my voxel game, much like other voxel games, you use the mouse to add or delete blocks in the world.

    When the pause menu or other UI elements are on screen, I don't want the click (used for returning back to the game) to go through and also add or delete a block in the world.

    So, I use IsPointerOverGameObject() to determine if the cursor is over an event object. If it is, I don't cast the ray into the world.

    The problem is, I overlay a cursor icon over the hidden/locked cursor while in the game. And it detects that the cursor is over this icon, and therefore doesn't let me place or delete any blocks ever.

    So my question is (and perhaps I overlooked the answer somewhere, but haven't been able to find quite what I'm looking for) - how can I make it so that IspointerOverGameObject() works in this way but ignores that cursor icon I'm using?

    Thanks!
     
  2. georgedarocha

    georgedarocha

    Joined:
    Jun 5, 2013
    Posts:
    2
    Hi Jason,

    I came across a similar problem in my game and what I did was use EventSystem.current.currentSelectedGameObject, it returns null if you are touching/clicking a GameObject (UI Element) that doesn't inherit from UnityEngine.UI.Selectable, for example an UI Image, and returns the GameObject if you're touching/click a Button (because it inherit from Selectable).

    I hope it work for you either.
     
    Last edited: Mar 18, 2015
  3. dylanHart

    dylanHart

    Joined:
    Sep 11, 2015
    Posts:
    3
    George, EventSystem.current.currentSelectedGameObject worked great for me. Thanks!
     
  4. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    I haven't checked this topic in a while and didn't notice the responses. I hadn't thought to try currentSelectedGameObject - that's interesting.

    I actually ended up setting the cursor image using Cursor.SetCursor, and so this wasn't an issue anymore. However, it's good to know for future cases :).
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Since its back on top and appears to be high in the search rankings, you can also use EventSystem.RaycastAll
     
  6. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
  7. wasicool7

    wasicool7

    Joined:
    Jul 28, 2016
    Posts:
    25
    Hi, sorry I looked up what you said and tried it out but I got an error and I made sure that I had this:
    Code (CSharp):
    1. using UnityEngine.EventSystems;
    2.  
    anyway this is my error: The name `EventSystems' does not exist in the current context
    And this is my code:
    Code (CSharp):
    1.     public GameObject currentSelectedGameObject;
    2.  
    3.     void Update () {
    4.             if (Input.GetMouseButtonDown (0)) {
    5.             if (!EventSystems.EventSystem.currentSelectedGameObject ()) {
    6.                         Vector3 mousePosition = Input.mousePosition;
    7.                         mousePosition.z = 10; // distance from the camera
    8.                         target = Camera.main.ScreenToWorldPoint (mousePosition);
    9.                         target.z = transform.position.z;
    10.             }
    11.         }
    12.        
    13.             transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.fixedDeltaTime);
    Anyway Thank you. :)
     
  8. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    EventSystem (note: no s at the end) is the variable your looking for. It resides in UnityEngine.EventSystems namespace.

    So at the top of your script add:
    using UnityEngine.EventSystems;
    and in your code get rid of EventSystems

    or change the line to
    UnityEngine.EventSystems.EventSystem. all the other stuff

    Just Using EventSystems by itself will never work since its not a root level namespace (or a class definition)
     
  9. baobieber

    baobieber

    Joined:
    Apr 19, 2016
    Posts:
    3
    Hi Everyone,

    If you want use IsPointerOverGameObject() & ignore any UI Image, you can uncheck Raycast Target in Image component of that GameObject.

    I hope this will be helpful for you.
     
  10. Clicksurfer

    Clicksurfer

    Joined:
    Aug 17, 2014
    Posts:
    77
    I can't believe I missed this. THANK YOU for saving me time fiddling with new functions to an already working system :)
     
  11. BlindsidedGames

    BlindsidedGames

    Joined:
    Aug 24, 2020
    Posts:
    9
    Found this code
    Code (CSharp):
    1. public bool IsPointerOverUIObject()
    2.     {
    3.  
    4.         if (EventSystem.current == null) return false;
    5.         PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
    6.         eventDataCurrentPosition.position = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    7.         List<RaycastResult> results = new List<RaycastResult>();
    8.         EventSystem.current.RaycastAll(eventDataCurrentPosition, results);
    9.         return results.Count > 0;
    10.     }
    From this saint
    https://github.com/GibsS/unity-pan-and-zoom

    It works for mobile too.