Prevent mouse clicking through UI

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.

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.

// verify pointer is not on top of GUI; if it is, return
if (EventSystem.current.IsPointerOverGameObject()) return;

// continue and do your other code
...

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: Unity - Scripting API: UI.Selectable.interactable

9 Likes

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HideLockCursor : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
       
    }
}

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.

You’re not unhelpful. Thanks for getting back.

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.

using UnityEngine.EventSystems;
using UnityEngine.UI;

public class DebugButton : Button
{
    // disables color change when moused over
    public override void OnPointerEnter(PointerEventData eventData) { }

    // disables button event call
    public override void OnPointerClick(PointerEventData eventData) { }

    // disables color change when pressed
    public override void OnPointerDown(PointerEventData eventData) { }
}

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?

1 Like

Thank you. I’ll check it out

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.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class KillTheMouse : MonoBehaviour
{
    GameObject lastselect;

    void Start()
    {
        lastselect = new GameObject();
    }

    void Update()
    {
        if (EventSystem.current.currentSelectedGameObject == null)
        {
            EventSystem.current.SetSelectedGameObject(lastselect);
        }
        else
        {
            lastselect = EventSystem.current.currentSelectedGameObject;
        }
    }
}
5 Likes

WOO! Glad you got the desired effect!

u are a life saver