Search Unity

Custom cursor is not visible on second monitor in multi-display setup

Discussion in 'Immediate Mode GUI (IMGUI)' started by ivanshv, Jul 23, 2019.

  1. ivanshv

    ivanshv

    Joined:
    Apr 13, 2018
    Posts:
    11
    Hi,

    short preamble. As I wasn't able to set the mouse cursor control with a joystick axis (which is essential for my project), I had to create a custom cursor with the code below. In a few words, my app is very simple, the user obtains the display coordinates on mouse click and writes them to csv file.
    And everything is working absolutely fine. But for one thing, the cursor is not visible on second display. I can obtain needed coordinates, but I don't see the pointer. I was trying to put a canvas with a panel rendered to Display2 (scree Space - Overlay), but it doesn't help. On the first display everything is perfect, but when I move the cursor to the second one it is not drown.

    This is the very final step of my project, would be glad to hear any advice. Thanks.

    Code (CSharp):
    1.  
    2. public Texture2D cursorImage;
    3.  
    4. private int cursorWidth = 70;
    5. private int cursorHeight = 70;
    6. public float horizontalSpeed = 2.0F;
    7. public float verticalSpeed = 2.0F;
    8. private Vector2 cursorPosition;
    9.  
    10. private void Start()
    11. {
    12.     Cursor.visible = false;
    13.     cursorPosition = new Vector2(Screen.width/2f, Screen.height/2f);
    14. }
    15.  
    16. private void OnGUI()
    17. {
    18.  
    19.     float h = horizontalSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
    20.     float v = verticalSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
    21.  
    22.     cursorPosition.x += h;
    23.     cursorPosition.y += v;
    24.  
    25.     GUI.DrawTexture(new Rect(cursorPosition.x, Screen.height - cursorPosition.y, cursorWidth, cursorHeight), cursorImage);
    26. }
    27.