- Home /
Can't interact with world space ui button when cursor mode is locked any solutions?
Can't interact with world space buttons when cursor is locked (can't interact as in button doesn't fade to a colour when hovering over) any other way to lock cursor to centre and also interact with world space buttons?
Answer by DenisIsDenis · May 06, 2021 at 10:01 AM
Hello. This script should work.
public class LockMouse : MonoBehaviour
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetCursorPos(int x, int y);
public bool isLock = true;
void Update()
{
if (Input.GetKeyDown(KeyCode.L))
{
isLock = !isLock;
}
/*
In the editor, the cursor will be slightly offset from the center,
but in the build it should be exactly in the center (if the game is full screen)
*/
if (isLock)
{
SetCursorPos(Screen.width / 2, Screen.height / 2);
Cursor.visible = false;
}
else
{
Cursor.visible = true;
}
}
}
I'm wondering how I could get something working like this, but on a Mac. Do you have any suggestions? I'm having the same issues using a locked cursor and world gui. Unfortunately, setcursorpos seems to only be for windows (and user32.dll is a windows file)
Your answer
Follow this Question
Related Questions
UI button and raycast script that I have made both aren't working to change my scene 1 Answer
How to click on irregular buttons in new ui 4 Answers
Holding down UI Button and shooting raycast from touch position doesn't work simultaneously:(( 0 Answers
Raycasting UI components 3 Answers
Unity 5 new UI Button Colliders moved to the left in different resolutions 1 Answer