- Home /
Disable mouse input and cursor in game
Hello, I'd like to completely disable everything related to mouse input. My game should be controlled only via keyboard (GUI and gameplay).
So, how can I disable mouse input and cursor? I don't want to show cursor in whole game and as I said game should only use keyboard.
Thanks.
Answer by Vicarian · May 18, 2018 at 07:07 PM
To disable mouse input, open the InputManager from Edit > Project Settings > Input. Remove the input entries with Mouse in their name. Expand Fire1 - Fire3 and remove the mouse buttons from these. To hide the cursor, use the statements:
Cursor.lockState = CursorLockMode.locked;
Cursor.visible = false;
I did what you suggested. Actually, it works partially. There is a problem with mouse clicks. In menu screens in my Event System uses first selected button. When I click mouse, button loses focus and I can't navigate thourgh buttons in menu.
Do you have a solution for it?
I'm not sure if there's a clean way of completely disabling it. One hack you could pull is to just set the focus back to the button when the mouse is clicked.
This will temporarily break your menu navigation, but try unchecking Send Navigation Events in the EventSystem, and see if the mouse button causes focus loss?
For reference, it's CursorLock$$anonymous$$ode.Locked (with an upper-case L).
When I tried this it would either only work when my cursor is over the game view, or it would give me a compile error and say "'CursorLock$$anonymous$$ode' does not contain a definition for locked" Is there a library I have to import? I only have the defaults for when you make a new c# script.
Thanks in advanced :)
Answer by Robdon · Feb 14, 2020 at 06:01 PM
A bit late, but I just found this as I was trying to do the same.
What I did was have a GO (named MouseDisable) with a UI Image on it that stretches across the whole screen, and has 'Raycast Target' ticked. Also, make the Color white, and with Alpha = 0. IE not visible.
Then when ever I change the Cursor.visible, I just enable/disable that GO, and it 'absorbs' all the mouse clicks.
So, something like:
public void SetCursorVisible(bool vis) {
Cursor.visible = vis;
MouseDisable.SetActive(!vis);
}
Worked fine for what I needed. HTHs.
Answer by duckquark · Apr 02, 2019 at 10:52 PM
Hey. I had a similar issue and this worked for me. If there's any mouse clicks it just goes back to the default selection.
If you stored your current selection in a variable you could always use that as a parameter: CatchMouseClicks(currentButton);
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0) || Input.GetMouseButtonDown(1) || Input.GetMouseButtonDown(2))
{
CatchMouseClicks(defaultSelection);
}
}
public void CatchMouseClicks(GameObject setSelection)
{
EventSystem.current.SetSelectedGameObject(setSelection);
}
If you are using Color Tint to indicate selected objects, this causes a visual indication that you clicked even if you are running a game where the mouse is "disabled". It very quickly deselects and reselects the object.
I$$anonymous$$HO, not being able to have this new UI event system just ignore clicks is kinda dodgy. Has Unity not heard of keyboard driven games?
Your answer
Follow this Question
Related Questions
Unity Web Player does not respond to mouse clicks 1 Answer
Mouse click is detected twice 1 Answer
EventSystem - detect any click NOT on object 1 Answer
mouse Input Instantiate 0 Answers