Search Unity

UI : Detect a click anywhere except on the UI

Discussion in 'Scripting' started by Quasar47, Feb 8, 2018.

  1. Quasar47

    Quasar47

    Joined:
    Mar 24, 2017
    Posts:
    122
    Hello there, my question is quite simple : Is it possible, via a built-in function in Unity or not, to detect if we clicked anywhere except on the UI (Image, button, etc...) which has the related script ?

    I know there is a built-in function in Unity that allows to detect the mouse events when hovering a gameObject, but this time I would like to detect if the mouse cursor is in its bounds or not, and if it's not the case, disable it.

    For example, I have a button that displays a panel and hides it each time I click on it. But I would like to hide the panel when I click anywhere else as well. Is it possible ? If yes, do you have any suggestions for me ?

    Thanks for your answer.
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I think this might work for you
    you can put this in Update()

    Code (CSharp):
    1. private void HideIfClickedOutside(GameObject panel) {
    2.          if (Input.GetMouseButton(0) && panel.activeSelf &&
    3.              !RectTransformUtility.RectangleContainsScreenPoint(
    4.                  panel.GetComponent<RectTransform>(),
    5.                  Input.mousePosition,
    6.                  Camera.main)) {
    7.              panel.SetActive(false);
    8.          }
    9.      }
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    or just have a image cover the entire screen at the UI root level (so "behind" everything ui) that allows pass through of clicks without an image set (so it's "invisible") and handle the clicks as you would the user clicking on any custom ui element.
     
    erangeva likes this.
  4. Quasar47

    Quasar47

    Joined:
    Mar 24, 2017
    Posts:
    122
    Hello there, thanks a lot for your answsers. Johne5, I already have tried these line yesterday and unfortunately, it didn't work. I had to apply some changes to make them fit my code, but even that was useless. Thanks for helping me though, I really appreciate :)

    LeftyRighty's solution worked however. I have applied a Button script to my mage cover to hide my panels each time I pressed on it. Simple, but it worked. At first I was afraid of using this method, since my code wasn't really optimized and practical to pick functions in it (Yeah I know, shame on me...) but in the end, it worked pretty well, so thanks once again ^^
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    For the record, @johne5 's answer should work. It might require you to pass 'null' instead of the main camera, if you're using a screen space overlay canvas. I've used a very similar piece of code before.
     
  6. krushna_sahoo

    krushna_sahoo

    Joined:
    Oct 28, 2020
    Posts:
    1
    Hello there, I am working on android AR project where i have given multiple panels combined with multiple buttons. when i click on a button, panel opens perfectly, but for closing the panel i had to touch the same button again. otherwise it overlaps on other panel. so please help me how can i close the panel anywhere i touch.