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.
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);
}
}
}
↧