如何解决可序列化对象的Unity PropertyDrawer
[System.Serializable]
public class Item
{
public string Name;
public int ID;
}
[System.Serializable]
public class ItemGroup
{
public Item item;
public int count;
}
public class Test : MonoBehavIoUr
{
public Item item;
public ItemGroup itemGroup;
public Item[] items;
public ItemGroup[] itemGroups;
}
默认检查器如下所示:
然后我为 PropertyDrawer
和 Item
添加 ItemGroup
脚本
[CustomPropertyDrawer(typeof(Item))]
public class ItemDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property,GUIContent label)
{
return EditorGUI.GetPropertyHeight(property,label);
}
public override void OnGUI(Rect position,SerializedProperty property,GUIContent label)
{
var style = new GUIStyle(EditorStyles.foldoutHeader);
style.fontStyle = FontStyle.normal;
if (property.isExpanded = EditorGUI.BeginFoldoutHeaderGroup(position,property.isExpanded,label,style))
{
EditorGUI.indentLeveL++;
position.height = EditorGUIUtility.singleLineHeight;
position.y += position.height;
EditorGUI.PropertyField(position,property.FindPropertyRelative("Name"));
position.y += position.height;
EditorGUI.PropertyField(position,property.FindPropertyRelative("ID"));
EditorGUI.indentLevel--;
}
EditorGUI.EndFoldoutHeaderGroup();
}
}
[CustomPropertyDrawer(typeof(ItemGroup))]
public class ItemGroupDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property,label);
}
public override void OnGUI(Rect position,GUIContent label)
{
var style = new GUIStyle(EditorStyles.foldoutHeader);
style.fontStyle = FontStyle.normal;
var itemProperty = property.FindPropertyRelative("item");
var countProperty = property.FindPropertyRelative("count");
position.height = EditorGUIUtility.singleLineHeight;
if (property.isExpanded = EditorGUI.BeginFoldoutHeaderGroup(position,style))
{
EditorGUI.indentLeveL++;
position.y += position.height;
position.height = EditorGUI.GetPropertyHeight(itemProperty);
EditorGUI.PropertyField(position,itemProperty,true);
position.y += position.height;
position.height = EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(position,countProperty);
EditorGUI.indentLevel--;
}
EditorGUI.EndFoldoutHeaderGroup();
}
}
问题 1:当Enable ItemDrawer
和 disable ItemGroupDrawer
时,Item
的缩进字段错误,见下文。如何在 ItemGroup
类和数组中都正确?
[CustomPropertyDrawer(typeof(Item))] //Enable ItemDrawer
//[CustomPropertyDrawer(typeof(ItemGroup))] //disable ItemGroupDrawer
问题 2:同时启用 ItemDrawer
和 ItemGroupDrawer
时出现错误:
那么如何使用 PropertyDrawer
手动绘制 Item
和 ItemGroup
的属性,使其看起来像默认检查器?
我不想使用 EditorGUI.PropertyField(position,property);
我想一一画出它们的属性
解决方法
缩进是错误的,因为您在没有任何缩进的情况下传入原始 position
.. EditorGUI.indentLevel
仅直接影响通过 EditorGUILayout
绘制的没有矩形位的属性字段。
你可以获得一个缩进的矩形,但是使用
var indentedRect = EditorGUI.IndentedRect(position);
但是,直到现在我还没有看到为您的类型定制抽屉的真正目的......默认抽屉有什么问题? ;)
,感谢 derHugo,我已经解决了
答案 1:我应该将 @Service
public class AddressServiceImpl implements AddressService{
private AddressRepository addressRepository;
@Autowired
public AddressServiceImpl(AddressRepository theAddressRepository){
addressRepository = theAddressRepository;
}
@Override
public void deleteById(int addressId) {
addressRepository.deleteById(addressId);
}
添加到 position = EditorGUI.IndentedRect(position);
答案 2:我应该使用 OnGUI
而不是 EditorGUI.Foldout
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。