In some forums, someone asked how to control the mouse's moving range in unity. There are two methods: one is to call the clipcursor function of user32.dll in windows.
Another method is to use the cursor. setcursor function.
Here we will introduce the first method.Code
[Dllimport ("user32.dll", charset = charset. Auto, exactspelling = true)]
[Return: financialas (unmanagedtype. bool)]
Public static extern bool clipcursor (ref rect rcclip );
[Dllimport ("user32.dll")]
[Return: financialas (unmanagedtype. bool)]
Public static extern bool getclipcursor (Out rect rcclip );
[Dllimport ("user32.dll")]
Static extern int getforegroundwindow ();
[Dllimport ("user32.dll")]
[Return: financialas (unmanagedtype. bool)]
Static extern bool getwindowrect (INT hwnd, ref rect lprect );
[Structlayout (layoutkind. Sequential)]
Public struct rect
{
Public int left;
Public int top;
Public int right;
Public int bottom;
Public rect (INT left, int top, int right, int bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
}
Rect currentclippingrect;
Rect originalclippingrect = new rect ();
Void start ()
{
Hndl = getforegroundwindow ();
Getwindowrect (hndl, ref currentclippingrect );
Getclipcursor (Out originalclippingrect );
Clipcursor (ref currentclippingrect );
}
Void onapplicationquit ()
{
Clipcursor (ref originalclippingrect );
}