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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Different cursor size on different resolutions?

Discussion in 'Editor & General Support' started by helgarddebarros, Mar 26, 2018.

  1. helgarddebarros

    helgarddebarros

    Joined:
    May 10, 2014
    Posts:
    169
    In my game my cursor is a texture 32x32 pixels. It looks fine on a 1920 by 1080 display.

    But one of my beta testers is on a 2560x1440 at 120Hz display setting, and the cursor stays at 32x32, which is way too small for that resolution.

    Is there any way for Unity to adjust the cursor size depending on the screen resolution used? I have read a few threads and they explain how to change the cursor size, but none of them explain how that could be done depending on the screen resolution. It seems to me like the cursor size is hard wired to 32 x 32.

    I suppose that the reverse will be true as well, if someone is on a lower resolution, the cursor will then appear too big on the screen.
     
  2. helgarddebarros

    helgarddebarros

    Joined:
    May 10, 2014
    Posts:
    169
    So, I have been thinking about this problem. It might be a simpler solution instead of trying to change the cursor size, what about just changing the image according to the screen resolution.

    So you set the cursor size to 64 x 64, and you have a range of images. If the screen resolution is 1920 by 1020 you use image 1, and if it is 2560 x 1440 you use image 2. Unity already has the ability to change cursor images, even if you can't change the size.

    So a range of images that are dependent on the screen resolution might be an easier fix for this problem?
     

    Attached Files:

    • _11.png
      _11.png
      File size:
      5 KB
      Views:
      913
    • _22.png
      _22.png
      File size:
      9.8 KB
      Views:
      886
  3. FirstTimeCreator

    FirstTimeCreator

    Joined:
    Sep 28, 2016
    Posts:
    768
    Detect the resolution and adjust accordingly. I'm not sure about an "automated way" but you can easily write a c# class to handle it.
     
  4. helgarddebarros

    helgarddebarros

    Joined:
    May 10, 2014
    Posts:
    169
    But adjust what? The size? I can see no way to change the cursor size at runtime. So that is what I am asking. Has anyone else run into this problem and how did they solve it? By adjusting the cursor size or by using different images depending on the screen resolution.
     
  5. FirstTimeCreator

    FirstTimeCreator

    Joined:
    Sep 28, 2016
    Posts:
    768
    https://forum.unity.com/threads/cursor-size.288333/
     
  6. helgarddebarros

    helgarddebarros

    Joined:
    May 10, 2014
    Posts:
    169
    Yeah, I read that thread, and they have the same problem. It is locked to 32 x 32, and the options to increase the size are not dynamic, once you change it to another size, like 64 x 64, then that is the size, you can't change it during run time back to 32 x 32.
     
  7. RulioOW

    RulioOW

    Joined:
    May 27, 2018
    Posts:
    7
    I also encountered this problem and decided to work around it with a script using sprites, which opens the door to more customizations depending on the needs of your game.

    Put this script on an empty gameobject in your first scene :
    Edit: Set the gameobject as a child of your camera and set rotation to 0.
    (you can add a sprite shader that draw the sprite on top of everything, i found this solution here : https://forum.unity.com/threads/render-sprite-in-front-of-everything.580945/)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(SpriteRenderer))]
    4. public class CursorManager : MonoBehaviour
    5. {
    6.     // Sprites/colors IDs : 0, 1, ...
    7.     [SerializeField] private Sprite mainCursor, clickCursor;
    8.     [SerializeField] private Color mainColor, clickColor;
    9.  
    10.     [SerializeField] private bool getClicks = true;
    11.  
    12.     private bool cameraMoving;
    13.  
    14.     private Transform t;
    15.     private SpriteRenderer sr;
    16.     private Vector3 mousePosToWorld;
    17.     private float zPos;
    18.  
    19.     private Vector3 baseScaleCursor;
    20.  
    21.     void Start()
    22.     {
    23.         t = transform;
    24.         baseScaleCursor = t.localScale;
    25.         sr = GetComponent<SpriteRenderer>();
    26.         sr.sprite = mainCursor;
    27.         sr.color = mainColor;
    28.  
    29.         // Set the sortingOrder value so that the cursor is displayed on top of everything
    30.         sr.sortingOrder = 10;
    31.  
    32.         zPos = Camera.main.nearClipPlane + 1f;
    33.         Cursor.visible = false;
    34.         //SetCursor(0.12f);
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         if (!cameraMoving)
    40.         {
    41.             Move();
    42.         }
    43.  
    44.         // Change color or sprite or both on click
    45.         if (getClicks)
    46.         {
    47.             if (Input.GetMouseButtonDown(0))
    48.             {
    49.                 //ChangeColor(1);
    50.                 ChangeSprite(1);
    51.             }
    52.             else if (Input.GetMouseButtonUp(0))
    53.             {
    54.                 //ChangeColor(0);
    55.                 ChangeSprite(0);
    56.             }
    57.         }
    58.     }
    59.  
    60.     // If the cursor trembles when the camera moves:
    61.     // Set bool cameraMoving to true and call this function just after the Camera movement
    62.     public void Move()
    63.     {
    64.         mousePosToWorld = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, zPos));
    65.         t.position = mousePosToWorld;
    66.     }
    67.  
    68.     void ChangeColor(int colorID)
    69.     {
    70.         sr.color = GetColor(colorID);
    71.     }
    72.  
    73.     Color GetColor(int colorID)
    74.     {
    75.         if (colorID == 0)
    76.         {
    77.             return mainColor;
    78.         }
    79.         else if (colorID == 1)
    80.         {
    81.             return clickColor;
    82.         }
    83.         else
    84.         {
    85.             return Color.white;
    86.         }
    87.     }
    88.  
    89.     void ChangeSprite(int spriteID)
    90.     {
    91.         sr.sprite = GetSprite(spriteID);
    92.     }
    93.  
    94.     Sprite GetSprite(int spriteID)
    95.     {
    96.         if (spriteID == 0)
    97.         {
    98.             return mainCursor;
    99.         }
    100.         else if (spriteID == 1)
    101.         {
    102.             return clickCursor;
    103.         }
    104.         else
    105.         {
    106.             return null;
    107.         }
    108.     }
    109.  
    110.     /// <summary>
    111.     /// Hide cursor or redisplay it as it was before hiding it.
    112.     /// </summary>
    113.     /// <param name="visible"></param>
    114.     public void SetCursor(bool visible)
    115.     {
    116.         if (!visible)
    117.         {
    118.             t.localScale = Vector3.zero;
    119.         }
    120.         else
    121.         {
    122.             t.localScale = baseScaleCursor;
    123.         }
    124.     }
    125.  
    126.     /// <summary>
    127.     /// Set cursor scale.
    128.     /// </summary>
    129.     /// <param name="scaleAmount">xAmount will be multiplied by Vector3.one</param>
    130.     public void SetCursor(float scaleAmount)
    131.     {
    132.         t.localScale = Vector3.one * scaleAmount;
    133.         t.localScale = new Vector3(
    134.             Mathf.Clamp(t.localScale.x, 0f, 10f),
    135.             Mathf.Clamp(t.localScale.y, 0f, 10f),
    136.             1f);
    137.         baseScaleCursor = t.localScale;
    138.     }
    139.  
    140.     public bool IsCursorVisible()
    141.     {
    142.         return t.localScale == Vector3.zero ? false : true;
    143.     }
    144.  
    145.     public void PauseInputs(bool pause)
    146.     {
    147.         getClicks = !pause;
    148.     }
    149.  
    150.     /*public void OnSceneTransition()
    151.     {
    152.         // TODO : Play an Animation or change cursor;
    153.         // SetCursor(false);
    154.         // getClicks = false;
    155.     }*/
    156.  
    157.     void OnApplicationFocus(bool focus)
    158.     {
    159.         if (focus)
    160.         {
    161.             Cursor.visible = false;
    162.         }
    163.     }
    164. }
    If your cursor is already too big or too small with the gameobject scale of 1 ou can change the pixels per unit of the image.
     
    Last edited: Feb 14, 2022
  8. BakoGames

    BakoGames

    Joined:
    Aug 28, 2017
    Posts:
    3
    Hello @RulioOW Where did you get this script from? When I add it to the empty game object It seems to work except that sprite image seems to scale depending on cursors position. I know now that script disagrees with "Cinemachine" cameras. Could you help me combine those two or link me to the ones who could? Thank you in advance.
     
    Last edited: Jan 31, 2020
  9. RulioOW

    RulioOW

    Joined:
    May 27, 2018
    Posts:
    7
    Sorry for the extra late reply, i edited my post, i was working on a 2D project so i never had this problem, a sprite renderer is in world position so you had to set it as a child of your camera and reset the rotation