Search Unity

Changes to cursor not updating immediately

Discussion in 'UI Toolkit' started by WilliamEnders, Mar 3, 2020.

  1. WilliamEnders

    WilliamEnders

    Joined:
    Nov 10, 2015
    Posts:
    5
    I'm trying to create a cursor with multiple states, similar to what happens when flying around the scene view.
    I've got multiple USS classes set up for each cursor type and I'm swapping between them when holding down different modifier keys. Everything seems to be fine except the cursor won't update until it leaves the VisualElement and hovers back onto it. Is there something I'm missing?
     
  2. alexandred_unity

    alexandred_unity

    Unity Technologies

    Joined:
    Dec 12, 2018
    Posts:
    43
    Could you report a bug for it?
     
  3. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    102
    I found VisualElement's cursor will not updated until you move the mouse over some standard controller.
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.UIElements;
    4. using UnityEditor.UIElements;
    5.  
    6.  
    7. public class TestEditor : EditorWindow
    8. {
    9.     [MenuItem("Test/Cursor Not Updated")]
    10.     public static void ShowExample()
    11.     {
    12.         TestEditor wnd = GetWindow<TestEditor>();
    13.         wnd.titleContent = new GUIContent("TestEditor");
    14.     }
    15.     VisualElement clonedTree;
    16.     public void OnEnable()
    17.     {
    18.         // Each editor window contains a root VisualElement object
    19.         VisualElement root = rootVisualElement;
    20.  
    21.         // Import UXML
    22.         var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/TestEditor.uxml");
    23.         clonedTree = visualTree.CloneTree();
    24.         root.Add(clonedTree);
    25.         clonedTree.StretchToParentSize();
    26.         // A stylesheet can be added to a VisualElement.
    27.         // The style will be applied to the VisualElement and all of its children.
    28.         var styleSheet = AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Editor/TestEditor.uss");
    29.  
    30.         clonedTree.styleSheets.Add(styleSheet);
    31.  
    32.  
    33.         clonedTree.RegisterCallback<MouseDownEvent>(OnMouseDown);
    34.     }
    35.  
    36.     private void OnMouseDown(MouseDownEvent evt)
    37.     {
    38.         clonedTree.RemoveFromClassList("arrow-plus");
    39.         clonedTree.RemoveFromClassList("arrow-minus");
    40.  
    41.         if (evt.altKey)
    42.         {
    43.             clonedTree.AddToClassList("arrow-plus");
    44.         }
    45.         else if (evt.actionKey)
    46.         {
    47.             clonedTree.AddToClassList("arrow-minus");
    48.         }
    49.     }
    50.  
    51. }
    Code (CSharp):
    1. .arrow-plus {
    2.     cursor: arrow-plus;
    3. }
    4.  
    5. .arrow-minus {
    6.     cursor: arrow-minus;
    7. }
    Code (CSharp):
    1. <ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
    2.     <ui:Label text="Mouse Click blank space will change the cursor.&#10;with action key set it to arrow-plus,&#10;with alt key set it to arrow-minus&#10;no key down will reset the cursor.&#10;You must move the cursor over this label, &#10;then the root visualElement&apos;s cursor could be take effect!">
    3.         <Style src="/Assets/TestEditor.uss" />
    4.     </ui:Label>
    5. </ui:UXML>
    6.  
     
  4. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    102
    even i use reflect to update the cursor style, the style cursor on visualElement is odd!
    Code (CSharp):
    1.  
    2.             var elementType = typeof(VisualElement);
    3.             var methodUpdateCursorStyle = elementType.GetMethod("UpdateCursorStyle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    4.             methodUpdateCursorStyle?.Invoke(this, new object[] { MouseOverEvent.TypeId() });
     
  5. JoNax97

    JoNax97

    Joined:
    Feb 4, 2016
    Posts:
    611
    I'm also having issues with the cursor, not sure if related but here it goes:
    - Custom cursors only display if the pointer is directly under the element on which the property is assigned (either via uss class or directly via element.style). I understand that a 'inherit' behavior is missing. Assigning the cursor to * in uss works.

    - Custom cursors appears big, stretched and blurry. I didn't find a way to set the cursor size but I expect it to render at the same resolution than the rest of the elements.

    These issues appear both in editor windows and panel renderers, both in editor and build.

    Edit: Ok, apparently the cursor size limitations go beyond UI toolkit and depend on the OS. That leaves the first issue then.
     
    Last edited: Apr 8, 2020
    unity_3Vgvc20-AT9hew likes this.
  6. visose

    visose

    Joined:
    Nov 2, 2014
    Posts:
    28
    I run into the same issue. For now I'm using a work around by sending a MouseOverEvent to the VisualElement after changing the cursor:

    Code (CSharp):
    1. var panel = _root.Q(panelName);
    2. panel.AddToClassList(cursor);
    3. panel.SendEvent(new MouseOverEvent());
     
    LuJyKa likes this.