Unity Editor:如何调整检查器 属性 字段的大小和 space 值字段?
Unity Editor : How to resize and space value field of an inspector property field?
我正在尝试实现我的第一个统一编辑器代码,以制作一个自定义数组 属性 抽屉,其中包含缩进的命名条目以及缩进和动态调整大小的值字段。
我使用以下简单的 git 解决方案作为我的代码的基础,它允许在检查器中设置数组的标签:HERE
替换 gitHub 解决方案中显示的示例,我正在使用此枚举作为我的数组元素名称容器:
[System.Serializable]
public enum HealthNames
{
General,
Head,
Body,
RightArm,
LeftArm,
Rightleg,
leftLeg,
}
并以单一行为将其设置在数组上 class :
[ LabeledArray( typeof( HealthNames ) ) ]
public int[] m_aHealth = new int[ Enum.GetNames( typeof( HealthNames ) ).Length ];
我在try语句的开头和结尾添加了EditorGUI.indentLevel++;
和EditorGUI.indentLevel--;
来缩进数组元素的标签,使它们从大小属性中脱颖而出。
从那里开始,我搜索了在元素的值字段上添加缩进或从大小 属性 的值字段中删除缩进的方法。但使用 EditorGUI
找不到答案
我还希望能够动态调整所有值字段的大小,但同样,仅使用 EditorGUI 也没有得到任何答案。无法在 属性 字段上使用 EditorStyle.AnyField.WordWrap = true;
。将 PropertyField
传递给 IntField
,使用 EditorStyles.NumberField
并预先将 wrodwrapping 设置为 true 的设置无效。
我也发现了几年前的少量 EditorGUILayout
,但由于解决方案是使用 EditorGUI
从头开始构建的,所以它们不起作用
望各位指教
如果我对你的理解正确,你希望标签 和 值字段缩进。
我认为可以像这样
private const int INDENT = 15;
public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
var fieldsRect = new Rect(rect.x + INDENT, rect.y, rect.width - INDENT, rect.height);
try
{
var path = property.propertyPath;
int pos = int.Parse(path.Split('[').LastOrDefault().TrimEnd(']'));
EditorGUI.PropertyField(fieldRect, property, new GUIContent(ObjectNames.NicifyVariableName(((LabeledArrayAttribute)attribute).names[pos])), true);
}
catch
{
EditorGUI.PropertyField(fieldRect, property, label, true);
}
EditorGUI.EndProperty();
}
我正在尝试实现我的第一个统一编辑器代码,以制作一个自定义数组 属性 抽屉,其中包含缩进的命名条目以及缩进和动态调整大小的值字段。
我使用以下简单的 git 解决方案作为我的代码的基础,它允许在检查器中设置数组的标签:HERE
替换 gitHub 解决方案中显示的示例,我正在使用此枚举作为我的数组元素名称容器:
[System.Serializable]
public enum HealthNames
{
General,
Head,
Body,
RightArm,
LeftArm,
Rightleg,
leftLeg,
}
并以单一行为将其设置在数组上 class :
[ LabeledArray( typeof( HealthNames ) ) ]
public int[] m_aHealth = new int[ Enum.GetNames( typeof( HealthNames ) ).Length ];
我在try语句的开头和结尾添加了EditorGUI.indentLevel++;
和EditorGUI.indentLevel--;
来缩进数组元素的标签,使它们从大小属性中脱颖而出。
从那里开始,我搜索了在元素的值字段上添加缩进或从大小 属性 的值字段中删除缩进的方法。但使用 EditorGUI
找不到答案我还希望能够动态调整所有值字段的大小,但同样,仅使用 EditorGUI 也没有得到任何答案。无法在 属性 字段上使用 EditorStyle.AnyField.WordWrap = true;
。将 PropertyField
传递给 IntField
,使用 EditorStyles.NumberField
并预先将 wrodwrapping 设置为 true 的设置无效。
我也发现了几年前的少量 EditorGUILayout
,但由于解决方案是使用 EditorGUI
望各位指教
如果我对你的理解正确,你希望标签 和 值字段缩进。
我认为可以像这样
private const int INDENT = 15;
public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(rect, label, property);
var fieldsRect = new Rect(rect.x + INDENT, rect.y, rect.width - INDENT, rect.height);
try
{
var path = property.propertyPath;
int pos = int.Parse(path.Split('[').LastOrDefault().TrimEnd(']'));
EditorGUI.PropertyField(fieldRect, property, new GUIContent(ObjectNames.NicifyVariableName(((LabeledArrayAttribute)attribute).names[pos])), true);
}
catch
{
EditorGUI.PropertyField(fieldRect, property, label, true);
}
EditorGUI.EndProperty();
}