Search Unity

Bug Cursor.SetCursor seems to display the wrong texture in software mode

Discussion in 'Scripting' started by Mloren, Aug 7, 2021.

  1. Mloren

    Mloren

    Joined:
    Aug 20, 2011
    Posts:
    83
    Hi all,

    I'm trying to set custom cursors from code, I have 50 different cursors I need to change between depending on the context.
    My code works fine if I use a hardware cursor but since that's limited to 32x32 pixels and I need larger textures, it's not an option.

    So what's happening is that when I set the texture, the wrong cursor is showing. I've debugged it and this seems to be a bug in Unity, I'm wondering if anyone has found a work around.

    To make sure it really was the cursor that's the problem, here's my test: I put all the cursors in an array, I have an index I can increase and decrease by key press so I can cycle through all of them.
    In the Update() I apply the same texture to a quad in the scene and to the cursor to compare the result:
    Code (CSharp):
    1. Texture2D texture = textureList[index];
    2.  
    3. //Apply to quad
    4. GameObject obj = GameObject.Find("TestCursor");
    5. Renderer render = obj.GetComponent<Renderer>();
    6. render.material.SetTexture("_MainTex", texture);
    7.  
    8. //Apply to cursor
    9. Cursor.SetCursor(texture, m_v2HotSpot, CursorMode.ForceSoftware);
    Sometimes it works fine:
    (The one of the left is the quad, the one on the right is the cursor)
    upload_2021-8-7_14-6-17.png

    Sometimes it's completely wrong:
    (The quad on the left is correct, the cursor on the right is displaying the wrong image)
    upload_2021-8-7_14-6-46.png

    But I don't understand how this is possible, they are using the same texture. Also this doesn't happen if I change the cursor to hardware mode.

    Anyone know why this might be happening or how I can work around it?
    (I'm using the old 3D/Fixed Render Pipeline if that makes any difference)
     
  2. mikeohc

    mikeohc

    Joined:
    Jul 1, 2020
    Posts:
    215
    If the goal is to change the look of the cursor, could you use an UI image component that move to the mouse position to achieve a similar thing?
     
  3. Mloren

    Mloren

    Joined:
    Aug 20, 2011
    Posts:
    83
    Well I fixed it... kind of...
    I restarted Unity and the problem went away for a while... then it came back, so I restarted again and it went away again... hasn't come back yet.
    Must be some corrupt metadata or something. Hopefully the bug doesn't reappear.
     
  4. Mloren

    Mloren

    Joined:
    Aug 20, 2011
    Posts:
    83
    It came back :(
    I'm also loading the cursor textures through the AddressibleAsset system and I'm wondering if that has something to do with it.
     
  5. eddiere

    eddiere

    Joined:
    Apr 13, 2020
    Posts:
    1
    I have the same issue. Cursor.SetCursor() is displaying the wrong Texture2D, and only sometimes. I also am using the old pipeline, also haven't found a pattern as to what causes it. I'm not pulling in the texture from AddressibleAssets; I'm just dragging it in the inspector.
     
  6. JulenScarecrowStudio

    JulenScarecrowStudio

    Joined:
    Jan 17, 2018
    Posts:
    20
    This keeps happening in 2021.3.18f1 and loading the Texture2D via the addressables system.

    I've checked the texture in VS, it's pointing at the correct one. I've saved the Texture2D in runtime as a png, it's the correct one, but the displayed cursor isn't the proper one.

    I'm not able to detect what causes it, it's not consistent.
     
  7. Pitou22

    Pitou22

    Joined:
    Sep 23, 2015
    Posts:
    59
    I just had the same problem in the editor, version 2022.3.5f1.
    To change the cursor I simply use:
    Code (CSharp):
    1. UnityEngine.Cursor.SetCursor(myTexture, Vector2.zero, CursorMode.Auto);
    The cursor was changing but not to the correct texture. It didn't happened at the beginning, but only after playing a little (something like 2 minutes).

    I have seen an new issue reported for this: https://issuetracker.unity3d.com/is...n-using-cursor-dot-setcursor-in-software-mode
     
    Last edited: Aug 25, 2023
    Glader and JulenScarecrowStudio like this.
  8. Newfa_Games

    Newfa_Games

    Joined:
    Oct 16, 2017
    Posts:
    15
    This worked for me, but using CursorMode.Auto made the cursor bigger for some reason, changing to CursorMode.ForceSoftware made it work. Weird bug, it's weird that it has persisted for 2 years.