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

UIElements style.cursor in code

Discussion in 'UI Toolkit' started by EvgenyVasilyev, Oct 22, 2019.

  1. EvgenyVasilyev

    EvgenyVasilyev

    Joined:
    Jul 5, 2019
    Posts:
    7
    I tried searching the forums, the docs, and the internet to no avail.

    How do I set the cursor style for a VisualElement to one of the predefined classes in code? (Arrow, Text, Split Resize Left Down, etc)

    I am aware the cursor can be set using style.cursor = SomeCursorStyle;
    The problem is I want to use the SplitResizeLeftRight cursor and I can't seem to find where the static classes are defined. Obviously I can do this though a stylesheet, but I want to do it through code.


     
    Last edited: Oct 22, 2019
  2. EvgenyVasilyev

    EvgenyVasilyev

    Joined:
    Jul 5, 2019
    Posts:
    7
    Well, I figured it out. For anyone else who ever runs into this issue:

    Code (CSharp):
    1. style.cursor = UIElementsEditorUtility.CreateDefaultCursorStyle(MouseCursor.ResizeHorizontal)
     
  3. Djayp

    Djayp

    Joined:
    Feb 16, 2015
    Posts:
    114
    Damn... it's internal... and it's gone in 2019.3...
     
    qcbf1 likes this.
  4. pstudio

    pstudio

    Joined:
    Apr 20, 2014
    Posts:
    12
    What I ended up doing was to create a style class in USS for each cursor I needed. Then I would call AddToClassList/RemoveFromClassList to set the desired cursor.
     
  5. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,203
    Using USS styles is the recommended approach for assigning default cursors. Most cases of default cursors are while hovering anyway, which uses the :hover pseudo state, and pseudo states are equally not accessible in C#.
     
    EZaca and EvgenyVasilyev like this.
  6. tonycoculuzzi

    tonycoculuzzi

    Joined:
    Jun 2, 2011
    Posts:
    301
    I feel like this should be accessible through the C# API. It'd be handy if both USS and C# functionality matched
     
    Last edited: Dec 18, 2020
  7. qcbf1

    qcbf1

    Joined:
    Nov 1, 2013
    Posts:
    19
    help... unity 2020 how work in csharp
     
  8. microbrewer

    microbrewer

    Joined:
    Jun 6, 2020
    Posts:
    12
    Hi, I am a bit lost with this in Unity 2021.3. When I set a cursor via USS, all I can specify in UIBuilder is a texture. How do I change its size and hotspot? Or how do I specify a standard cursor type like "resize horizontal"?
     
    Last edited: Jun 27, 2022
  9. KamilCSPS

    KamilCSPS

    Joined:
    May 21, 2020
    Posts:
    376
    Bumping this - anyone?
     
    Idioismist and sjompheCSPS like this.
  10. EZaca

    EZaca

    Joined:
    Dec 9, 2017
    Posts:
    31
    This still seems to be a relevant issue since 2019. For example, I can't define Unity's built-in cursors in UIBuilder even with Editor Extensions Authoring enabled. So it seems the USS is the only way to achieve that,

    I don't know if this was valid back in 2019 when this quote was written, or if I misinterpreted it, but in CSS, for example, and probably USS too, if you set the cursor to the element, it becomes the cursor for the element when hovering. In other words, there is no need for pseudo states nor they are relevant in this scenario.
     
  11. Ozgekocaoglu

    Ozgekocaoglu

    Joined:
    Feb 13, 2019
    Posts:
    38
    I found when i was looking for an answer and here is how you can change cursor via code:


    I've created a button in c#. Like that:
    Code (CSharp):
    1.       Button reimportMaterialsButton = new Button();
    2.       reimportMaterialsButton.name = "reimportMatButton";
    And i've added this class to the my uss file:

    Code (Boo):
    1. #reimportMatButton {
    2.     min-width: 100;
    3.     flex-grow : 1;
    4.     cursor: link;
    5. }
    6.  
    To detailed answer, you can check out this documentation:

    https://docs.unity.cn/Manual/UIE-USS-SupportedProperties.html#cursor
     
    Last edited: Oct 5, 2023
  12. EZaca

    EZaca

    Joined:
    Dec 9, 2017
    Posts:
    31
    Sure it works, but it is via USS, not via code. Instead of setting the name of the element, which is useful for other purposes, I suggest you add a class:

    Code (CSharp):
    1. reimportMaterialsButton.AddToClassList("cursor-link");
    In your USS you would do:
    Code (CSharp):
    1. .cursor-link{
    2.   cursor: link;
    3. }
    So you isolate the cursor for elements that use it, without affecting any other aspect of the element.

    It still have some problems, like how you will attach the USS to the element or the root, but it's an acceptable workaround.