我正在try 制作一个自定义判断器,其功能适用于所有类型的内置组件.

What I'm really trying to do is get as a string the name of the property selected in blue in the inspector when I select it. Is there any way? Example: When highlighting Local Position, is there any way to get "Transform Component - LocalPosition (0,0,0); Unity Editor highlighted element

我试着阅读API,想知道是否有什么东西可以帮助我,但没有任何运气.还try 使用CustomEditor(TypeOf(Transform)),但在突出显示时无法访问值.

任何帮助都将不胜感激!

推荐答案

FocusInEvent个对你有用吗?

您必须 for each VisualElement或通过根循环添加一个事件,并找到所有VisualElement并将事件添加到其中.然后,您可以获得VisualElement的名称,并在处理事件时使用该名称.

从关于focus events的文档中

public void CreateGUI()
{
    TextField textField = new TextField();
    textField.value = placeHolderText;
    rootVisualElement.Add(textField);

    textField.RegisterCallback<FocusInEvent>(OnFocusInTextField);
    textField.RegisterCallback<FocusOutEvent>(OnFocusOutTextField);
}

private void OnFocusInTextField(FocusInEvent evt)
{
    // If the text field just received focus and the user might want to write
    // or edit the text inside, the placeholder text should be cleared (if active)
    if (placeHolderMode)
    {
        var textField = evt.target as TextField;
        textField.value = "";
    }
}

我已经添加了一个工作示例,正如您所说,它似乎不适用于编辑器.编辑器实现在我的unity版本(2020.3.30f1)中使用以下代码.

using UnityEngine;

public class FocusScript : MonoBehaviour
{

}



using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

[CustomEditor(typeof(FocusScript))]
public class CustomEditorFocus : Editor
{
    public override VisualElement CreateInspectorGUI()
    {
        VisualElement myInspector = new VisualElement();

        TextField textField = new TextField();
        textField.name = "test text field";
        textField.value = "test";
        textField.label = "test label";
        myInspector.Add(textField);

        textField.RegisterCallback<FocusInEvent>(OnFocusInTextField);
        return myInspector;
    }

    private void OnFocusInTextField(FocusInEvent evt)
    {
        var textField = evt.target as TextField;
        Debug.Log("Testing with field: " + textField.name);
    }
}

Csharp相关问答推荐

循环访问Android视图中动态创建的子视图

如何使用Automapper映射两个嵌套列表

我无法在Ubuntu下编译使用microsoft.extension.configurationbuilder jsonapi和mono mcs的c#应用程序

如何从泛型方法返回一个可为空的T,其中T:notnull?

为什么EF Core 6会针对null验证Count(*)?

有没有办法使.NET 6应用程序在特定的.NET 6运行时版本上运行

MS Graph v5.42.0:在寻呼消息时更改页面大小

附加标题不起作用,而添加则起作用

如何将MongoDB序列化程序设置为内部对象属性

共享暂存/生产环境中Azure事件中心的建议配置

如何在C#中将方法/线程启动传递给基本构造函数

C#普罗米修斯指标

Blazor Web App WASM的两个独立项目令人困惑

当索引和外键是不同的数据类型时,如何设置导航属性?

按需无缝转码单个HLS数据段

记录类型';==运算符是否与实现IEquatable<;T&>;的类中的';equals&>方法执行等价比较?

C#按名称从类获取属性值类型<;t>;,我需要反射吗?

在同一个捕获中可以有多种类型的异常吗?

如何在C#中从MongoDB IPipelineStageDefinition中获取聚合命令的字段/选项?

如何在C#控制台应用程序中获取用户输入并将其作为订单项目进行处理?