Is it possible to get Input.mousePosition when the game is minimized?

li7mc0

I’m working on a tool for streamers that is supposed to be overlayed on top of a stream’s screen in programs like OBS. I want to capture the cursor position in order to make the character look at it, as shown in the video, but -here’s the catch- when the game is minimized. Currently, when the game is out of focus it’s perfect. When I minimize the game the character looks at the right-bottom corner of the screen, ignoring completely the cursor.

I read in an older thread that Unity disables some inputs when the game is out of focus as a security measure (to prevent keylogging an the like). But that seems to have changed, since I can in fact get the cursor position when the game is out of focus, though not minimized.

Can this be done?

bump. Same situation here, I’m also developing a vtuber app.

Found the solution:

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetCursorPos(out System.Drawing.Point lpPoint);

private void OnGUI()
{
    System.Drawing.Point cursorPoint;
    GetCursorPos(out cursorPoint);

    Rect labelRect = new Rect(200, 100, 600, 1000);
    GUI.color = Color.yellow;
    GUI.skin.label.fontSize = 24;
    string debugtext = "";
    debugtext += $"{cursorPoint.X} {cursorPoint.Y}";

    GUI.Label(labelRect, debugtext);
}

use cursorPoint.X and cursorPoint.Y to get your numbers. You might find that the Y axis is inverted, just use it as myYmouse = Screen.height - cursorPoint.Y