unity3d - Unity Editor:如何调整检查器属性字段的大小和空间值字段?
问题描述
我正在尝试实现我的第一个统一编辑器代码来制作一个自定义数组属性抽屉,其中包含缩进的命名条目以及缩进和动态调整大小的值字段。
我使用以下简单的 git 解决方案作为我的代码的基础,它允许在检查器中设置数组的标签:HERE
替换 gitHub 解决方案中显示的示例,我将此枚举用作我的数组元素名称容器:
[System.Serializable]
public enum HealthNames
{
General,
Head,
Body,
RightArm,
LeftArm,
Rightleg,
leftLeg,
}
并将其设置在 monobehaviour 类中的数组上:
[ LabeledArray( typeof( HealthNames ) ) ]
public int[] m_aHealth = new int[ Enum.GetNames( typeof( HealthNames ) ).Length ];
我在 try 语句的开头和结尾添加了EditorGUI.indentLevel++;
andEditorGUI.indentLevel--;
来缩进数组元素的标签,使它们从 size 属性中脱颖而出。
从那里开始,我已经搜索了在元素的值字段上添加缩进或从 size 属性的值字段中删除它的方法。但使用 EditorGUI 没有找到答案
我还希望能够动态调整所有值字段的大小,但同样,仅使用 EditorGUI 没有答案。无法EditorStyle.AnyField.WordWrap = true;
在属性字段上使用。将 a传递PropertyField
给 a IntField
,使用 aEditorStyles.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();
}
推荐阅读
- node.js - 在 ElectronJS 中检测大写锁定状态
- php - 仅在单个页面上禁用 Wordpress 可视化编辑器选项卡 - WP 5.3+
- python - 如何在python中的一行中追加最近的行
- php - 如何在laravel中验证数组字段值
- python - key - 字典中的键查找
- python-3.x - 在 plotly 中为子图共享相同的图例
- c# - 使用 C# 和 .NET Core 的 Facebook 应用
- javascript - 如何使点击png图像只点击不透明的部分
- javascript - 斯坦福卡雷尔第 8 单元,第 5 课
- laravel-7 - 如何删除电子邮件地址中的姓名