Search Unity

Unity UI Render Custom Mouse cursor on top of canvas

Discussion in 'UGUI & TextMesh Pro' started by mrCharli3, Jun 21, 2017.

  1. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I just created a custom mouse pointer:

    Code (CSharp):
    1. public class MousePointer : MonoBehaviour {
    2.  
    3.     void Start () {
    4.         Cursor.visible = false;
    5.     }
    6.    
    7.     void Update () {
    8.         Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    9.         transform.position = new Vector2(mousePos.x, mousePos.y);
    10.     }
    11. }
    Until now I've always wanted Canvas to be rendered on top of everything else, so I've used 'Render Mode = Screen Space Overlay'. I want my mouse cursor to be rendered on top of the Canvas, so I tried changing it to 'Render Mode = World Space' and setting it to Sorting Layer 'UI' which is on top of everything except the mouse cursor. However using this method my UI doesnt show up at all on camera, what am I doing wrong?

    PS: My mouse cursor is a bit stuttery when moving around, any fix for that?
     
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    I recommend having a mousepointer gameobject within canvas (at the very bottom).

    Higher framerate? update() gets called every frame, if that isn't enough you'll have to use something else.
     
  3. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    Hi, putting it at the very bottom of the Canvas didnt do the trick. Still renders behind the rest of my Canvas. Most likely because it has a Sprite Renderer.
    If I change it to a canvas Renderer+Image it stops behaving as it should so that didnt work either.

    I tried putting it in FixedUpdate but just doesnt feel "smooth", its barely noticable bot still a little bit.
     
    Last edited: Jun 21, 2017
  4. mrCharli3

    mrCharli3

    Joined:
    Mar 22, 2017
    Posts:
    976
    I tried another solution, I set the Cursor in Player settings (Edit->Project Settings->Player)

    But that gives this result:


    And my cursor texture is just a normal looking cursor. Not that weird thing in the Gif, not sure what is happening there.

    When I build the mouse turns into a black square, maybe the problem is with my texture? but its the same as every other texture
     
    Last edited: Jun 21, 2017
  5. Reijii

    Reijii

    Joined:
    Jul 22, 2015
    Posts:
    1
    In image settings change compressed to truecolor
     
  6. jgalarza_unity

    jgalarza_unity

    Joined:
    Jul 19, 2019
    Posts:
    2
    The Texture should be imported as Cursor instead of Sprite 2D