Search Unity

Dynamic Cursor Change

Discussion in 'UI Toolkit' started by TheHeftyCoder, Mar 14, 2020.

  1. TheHeftyCoder

    TheHeftyCoder

    Joined:
    Oct 29, 2016
    Posts:
    91
    Is there any way to specify a cursor in code and ignore the element itself?

    I.e, I have a container of elements. Suppose each element is an asset. So, when I'm dragging an asset to that container, I want the cursor to change, depending on if you can add the asset or not (based on its type). Tried a lot of things, couldn't figure out how to do it reliably. Bare in mind that when I'm hovering over the elements, each element overrides the cursor, while I want it to stay the same if I'm dragging an asset over the container.

    Any thoughts would be appreciated. Some documentation or perhaps an easier way to change cursors and also utilize the different presets that Unity Editor would be super useful for something like this. It's weird if I can't find a way to change the cursor easily...
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    Try something like this:

    Code (CSharp):
    1. currentVisualElement.style.cursor = new UnityEngine.UIElements.Cursor() { defaultCursorId = (int)MouseCursor.FPS };
    2. currentVisualElement.style.cursor = new UnityEngine.UIElements.Cursor() { defaultCursorId = (int)MouseCursor.Text };
     
    Nexer8 likes this.
  3. Xan_9

    Xan_9

    Joined:
    Oct 7, 2020
    Posts:
    31
    defaultCursorId is internal in 2022.1 How to change the cursor this way currently?
     
    Last edited: Mar 17, 2022
    Nexer8 likes this.
  4. microbrewer

    microbrewer

    Joined:
    Jun 6, 2020
    Posts:
    12
    Bump. Anything?
     
  5. Maverick

    Maverick

    Joined:
    Dec 18, 2009
    Posts:
    240
    For runtime, I've managed to change cursor only via code:

    Code (CSharp):
    1.  
    2. var cursor = new UnityEngine.UIElements.Cursor();
    3. cursor.texture = Resources.Load<Texture2D>($"UI/Cursors/mt-pipette-24");
    4. cursor.hotspot = new Vector2(0, cursor.texture.height);
    5.  
    6. _palette.style.cursor = new StyleCursor(cursor);
    7.  
     
    PrimalCoder and microbrewer like this.