- Home /
Set cursor position
How can i set mouse cursor position?
Answer by Ashkan_gc · Dec 24, 2009 at 01:42 PM
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.
how do you add this assembly to your project? I referenced it in mono, but it keeps dereferancing when I compile in unity?
You can't modify a generated file. The mono solution is generated by Unity.
You can add the dll file into your Unity asset folder and write "using namespaceOfTheDll;" at the beginning of your script to be able to use it.
$$anonymous$$ine is a $$anonymous$$oving UI, So it is must for me to reset cursor to centre of screen every time user hits a button or returns to $$anonymous$$ain $$anonymous$$enu. So plzz help.
If you need help with anything, then DON'T ask for it in a comment to a question that is 7 years old.
Please be sure to read the user guide and watch this video:
https://www.youtube.com/watch?v=ezAPpViLs2Q
If you need help, google your question and if you can't find an answer, ask a new question. Properly formulate your problem and explain what you tried and what doesn't work.
Don't do that here, ask a new question. But only if you can't find an answer on the internet.
problem is you also have to add System.Drawing in order the Point method to change System.Windows.Form.Cursor.Position.
Answer by Benproductions1 · Jul 04, 2012 at 10:33 AM
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 see this. Where can I get this mouse delta? Assu$$anonymous$$g it's Input.mousePosition from this and previous frame subtracted, then what, when real mouse will touch the screen edge? All deltas will become 0, even if GUI cursor will be on the center of the screen.
Edit: Oh, ok. I can do this with an axis.
Answer by duck · Dec 24, 2009 at 10:10 AM
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.
Is there a way to do it on WebGl build? [DllImport("user32.dll")] is not working on WebGl build.
Answer by amisane · Nov 01, 2016 at 04:32 PM
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);
}
I'm getting:
The type or namespace name 'DllImport' could not be found (are you missing a using directive or an assembly reference?)
when I try the above; do I need to add a 'using'?
Duh, ignore that. It was just 'using System.Runtime.InteropServices;'.
Answer by NightmarexGR · Apr 30, 2014 at 11:16 AM
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