问题描述
我正在尝试实现我的第一个统一编辑器代码,以制作一个带有缩进命名条目和缩进和动态调整大小的值字段的自定义数组属性抽屉。
我使用以下简单的 git 解决方案作为我代码的基础,它允许在检查器中设置数组的标签:HERE
替换 gitHub 解决方案中显示的示例,我将此枚举用作我的数组元素名称容器:
[System.Serializable]
public enum HealthNames
{
General,Head,Body,RightArm,LeftArm,Rightleg,leftLeg,}
并将其设置在单一行为类中的数组上:
[ LabeledArray( typeof( HealthNames ) ) ]
public int[] m_aHealth = new int[ Enum.GetNames( typeof( HealthNames ) ).Length ];
我在 try 语句的开头和结尾添加了 EditorGUI.indentLeveL++;
和 EditorGUI.indentLevel--;
以缩进数组元素的标签,使它们从 size 属性中脱颖而出。
从那里开始,我搜索了在元素的值字段上添加缩进或将其从 size 属性的值字段中删除的方法。但使用 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,true);
}
EditorGUI.EndProperty();
}