Суть проблемы в следующем: если для свойства объекта назначить DecoratorDrawer, а потом в PropertyDrawer вызвать EditorGUI.PropertyField(), то на выходе получаем дублирование по выводу Decorator-ов.
Вот простейший пример:
Атрибут
Синтаксис:
Используется csharp
using UnityEngine;
public class LabelAttribute : PropertyAttribute {
public string label = "";
public LabelAttribute(string label = "")
{
if (!string.IsNullOrEmpty(label))
this.label = label;
}
}
public class LabelAttribute : PropertyAttribute {
public string label = "";
public LabelAttribute(string label = "")
{
if (!string.IsNullOrEmpty(label))
this.label = label;
}
}
PropertyDrawer
Синтаксис:
Используется csharp
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(LabelAttribute))]
public class LabelDrawer : PropertyDrawer {
LabelAttribute LabelAttribute
{
get
{
return (LabelAttribute)attribute;
}
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (!string.IsNullOrEmpty(LabelAttribute.label))
label = new GUIContent(LabelAttribute.label);
EditorGUI.PropertyField(position, property, label);
}
}
using UnityEditor;
[CustomPropertyDrawer(typeof(LabelAttribute))]
public class LabelDrawer : PropertyDrawer {
LabelAttribute LabelAttribute
{
get
{
return (LabelAttribute)attribute;
}
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (!string.IsNullOrEmpty(LabelAttribute.label))
label = new GUIContent(LabelAttribute.label);
EditorGUI.PropertyField(position, property, label);
}
}
И использование
Синтаксис:
Используется csharp
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
[Header("This is test Decorator.")]
[Space(15)]
[Label("Message")]
public string Test = "Hello World!!!";
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
public class NewBehaviourScript : MonoBehaviour
{
[Header("This is test Decorator.")]
[Space(15)]
[Label("Message")]
public string Test = "Hello World!!!";
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
В итоге получаем вот такую картинку (см вложение).
А еще хотелось бы пару предложений сделать) чтобы для DecoratorDrawer с order отрицательным выводить их после поля а не до. И еще хотелось бы иметь возможность вносить изменения в вывод Array, а сейчас к сожалению можно управлять только выводом элементов массива.