Unity UIElemens changing style.cursor in code

Viewed 577

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 the CursorStyle property 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. An alternative is to create my own CursorStyle and point it to the appropriate cursor textuer, but again, I can't seem to find where those are located.

According to these docs and the UIElement Debugger I know I can change the style of the cursors. I just can't figure out how to set it to one of those in code.

enter image description here

2 Answers

Take a look at the MouceCursor enum: https://docs.unity3d.com/ScriptReference/MouseCursor.html

"Custom mouse cursor shapes used with EditorGUIUtility.AddCursorRect."

It defines multiple cursor shapes, one of them is SplitResizeLeftRight. Usage example:

EditorGUIUtility.AddCursorRect(new Rect(20, 20, 140, 40), MouseCursor.SplitResizeLeftRight);

I hope this helps you.

By some miracle I stumbled upon this class: UIElementsEditorUtility

At the time of posting the class can not be found in Unity docs.

So here is an example of how to use it:

style.cursor = UIElementsEditorUtility.CreateDefaultCursorStyle(MouseCursor.ResizeHorizontal);
Related