Unity编辑器 - 如何在编辑时停止字段变为蓝色

问题描述 投票:-1回答:1

我在Unity中创建一个工具,当您按下按钮时,可以为多个平台构建项目。

我从这个工具的首选项窗口开始,然后想出了一个令人讨厌的东西。每当我更改EnumPopup字段的枚举值时,该字段在编辑器窗口中变为蓝色。有没有办法禁用它?

This is the code I am using to show the enum field on the window

This is the window when the enum field has not been changed

This is the window when the enum field has been changed

看看第二张图片中的字段是不是蓝色,在第3张图片中字段是否变为蓝色?我该如何防止这种情况发生?

提前致谢!

unity3d unity-editor
1个回答
0
投票

没有剩下的代码就很难提供帮助。

这是Unity内置行为。我尝试了很多东西see here来禁用/覆盖前缀标签的内置着色但到目前为止没有运气。


然而,一个工作场所可能是使用独立的EditorGUI.LabelField,它不会受到EnumPopupEditorGUIUtility.labelWidth的影响:

var LabelRect = new Rect(
    FILEMANAGEMENT_ENUMFIELD_RECT.x, 
    FILEMANAGEMENT_ENUMFIELD_RECT.y,
    // use the current label width
    EditorGUIUtility.labelWidth, 
    FILEMANAGEMENT_ENUMFIELD_RECT.height
);

var EnumRect = new Rect(
    FILEMANAGEMENT_ENUMFIELD_RECT.x + EditorGUIUtility.labelWidth, 
    FILEMANAGEMENT_ENUMFIELD_RECT.y, 
    FILEMANAGEMENT_ENUMFIELD_RECT.width - EditorGUIUtility.labelWidth, 
    FILEMANAGEMENT_ENUMFIELD_RECT.height
);

EditorGUI.LabelField(LabelRect, "File relative to");
QuickBuilder.Settings.Relation = (QuickBuilder.Settings.PathRelation)EditorGUI.EnumPopup(EnumRect, QuickBuilder.Settings.Relation);

正如您所看到的,当宽度保持灵活时,标签不会变为蓝色

enter image description here


图片的标题说明 而不是像QuickBuilder.Settings.Relation =直接通过edito脚本设置值,你应该总是尝试使用正确的SerializedProperty。它处理撤消/重做等事情,并将相应的对象和场景标记为dirty

是否还有一个特殊的原因,你使用EditorGUI而不是EditorGUILayout?在后者中,您不需要设置Rects。

EditorGUILayout.BeginHorizontal();
{
    EditorGUILayout.LabelField("File relative to", GUILayout.Width(EditorGUIUtility.labelWidth));
    QuickBuilder.Settings.Relation = (QuickBuilder.Settings.PathRelation)EditorGUILayout.EnumPopup(QuickBuilder.Settings.Relation);
}
EditorGUILayout.EndHorizontal();
© www.soinside.com 2019 - 2024. All rights reserved.