Click here to Skip to main content
15,887,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
public bool MOUSELOCK;
 public bool Buttonpress;
 public Button button;
 [DllImport("user32.dll")]
 static extern bool ClipCursor(ref RECT lpRect);
 // Start is called before the first frame update
 public struct RECT
 {
     public int Left;
     public int Top;
     public int Right;
     public int Bottom;
 }
 
 
 void Start(){
 
     MOUSELOCK = false;
     Buttonpress = false;
 }
 
 public void OnPointerDown(PointerEventData eventData){
 
 
         MOUSELOCK = true;
 
 }
 
 public void OnClicked(){
 
 
 
     Debug.Log ("Clicked true");
 
 
     if (MOUSELOCK == true) {
 
         Debug.Log ("Clicked trueCON");
         RECT cursorLimits;
 
         cursorLimits.Left = 40;
         cursorLimits.Top = 30;
         cursorLimits.Right = Screen.width - 8;
         cursorLimits.Bottom = Screen.height + 2;
         ClipCursor (ref cursorLimits);
 
 
 
 
         }
 }
 
 
 
     }

Hello, I am using user32.dll in Unity to write to the mouse's movement. I realized the cursor must be operating in Windows screen space, and I cannot figure out how to get it to operate only within the bounds of the Unity Game Window or Unity screenspace. When I try to set the mouse position based on Unity World or Screen Coordinates I get undesired results. I tried added an offset to the values but this only works when the camera is not zoomed in and the game window is full screen. I am new to coding but I really need some help as to how to be able to lock the cursor's position to position in the unity world/screen space when certain conditions in the game are met.

The second problem is I cannot undo the restriction of the mouse's movement from my script. Even after I stop running the game the mouse stays within the parameters set in the code. (Not desirable!)

Thankyou for your time!

I have looked at many different posts online but some of them are above my head. This is the first time I have used code not in the Unity API, so trying to understand how the Windows classes function and such is going very slowly.


What I have tried:

C#
<pre>public bool MOUSELOCK;
 public bool Buttonpress;
 public Button button;
 [DllImport("user32.dll")]
 static extern bool ClipCursor(ref RECT lpRect);
 // Start is called before the first frame update
 public struct RECT
 {
     public int Left;
     public int Top;
     public int Right;
     public int Bottom;
 }
 
 
 void Start(){
 
     MOUSELOCK = false;
     Buttonpress = false;
 }
 
 public void OnPointerDown(PointerEventData eventData){
 
 
         MOUSELOCK = true;
 
 }
 
 public void OnClicked(){
 
 
 
     Debug.Log ("Clicked true");
 
 
     if (MOUSELOCK == true) {
 
         Debug.Log ("Clicked trueCON");
         RECT cursorLimits;
 
         cursorLimits.Left = 40;
         cursorLimits.Top = 30;
         cursorLimits.Right = Screen.width - 8;
         cursorLimits.Bottom = Screen.height + 2;
         ClipCursor (ref cursorLimits);
 
 
 
 
         }
 }
 
 
 
     }
Posted
Comments
[no name] 17-Aug-19 17:41pm    
Trying to use hacks with a 3rd party product is like performing dentistry on yourself: you may have some luck but probably a lot of pain too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900