Search Unity

Issue with Cursor.SetCursor / Default Cursor

Discussion in 'Scripting' started by plumphelmet, May 20, 2019.

  1. plumphelmet

    plumphelmet

    Joined:
    Apr 18, 2013
    Posts:
    8
    I have an issue with Cursor.SetCursor which appears to also exist when using the default cursor. I've had a look around and it looks like this is an issue that has existed since forever – https://forum.unity.com/threads/cursor-setcursor-problem.247512/ for example.

    Surely, with the amount of games that are made with Unity, there must be a solution to this issue. I'm testing on Mac at the moment. Sometimes, the cursor works fine, both with default cursor set and with a very simple class that looks like this:

    Code (CSharp):
    1. public class SetCursor : MonoBehaviour
    2. {
    3.     Vector2 _hotspot = new Vector2(9, 7);
    4.     Texture2D _texture;
    5.  
    6.     void Awake()
    7.     {
    8.         _texture = Resources.Load<Texture2D>("Textures/UI/cursor");
    9.     }
    10.  
    11.     void Update()
    12.     {
    13.         SoundManager.instance.PlaySingle("blip");
    14.         Cursor.SetCursor(_texture, _hotspot, CursorMode.Auto);
    15.     }
    16. }
    17.  
    (Note: the sound plays so I know the code is running, and even though it's running on every update, the cursor remains the default OS cursor.)

    I noticed that often, the cursor was reset per scene. But, sometimes it is reset during a scene, and SetCursor then has no effect.

    Has anyone encountered this issue before (and successfully solved it) or even better, does anyone use custom cursors in a way that differs from the above or from using the default cursor setting?

    Thank you
     
  2. plumphelmet

    plumphelmet

    Joined:
    Apr 18, 2013
    Posts:
    8
    I've tested this on the Windows build and it doesn't seem to be an issue.
     
  3. plumphelmet

    plumphelmet

    Joined:
    Apr 18, 2013
    Posts:
    8
    I've submitted a bug report including a project that reproduces this, but it occurs even when Cursor.lockState is set to CursorLockMode.Locked, so it's nothing to do with movement or the 'find my cursor' issue.
     
  4. plumphelmet

    plumphelmet

    Joined:
    Apr 18, 2013
    Posts:
    8
    For what it's worth, for anyone in the future suffering from this, my solution is to have an image within the Canvas object which has a script attached which simply sets the visibility of the cursor to false and then sets the position of the image (acting as a cursor) to the mouse position.

    Code (CSharp):
    1. public class CustomCursor : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     GameObject CursorObject;
    5.  
    6.     void Awake()
    7.     {
    8.         Cursor.visible = false;
    9.     }
    10.  
    11.     void LateUpdate()
    12.     {
    13.         CursorObject.transform.position = Input.mousePosition;
    14.     }
    15. }
    This is going to have to do.
     
  5. plumphelmet

    plumphelmet

    Joined:
    Apr 18, 2013
    Posts:
    8
    Welp. This doesn't work because on mac OS you can't even control the cursor once Unity loses it's hold.

    It's almost as though there is a reference somewhere that gets changed, and Cursor no longer refers to it.
     
  6. Blenderik

    Blenderik

    Joined:
    May 14, 2013
    Posts:
    146
    CursorObject.transform.position = Input.mousePosition; doesn't seem right. let's say the mouse is in the middle of an HD screen, x = 900, y = 500, z = 0 then your object would be over 1km away from the center of your scene.