Unity3d controls the moving range of the mouse

Source: Internet
Author: User

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 );

}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.