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

Preventing uGUI mouse click from passing through GUI controls

Discussion in 'UGUI & TextMesh Pro' started by Kaz_Yamof, Oct 3, 2014.

  1. Kaz_Yamof

    Kaz_Yamof

    Joined:
    Jan 17, 2013
    Posts:
    28
    Hi,
    I have some gameobjects that can be selected by click mouse, but they are been selected even if I click on my GUI (if the GUI is over the gameobject on screen)
    There's any way to avoid mouse clicks over UI of interact with the gameobjects?

    Thanks!
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Put those objects under a CanvasGroup, it has options for clickthrough and such.
     
    AntFitch likes this.
  3. Kaz_Yamof

    Kaz_Yamof

    Joined:
    Jan 17, 2013
    Posts:
    28
    Didn't work. The click mouse still goes through GUI to World and affects my objects selection's
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Oh, you need to prevent your own scripts from catching clicks, you mean?

    Code (csharp):
    1.  
    2. // <= 4.6b18
    3. if (!EventSystemManager.currentSystem.IsPointerOverEventSystemObject() )
    4.  
    5. // >= 4.6b19
    6. if (!EventSystem.current.IsPointerOverGameObject() )
    7.  
     
  5. Kaz_Yamof

    Kaz_Yamof

    Joined:
    Jan 17, 2013
    Posts:
    28
    Whoa! I was trying something with the EventSystem right now!
    It's exactly this that I needed.
    As each selectable 3D game object has a script that implement the OnClick() method, when is clicked something happens.
    I made
    Code (csharp):
    1. void OnClick()
    2. {
    3.      if (!EventSystem.current.IsPointerOverGameObject())
    4.          //do something
    5. }
    and works perfectly.
    Thank you @StarManta .
     
    Clovis974 likes this.
  6. melkior

    melkior

    Joined:
    Jul 20, 2013
    Posts:
    199
    You probably don't need this but after some testing and tweaking I handle it with this bit of code where I use the event system to catch the UI hits (and therefore block raycasts) and if no UI was hit then go on and do the raycast instead:

    Code (CSharp):
    1. public class MouseClickTest : MonoBehaviour {
    2.  
    3.     public EventSystem eventSystem;
    4.  
    5.     public Camera orthoCamera;
    6.  
    7.  
    8.     void Update () {
    9.  
    10.         if (Input.GetMouseButton(0)) {
    11.             if (eventSystem.IsPointerOverGameObject()){
    12.                 // No code needed here your UI elements will receive this hit and NOT do raycast info in the else below
    13.             } else {
    14.                 Ray ray = orthoCamera.ScreenPointToRay(Input.mousePosition);
    15.                 RaycastHit hit;
    16.                
    17.                 if (Physics.Raycast(ray, out hit)) {
    18.  
    19.                     // if you got a valid hit with the ray then do something with it here
    20.                 }
    21.             }
    22.         }
    23.     }
    24.    
    25.     }
     
  7. Kaz_Yamof

    Kaz_Yamof

    Joined:
    Jan 17, 2013
    Posts:
    28
    I got it @melkior . Thanks!
    I did the selection by click using raycast before, but as each of my objects are dynamically created, I prefered put a script on each one doing implementation of OnClick() and delegating for Unity the work of specify when the mouse was clicked over which object (because will activate its OnClick()).
    But I imagine that the OnClick() method shall encapsulate some logic like your Raycast approach that you made above (same as I did on the past). Am I right?
     
    Last edited: Oct 4, 2014
  8. mk26

    mk26

    Joined:
    Dec 2, 2014
    Posts:
    5
    // UI elements will receive this hit/hover
    if (EventSystem.current.IsPointerOverGameObject()) { // UI elements getting the hit/hover
    }

    works for me...
     
  9. Nubeh

    Nubeh

    Joined:
    Oct 7, 2012
    Posts:
    47

    Hi there,
    I stumbled upon your post because I'm trying to make so that my physics raycast is blocked by the GUI but I'm trying to solve this with touch input, sadly the following code isn't working:

    if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended){

    if (eventSystem.IsPointerOverGameObject(Input.GetTouch(0))){


    what am I doing wrong?

    Thanks
     
  10. melkior

    melkior

    Joined:
    Jul 20, 2013
    Posts:
    199
    Nubeh - I have not tested this with Touch input at all - its possible something else is needed in this context unfortunately I am not working on mobile titles so its outside of my current experience/knowledge.ts
     
  11. DalamarTD7

    DalamarTD7

    Joined:
    Jan 21, 2016
    Posts:
    3
    Here's the solution I came up with for mobile. I set the GraphicRaycaster in the inspector from my one canvas item I have in my scene. Whatever code you put in the designated spot will only register if you are not pressing on a UI element.

    Code (CSharp):
    1.   using UnityEngine.EventSystems;
    2.    
    3.      Vector2 touchPos;
    4.      public GraphicRaycaster GR;
    5.    
    6.      void Update()
    7.          {
    8.              if(Input.touchCount > 0)
    9.              {
    10.                  if(Input.GetTouch(0).phase == TouchPhase.Began)
    11.                  {
    12.                      PointerEventData ped = new PointerEventData(null);
    13.                      ped.position = Input.GetTouch(0).position;
    14.                      List<RaycastResult> results = new List<RaycastResult>();
    15.                      GR.Raycast(ped, results);
    16.                      if(results.Count == 0)
    17.                      {
    18.                                        // YOUR CODE HERE
    19.                      }
    20.                  }
    21.              }
    22.           }
     
    Game_Developer20 likes this.
  12. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    I have the same problem:

    Clicking on a UI-Button passes through and triggers clickable 3D-Objects.

    In my opinion there should be a simple toggle somewhere (in the GraphicRaycaster) to avoid this.
     
  13. Game_Developer20

    Game_Developer20

    Joined:
    Jan 11, 2022
    Posts:
    1