unity3d 控制鼠标的移动范围

在一些论坛上看到有人问在unity里面控制鼠标的移动范围,有二种方法,一个是调用windows 系统的 user32.dll的ClipCursor函数 

再一种就是 通过Cursor.SetCursor函数

这里就介绍一下 第一种方法吧,不说废话了  直接上代码

 

[DllImport( "user32.dll", CharSet = CharSet.Auto, ExactSpelling = true )]

[return: MarshalAs( UnmanagedType.Bool )]

public static extern bool ClipCursor( ref RECT rcClip );

[DllImport( "user32.dll" )]

[return: MarshalAs( UnmanagedType.Bool )]

public static extern bool GetClipCursor( out RECT rcClip );

[DllImport( "user32.dll" )]

static extern int GetForegroundWindow( );

[DllImport("user32.dll")]

[return: MarshalAs( 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 );

}

 

 

你可能感兴趣的:(unity3d 控制鼠标的移动范围)