如何将VisualElement的游标样式设置为代码中的预定义类之一?(箭头、文本、左转调整大小等)
我知道可以使用CursorStyle属性style.cursor = SomeCursorStyle;
设置游标。
问题是我想要使用SplitResizeLeftRight游标,而且我似乎找不到静态类的定义位置。显然,我可以通过样式表来完成这个任务,但是我想通过代码来完成。另一种选择是创建自己的CursorStyle并将其指向适当的游标文本处理程序,但是,我似乎无法找到它们所在的位置。
根据这些文档和UIElement调试器,我知道我可以改变游标的样式。我只是想不出怎么把它设置成代码中的一个。
发布于 2019-10-22 19:59:57
奇迹的是,我偶然发现了这门课:UIElementsEditorUtility
在发布时,在Unity中找不到该类。
下面是一个如何使用它的例子:
style.cursor = UIElementsEditorUtility.CreateDefaultCursorStyle(MouseCursor.ResizeHorizontal);
发布于 2022-11-01 17:08:44
UIElementsEditorUtility类不再存在。我找到了一个最新版本的统一解决方案(在我的例子中是2022.1.7),但是它包含了一些C#反射.
//using System.Reflection;
public static void SetCursor(this VisualElement element, MouseCursor cursor)
{
object objCursor = new Cursor();
PropertyInfo fields = typeof(Cursor).GetProperty("defaultCursorId", BindingFlags.NonPublic | BindingFlags.Instance);
fields.SetValue(objCursor, (int)cursor);
element.style.cursor = new StyleCursor((Cursor)objCursor);
}
我希望它能帮助那些有同样问题的人。
发布于 2019-10-22 19:23:24
看看MouceCursor enum:https://docs.unity3d.com/ScriptReference/MouseCursor.html
“与EditorGUIUtility.AddCursorRect一起使用的自定义鼠标光标形状。”
它定义了多个游标形状,其中一个是SplitResizeLeftRight.。用法示例:
EditorGUIUtility.AddCursorRect(new Rect(20, 20, 140, 40), MouseCursor.SplitResizeLeftRight);
希望这能帮到你。
https://stackoverflow.com/questions/58510180
复制相似问题