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

Best way to block clicks on certain part of screen?

Discussion in 'Scripting' started by jsmall81, Mar 4, 2021.

  1. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    I Want to disable left clicks in a certain script when the mouse is over the lower part of the screen. The UI part on the bottom of this image.


    I've tried attaching a script to the image to do it when the mouse is over, which kind of works, but if I go over one of the other images, it doesn't detect it. All the smaller images are separate UI objects that are enabled when selecting different game objects. I tried to run a ray to detect if the mouse was over any image, but it doesn't seem to work at all.

    All I need to do is have a certain part of a script that handles unit movement to bypass left clicking as long as I am mouse over the lower menu part of the screen. Just looking for ideas. Thanks.
     
  2. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    You'd need to poll your mouse position every frame and set a bool like isOverMenu. If it's true, then a condition in your unit movement script will disallow the default actions.
    Your shapes at bottom are simple boxes, so you can use a rect bounds kind of check quite easily.
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You can't block the click itself afaik, you can only ignore it.

    do you know about EventSystem.current.IsPointerOverGameObject ? that what I use to differentiate "game" clicks from UI clicks

    you can just stick a transparent rect over there to block the UI
     
    Joe-Censored likes this.
  4. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    Right, that's what I mean. I plan on enabling a bool on my script and having it not perform the function on click if the bool is true, which would mean over my UI.