Search Unity

Lock Mouse to Window

Discussion in 'Scripting' started by SimssmiS, Apr 4, 2014.

  1. SimssmiS

    SimssmiS

    Joined:
    Nov 9, 2013
    Posts:
    3
    Hi,
    I'm trying to lock my cursor to the window when in build windowd mode.

    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Runtime.InteropServices;
    5. using UnityEngine;
    6.  
    7.  
    8.  
    9. #if UNITY_STANDALONE_WIN
    10.     [DllImport("user32.dll")]
    11.     static extern bool ClipCursor(ref RECT lpRect);    //ERROR at bool
    12.  
    13.     public struct RECT
    14.     {
    15.         public int Left;
    16.         public int Top;
    17.         public int Right;
    18.         public int Bottom;
    19.     }
    20. #endif
    21.  
    22. public class Cam_Movement : MonoBehaviour {
    23.  
    24.    
    25.     public InputsForCamera[] Inputs = new InputsForCamera[1];
    26.     public float zoomMulti;
    27.     public float moveMulti;
    28.     public float turnMulti;
    29.     public GameObject CamParent;
    30.  
    31.     private CamInputs camInput;
    32.  
    33.     // Use this for initialization
    34.  
    35.  
    36.     public void Start()
    37.     {
    38.         RECT cursorLimits;
    39.         cursorLimits.Left = 0;
    40.         cursorLimits.Top = 0;
    41.         cursorLimits.Right = Screen.width - 1;
    42.         cursorLimits.Bottom = Screen.height - 1;
    43.         ClipCursor(ref cursorLimits);
    44.     }
    45.  
    dose anybody see the problem? I assume I'm just blind and not seeing it.

    Thanks
    SimssmiS
     
    Last edited: Apr 4, 2014
  2. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
  3. SimssmiS

    SimssmiS

    Joined:
    Nov 9, 2013
    Posts:
    3
    Im not talking about locking the mouse to the Screen center
    I'm looking for a solution to keep the mouse in the bounds of the window.
     
  4. landon912

    landon912

    Joined:
    Nov 8, 2011
    Posts:
    1,579
    We can't see what's happening in ClipCusor. How would we help?
     
  5. SimssmiS

    SimssmiS

    Joined:
    Nov 9, 2013
    Posts:
    3
    My Problem is Not the dll itsself. Visual Studio teils me that it. Expertise a class enum or delegate insted of a bool.