Search Unity

Prevent mouse clicking through UI

Discussion in 'Scripting' started by matttardiff, Dec 24, 2020.

  1. matttardiff

    matttardiff

    Joined:
    Feb 27, 2018
    Posts:
    30
    I know there are lots of topics on this but I am not a programmer by any stretch so most of what I see is out of context, for me.
    The reason I'm posting here is in hopes that someone can point me to an already written script. Or someone that I can pay to write one.

    Basically my problem is this, I have a UICamera with a Pause and Quit button when I press Start.
    That all works fine. I found a script that'll lock and hide the curser and that works great as well.
    But no matter what I try, I can not stop the mouse from overriding the UI. Or passing, clicking through is another way I've seen it written. My game is only controller based and all keyboard inputs have been deleted from the Input Manager. So of course there's no reason to click on the mouse, but it's the one issue that's preventing my game being released to the Steam store.
     
  2. Kokowolo

    Kokowolo

    Joined:
    Mar 26, 2020
    Posts:
    60
    Hello matttardiff! I'm not certain what the actual issue in your game is, but this is some code I use in my current project.

    Code (CSharp):
    1. // verify pointer is not on top of GUI; if it is, return
    2. if (EventSystem.current.IsPointerOverGameObject()) return;
    3.  
    4. // continue and do your other code
    5. ...
    Essentially it makes sure that the mouse isn't over the UI so you don't accidentally pass through it and raycast/hit something on the other side of the UI, like another character or something.

    However, in reading your post, I'm a little confused. Are you having issues with overlapping buttons that you're trying to hide or disable? If so, you can set a button to be interactable. See the following link for an example: https://docs.unity3d.com/540/Documentation/ScriptReference/UI.Selectable-interactable.html
     
  3. matttardiff

    matttardiff

    Joined:
    Feb 27, 2018
    Posts:
    30
    Sorry for the confusion.

    I don't offer keyboard or mouse support for my game. When the player presses start for the UI pause/quit menu everything works great. I don't have any problems with overlapping buttons. I didn't know there was an issue with the mouse click and what I assume is ignoring the UI until the Steam reviewer sent it back.
    Why would the player press the mouse on a controller only based game? I don't know but it's that the problem exists.

    This is the script I found that hides and locks the mouse pointer.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HideLockCursor : MonoBehaviour
    6. {
    7.     // Update is called once per frame
    8.     void Update()
    9.     {
    10.         Cursor.lockState = CursorLockMode.Locked;
    11.         Cursor.visible = false;
    12.        
    13.     }
    14. }
    15.  
     
  4. Kokowolo

    Kokowolo

    Joined:
    Mar 26, 2020
    Posts:
    60
    Oh! Well sorry for being unhelpful. If you're trying to disable the mouse entirely on your buttons, then you could create your own button script that ignores mouse input. I'll try to find a better solution in the meanwhile.
     
  5. matttardiff

    matttardiff

    Joined:
    Feb 27, 2018
    Posts:
    30
    You're not unhelpful. Thanks for getting back.
     
  6. Kokowolo

    Kokowolo

    Joined:
    Mar 26, 2020
    Posts:
    60
    This solution isn't very aesthetic, but it works for disabling mouse input on a UnityEngine.UI.Button. It's what I was referring to in the last post.


    Code (CSharp):
    1.  
    2. using UnityEngine.EventSystems;
    3. using UnityEngine.UI;
    4.  
    5. public class DebugButton : Button
    6. {
    7.     // disables color change when moused over
    8.     public override void OnPointerEnter(PointerEventData eventData) { }
    9.  
    10.     // disables button event call
    11.     public override void OnPointerClick(PointerEventData eventData) { }
    12.  
    13.     // disables color change when pressed
    14.     public override void OnPointerDown(PointerEventData eventData) { }
    15. }
    16.  
    You might not want to include the function OnPointerEnter(...) because if your buttons are in the center of the screen where you lock your mouse, then it might look a little weird. I'm not sure though?

    EDIT: I removed the base function call from each of these. You want them to do nothing so you can just override them with empty code. Hopefully this works?
     
    Last edited: Dec 24, 2020
    matttardiff likes this.
  7. matttardiff

    matttardiff

    Joined:
    Feb 27, 2018
    Posts:
    30
    Thank you. I'll check it out
     
  8. matttardiff

    matttardiff

    Joined:
    Feb 27, 2018
    Posts:
    30
    Your code sent me in the right direction after I couldn't get it to work.
    Here is what I found, used and nearly freaked my dogs out with how excited I got.
    I slapped it onto the same GameObject that has the HideLockCursor script and bam! Success.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.EventSystems;
    6.  
    7. public class KillTheMouse : MonoBehaviour
    8. {
    9.     GameObject lastselect;
    10.  
    11.     void Start()
    12.     {
    13.         lastselect = new GameObject();
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (EventSystem.current.currentSelectedGameObject == null)
    19.         {
    20.             EventSystem.current.SetSelectedGameObject(lastselect);
    21.         }
    22.         else
    23.         {
    24.             lastselect = EventSystem.current.currentSelectedGameObject;
    25.         }
    26.     }
    27. }
     
  9. Kokowolo

    Kokowolo

    Joined:
    Mar 26, 2020
    Posts:
    60
    WOO! Glad you got the desired effect!
     
  10. Dev-lexvoid

    Dev-lexvoid

    Joined:
    Mar 7, 2023
    Posts:
    1
    u are a life saver