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 ?
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.
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 ^^
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.
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.