Search Unity

Bug `Cursor.visible` behaves strangely in the editor

Discussion in 'Scripting' started by MUGIK, May 22, 2021.

  1. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    481
    Emmmm,
    Cursor.visible
    behaves strangely in the editor.
    I enter playmode and focus on gameview.
    Through code I disable cursor by
    Cursor.visible = false;Cursor.lockState = Locked;


    I have a script that outputs cursor state every frame.

    Now I can see in console this message:
    "Cursor visible:False, lockState:Locked"

    When press Escape I can see cursor, and that's because of how unity works(docs):
     In the Editor the cursor is automatically reset when escape is pressed, or on switching applications.


    After pressing escape in the console I see the next message:
    "Cursor visible:True, lockState:Locked"
    That's already strange because I can see and freely move cursor, however, unity is saying that I can only see it o_O

    And when I try to click on gameview to focus back on game - cursor disappears, I can't see it anymore.
    BUT in console there is the same message logging:
    "Cursor visible:True, lockState:Locked"

    Any ideas on how to detect whether gameview in focus or not?
    Application.focusChanged
    works only when you pressed something outside gameview, and only after that click gameview.

    I'm making FPS game, and in editor I want to stop camera rotation when pressing escape.
    So in my camera script I checking for
    Cursor,visible
    and if it's visible then do not move camera.
    But with such Cursor behavior I can't do that:(
     
  2. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,326
    It seems like in CursorLockMode.Locked the cursor is only locked to the center of the game window and hidden when the window has focus, which would explain the behaviour you're seeing.

    It sounds like the simplest fix would be to have a script that simply sets Cursor.visible to false during every Update call in the editor.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class CursorLocker : MonoBehaviour
    4. {
    5.     private void Awake()
    6.     {
    7.         Cursor.visible = false
    8.     }
    9.  
    10.     #if UNITY_EDITOR
    11.     private void Update()
    12.     {
    13.         Cursor.visible = false
    14.     }
    15.     #endif
    16. }
     
  3. I don't really understand what is your goal here.
    In general, you shouldn't concern yourself with the editor state in your game state. You should act as if you had the control all along. Everything else is there for you in case you lock yourself out from the editor.
     
    Munchy2007 likes this.
  4. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    481
    When playing in editor I want to press escape and "freeze" the game (disable inputs, not setting timeScale=0) and move cursor somewhere else without changing camera's direction.
    So I just disabling camera rotation when cursor is visible. But Cursor.visible does not work properly and returns the wrong value. I've attached unitypackage so that you can reproduce this issue if you want. There is README with steps to reproduce.

    For now I come up with this workaround:
    I have a wrapper for a built-in Cursor class (I called it MCursor)
    All game code references MCursor.
    Only MCusor has access to the built-in Cursor and can set its values.
    Also, MCursor stores values provided by game code(visible and lockState).
    MCursor listens for
    Application.focusChanged
    event.
    When focus lost - MCursor stores current visible and lockState values.
    When focus regained - MCursor restores visible and lockState values to built-in Cursor.This resets cursor state to what was before leaving gameview.

    I'm using the new Input System.
    For it I also created a wapper, which disables inputs on focus lost.


    Anyway, there is a bug with the in-editor cursor, so I sent bug reports for Unity 2019. 2020, and 2021.
    Will see how it goes...
     

    Attached Files:

    Last edited: May 23, 2021
  5. MUGIK

    MUGIK

    Joined:
    Jul 2, 2015
    Posts:
    481
  6. halivudestevez

    halivudestevez

    Joined:
    May 12, 2019
    Posts:
    14
    "Note that in CursorLockMode.Locked mode, the cursor is invisible regardless of the value of this property." - this is indeed an unexpected behavior....
     
  7. xeosD

    xeosD

    Joined:
    Apr 6, 2023
    Posts:
    9
    Unity ignored his bug report, says it's by design. Perhaps that's how it should be(?) but there still exists a need to do what that poster wants, which is to lock the view when in editor mode when hitting escape, and then unlock it when clicking back into the game view. The solution proposed by MUGIK is a lot of work for such a simple thing. That said, care to share so we don't have to reimplement?
     
    Last edited: Dec 2, 2023