unity3d 控制鼠标的移动范围

电玩女神 2022-03-12 02:45 737阅读 0赞

在一些论坛上看到有人问在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

{

  1. public int Left;
  2. public int Top;
  3. public int Right;
  4. public int Bottom;
  5. public RECT( int left, int top, int right, int bottom )
  6. \{
  7. Left = left;
  8. Top = top;
  9. Right = right;
  10. Bottom = bottom;
  11. \}

}

RECT currentClippingRect;

RECT originalClippingRect = new RECT( );

void Start()

{

  1. hndl = GetForegroundWindow( );
  2. GetWindowRect( hndl, ref currentClippingRect );
  3. GetClipCursor( out originalClippingRect );
  4. ClipCursor( ref currentClippingRect);

}

void OnApplicationQuit()

{

  1. ClipCursor( ref originalClippingRect );

}

发表评论

表情:
评论列表 (有 0 条评论,737人围观)

还没有评论,来说两句吧...

相关阅读

    相关 unity3d摄像机控制

    其实不需要什么空物体了,war3式的平移效果其实是高度不变平行地面的一个平面上四处移动的问题了,光translate摄像机是不行的 贴上我完整的代码吧 复