Set cursor position

How can i set mouse cursor position?

in mono you can use the system.windows.form.cursor.position but it needs the windows.form.dll and you need to add this assembly to your project. for mouse clicks you should use external APIs witch are in user32.dll but i don't know anything about OSX. in web players you can use javascript to do mouse movement and click.

note: you can not use external native code in web players.

You can work around this problem, by basically manually implementing a GUI based mouse and moving it through the mouse delta given by Unity. You could then set the position of this mouse and just use static variables to access it from every script.

Wish unity would fix it, so we don’t have to do these stupid workarounds.

I don't think it's possible to directly set the mouse cursor position with Unity's own API (although it's likely possible if you use a custom DLL in a standalone build).

However, if your goal is to set the mouse cursor position in order to achieve a "mouse look" effect, there's a lockCursor command to do just this.

I was able to create an implementation for this that works (in my 2D game running on Windows at least…)
Add this struct in to your project:

[StructLayout(LayoutKind.Sequential)]
public struct Point
{
    public int X;
    public int Y;
    public static implicit operator Vector2(Point p)
    {
        return new Vector2(p.X, p.Y);
    }
}

And add the following to some script:

     [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetCursorPos(out Point pos);
    
        private void MoveCursorToNearbyObject(GameObject objToFocus)
        {
            float targetWidth = 1920f;
            float targetHeight = 1080f;
    
            Vector2 inputCursor = Input.mousePosition;
            inputCursor.y = Screen.height - 1 - inputCursor.y;
            Point p;
            GetCursorPos(out p);
            var renderRegionOffset = p - inputCursor;
            var renderRegionScale = new Vector2(targetWidth / Screen.width, targetHeight / Screen.height);
    
            var objPos = objToFocus.transform.position;
    
            var newXPos = (int)(Camera.main.WorldToScreenPoint(objPos).x + renderRegionOffset.x);
            var newYPos = (int)(Screen.height - (Camera.main.WorldToScreenPoint(objPos).y) + renderRegionOffset.y);
    
            SetCursorPos(newXPos, newYPos);
        }

Hello guys it seems this problem is still active, for this reason i created a unity API that lets you set the cursor’s position on screen
“SetCursorPosition(x : int,y : int)”

http://forum.unity3d.com/threads/242832-Official-Set-Cursor-Position?p=1606714#post1606714

Seeing that I recently looked at this and since found that the new InputSystem has a solution, I’m going to post this link here: Mouse support | Input System | 1.0.2

Only tested on Windows so far, but works great.