Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cursor size

Discussion in 'Scripting' started by Epictickle, Dec 30, 2014.

  1. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    Hey guys,

    I'm trying to resize a custom cursor that I've put into my game. I've read on the forum that using CursorMode.Auto will automatically resize to what Unity wants your cursor size to be, so I set my cursor with CursorMode.ForceSoftware, but when I do that the cursor is completely off center.. It's center on the Y axis just fine, but it's wayyyy to the left on the X axis... Is this a bug? If not, how can I fix it?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    CursorMode determines how the cursor is rendered, not explicitly what size it is. Generally speaking, if you're targetting desktop platforms using Auto should be fine. It'll fallback to software rendering for you.

    Make sure your texture is set as Cursor in the import settings and that it is 32x32
     
  3. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    How do I resize it, though? Using CursorMode.Auto makes the cursor soo small. I need it to be bigger.

    Seems like setting it to Auto just forces the cursor to be 32x32
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Your cursor will just take the size of the texture2d. I've been using 128x128. To set the center, you use hotspot. It's in the docs.
     
  5. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    It isn't taking the size of the texture... I've changed the resolution in photoshop now about 10 times... It started as 128x128.. I'm not up to 1536 and the cursor is always 32x32 on the screen. Also, I have tried changing the max size value in the import settings. Still nothing.
     
    bossnass1995 likes this.
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    This is the code I'm using:
    Code (csharp):
    1.  
    2.     void cursorSet(Texture2D tex){
    3.         CursorMode mode = CursorMode.ForceSoftware;
    4.         xspot = tex.width/2;
    5.         yspot = tex.height/2;
    6.         Vector2 hotSpot = new Vector2(xspot,yspot);
    7.         Cursor.SetCursor(tex, hotSpot, mode);
    8.     }
    9.  
     
    cyanspark and Volorf like this.
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    32x32 is required. If you want to resize the cursor you need to resize the actual pixels of the texture (not the dimensions of the texture itself). Making it anything bigger than 32x32 via import settings or otherwise is just taking up memory.
     
  8. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    How can you set the pixels without losing its color, though? The Color[] size must be at least width*height, but how would you rebuild the color array to match how it looked to begin with?

    @fire7side , I've tried using ForceSoftware mode, but the cursor is never in the right spot. I can set the hotspot just fine, which from my knowledge is only where the ray is casted from the image to detect a mouse click, etc. I'll upload a pic to show you what's going on.


    WeirdCursor.png

    My actual mouse cursor is where the blue box is. I know this because it is originally white, and it changes color in an OnMouseEnter function. The cursor generated from SetCursor using ForceSoftware is way off to the left... It seems to be correct on the Y axis, but on the X axis it is definitely not working.
     
    Last edited: Dec 31, 2014
  9. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Resize the pixels via Photoshop. No reason to do it in code. What I mean (and it's surprisingly difficult to explain actually) is to resize the "image" not the dimensions of the canvas.

    I have questions about your "test". Are you changing the color to something other than blue with OnMouseExit? Have you logged the mouse coordinates to double check?
     
  10. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    OnMouseExit changes it back to white. It's funny because I have it set to if my mouse gets too close to the edge of the screen it will pan the camera in that direction (like an RTS game). When I get my actual mouse close to the edge of the screen, it pans accordingly. All I can do is see the cursor that is generated by SetCursor.. It definitely isn't lined up with my real mouse cursor. If I set it to CursorMode.Auto it is lined up perfectly, but one of my cursors are too small.

    And okay, I think I understand what you mean by resizing the pixels in photoshop. I'll give it a try.
     
  11. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    We've always used Auto. No reason to not use hardware rendering on desktop platforms. If a single texture isn't working then I would hazard to guess something is wrong with the texture or its import settings.
     
  12. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    CursorPosition.png

    The red circle is my cursor that is generated with SetCursor.. As you can see, it is at the bottom left of the screen.. Which SHOULD give me the coordinates of (0,0,0), or at least somewhere around that. But, as you can see in the red rectangle the coordinates are (462,85,0)
     
  13. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    Okay, I'll see about resizing the pixels in photoshop. Still not exactly sure what you mean lol, but I'll give it a shot.. How does one resize an image without resizing its canvas? xP
     
  14. Epictickle

    Epictickle

    Joined:
    Aug 12, 2012
    Posts:
    431
    Okay, so I went to Image > Image Size and tried to resize it that way.. Still no luck... Any links you could possibly throw me that could explain what you mean?
     
  15. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Control A to Select All. Edit -> Transform -> Scale.

    This scales the image inside the canvas.

    Edit: Also make sure the image is in the upper left corner of the canvas.
     
  16. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Hmm, I just have a laptop and software mode works fine. My cursor is 128x128. I suppose it's how much you are taxing the system. I send a ray through the cursor vector and it lines up perfectly with objects which is all I need, and I'd rather have larger cursors for my purpose.
     
    Last edited: Dec 31, 2014
  17. Velo222

    Velo222

    Joined:
    Apr 29, 2012
    Posts:
    1,437
    I'm having the same issue as Epictickle. Using "Hardware" mode for your custom mouse cursor forces your cursor to be 32x32, which is what Kelso is saying. But that's really tiny! If you're playing in 1920x1080 resolution, your mouse is hard to see when it's that small. I've made my texture as big as it possibly can be to fill up the 32x32 canvas and there's just no getting around the fact that the canvas size is locked at 32x32. How can people think this is an okay size? lol.

    There should be a way to resize your hardware custom cursor, but I have a feeling this would require messing with the operating system itself, and something apart from Unity.

    Hardware cursors perform and work the best, but you can't see them. And software cursors you can re-size and adjust settings, but they perform like poo. You can't win either way lol.
     
    TooManySugar likes this.
  18. TooManySugar

    TooManySugar

    Joined:
    Aug 2, 2015
    Posts:
    864
    Ok I was struggling with this stuff. Finally solved.
    Set cursor icon to "cursor mode" and Max size set the cursor size you'll be interested in.

    The place this script on the first scene of your game. In editor the size stuff works BUT the center spot for some reason is shifted, but in the build its ok, so enable to check visual look and size but dissable for menu navigation as you test it in editor.

    autoCenterHotSpot sets your cursor hotstopt in the center automatically.

    Hope this helps someone.


    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ui_Menu_Cursor : MonoBehaviour {
    6.  
    7. //depending on source image size <<i.e Photoshop  Edit-->ImageSize>> you need to adjuts the center of the mouse.
    8.    //public float centerX;
    9.    //public float centerY;
    10.  
    11.    public Texture2D cursorTexture;
    12.    public CursorMode cursorMode = CursorMode.Auto;
    13.    public bool autoCenterHotSpot = false;
    14.    public Vector2 hotSpotCustom = Vector2.zero;
    15.    private Vector2 hotSpotAuto ;
    16.  
    17.    void Start () {
    18.  
    19.  
    20.  
    21.      Vector2 hotSpot;
    22.      if ( autoCenterHotSpot) {
    23.        hotSpotAuto =  new Vector2(cursorTexture.width*0.5f, cursorTexture.height*0.5f);
    24.        hotSpot = hotSpotAuto;
    25.  
    26.      }
    27.      else {hotSpot = hotSpotCustom;}
    28.  
    29.      //Cursor.SetCursor (cursorTexture, new Vector2( centerX,centerY) ,CursorMode.ForceSoftware);
    30.      Cursor.SetCursor (cursorTexture, hotSpot ,CursorMode.ForceSoftware);
    31.  
    32.    }
    33. }
    34.  
    35.  
     
  19. Sylvan-Rover

    Sylvan-Rover

    Joined:
    Feb 1, 2014
    Posts:
    1
    I was having the same issue. To fix it, after replacing the cursor sprite, make sure to go back to the player settings and set it to none. Then reapply the cursor sprite. Not sure why it doesn't update automatically
     
  20. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Is this the way to get a high-performance hardware rendered cursor in a larger than 32x32 size?
     
  21. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,134
    Hardware cursor size limits are completely platform dependent. If you're on Windows, for example, there is no way to increase the hardware cursor beyond 32x32.
     
  22. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    That's a shame. And there's no way of overlaying a software sprite on a hardware cursors motion? The software cursor is really laggy and feels like it has acceleration or smoothing on.
     
  23. unity_x_qe4ZQcRm_jUg

    unity_x_qe4ZQcRm_jUg

    Joined:
    Sep 6, 2020
    Posts:
    2
    Yeah Blackthornprod's video in that topic should cover your question if you are still here in 2020... :)
    In his second technique he doesn't use a texture for the cursor, but he makes the cursor invisible and stick a sprite on top of it instead. That way, it is possible to create cursor animations, change the cursor sprite if clicking, make particle effects and of course scale it to infinity.
    Here is the video:
     
    Last edited: Sep 6, 2020
    reza_b_mirzaei likes this.
  24. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    399
    This is great, until you need it to work with UI because he has it set up with the sprite renderer.

    Any further thoughts on how to get a cursor over the UI?
     
  25. kloot

    kloot

    Joined:
    Mar 14, 2018
    Posts:
    77
    But isn't the video exactly what Zebbi pointed out - software rendered and therefore sluggish?
    I seem unable to use a hardware rendered cursor above 32x32 on windows - has anyone managed to do so?
     
  26. CleverElucidation

    CleverElucidation

    Joined:
    Apr 23, 2021
    Posts:
    1
    You could try Sebastian Lague's tutorial that uses a custom sprite and secondary camera to render the UI and use layers to control what's on top of what.

     
    reza_b_mirzaei likes this.
  27. kloot

    kloot

    Joined:
    Mar 14, 2018
    Posts:
    77
    Cool, but this is also following the mouse and thus will lag. The challenge is to change the size of the hardware cursor.
     
  28. AustinSyu

    AustinSyu

    Joined:
    Dec 5, 2018
    Posts:
    12
    I don't think you can make a software cursor without any lag.
    Unity needs at least 1 frame to get the position of the hardware cursor, so...it have to be laggy.
     
  29. DenisCooper09

    DenisCooper09

    Joined:
    Dec 19, 2021
    Posts:
    1
    Use FixedUpdate() instead, it will fix cursor jittering if camera is moving.
     
  30. alexandervitkov

    alexandervitkov

    Joined:
    Dec 25, 2021
    Posts:
    5
    As far as I can tell Unity limits the cursor size to 32x32. The fact that Unity's cursor code only works with Texture2Ds and not Sprites suggests it was last updated before Sprites were added, so like 10 years ago.

    To use a larger hardware-rendered cursor in our game we currently use platform-specific APIs.

    For Windows, the relevant functions are:
    https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-loadcursorfromfilew
    https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setcursor
    https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showcursor

    The LoadCursorFromFile function needs a .cur file in disk, there are probably other WinAPI functions to load one form memory if you prefer.

    I still haven't gotten around to Linux, and I really don't want to, since we'll probably have to handle both X11/Wayland, and I doubt Unity exposes a nice API for interfacing with them...