Search Unity

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

Discussion in 'Input System' started by amarillosebas, Aug 8, 2020.

  1. amarillosebas

    amarillosebas

    Joined:
    Feb 28, 2013
    Posts:
    44


    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?
     
  2. Klausbdl

    Klausbdl

    Joined:
    Aug 12, 2013
    Posts:
    64
    bump. Same situation here, I'm also developing a vtuber app.
     
  3. Klausbdl

    Klausbdl

    Joined:
    Aug 12, 2013
    Posts:
    64
    Found the solution:

    Code (CSharp):
    1. [DllImport("user32.dll")]
    2. [return: MarshalAs(UnmanagedType.Bool)]
    3. private static extern bool GetCursorPos(out System.Drawing.Point lpPoint);
    4.  
    5. private void OnGUI()
    6. {
    7.     System.Drawing.Point cursorPoint;
    8.     GetCursorPos(out cursorPoint);
    9.  
    10.     Rect labelRect = new Rect(200, 100, 600, 1000);
    11.     GUI.color = Color.yellow;
    12.     GUI.skin.label.fontSize = 24;
    13.     string debugtext = "";
    14.     debugtext += $"{cursorPoint.X} {cursorPoint.Y}";
    15.  
    16.     GUI.Label(labelRect, debugtext);
    17. }
    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