Property drawer to display list
Hello everybody,
I have an Serializable Class made with different properties, including a list of string (m_eventsList).
[System.Serializable]
public class CasualityClass
{
public List<string> m_eventsList;
public E_Conditions m_conditions;
public string m_response;
}
Since I need it to be as clear as it can be, I want to make a Drawer Property to change the display.
For now, I have succeed of adjusting the height of the drawer and the fields positions according to the number of elements I have in my list of string (otherwise, when I open it, the items are displayed above the rest of the properties).
[CustomPropertyDrawer(typeof(CasualityClass))]
public class CasualityClassDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
//Find the number of fields and the space it will take (including the number of items in the list)
CasualityClass thisCasualityClass = fieldInfo.GetValue(property.serializedObject.targetObject) as CasualityClass;
int numberOfEventsListRaw = thisCasualityClass.m_eventsList.Count;
int spaceBetweenLines = 2;
return (EditorGUIUtility.singleLineHeight + spaceBetweenLines )* (4 + numberOfEventsListRaw + 1) ;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
//Find the number of fields and the space it will take (including the number of items in the list)
CasualityClass thisCasualityClass = fieldInfo.GetValue(property.serializedObject.targetObject) as CasualityClass;
int numberOfEventsListRaw = thisCasualityClass.m_eventsList.Count;
EditorGUI.BeginProperty(position, label, property);
EditorGUI.LabelField(position, label);
//Set the position of the different fields
float eventListYPosition = position.y + 18;
var eventsListRect = new Rect(position.x, position.y + 18, position.width, 16);
float conditionYPosition = eventListYPosition + 18 + (numberOfEventsListRaw + 1) * 18;
var conditionRect = new Rect(position.x, conditionYPosition, position.width, 16);
float responseYPosition = conditionYPosition + 18;
var responseRect = new Rect(position.x, responseYPosition, position.width, 16);
EditorGUI.indentLevel++;
EditorGUI.PropertyField(eventsListRect, property.FindPropertyRelative("m_eventsList"), true);
EditorGUI.PropertyField(conditionRect, property.FindPropertyRelative("m_conditions"));
EditorGUI.PropertyField(responseRect, property.FindPropertyRelative("m_response"));
EditorGUI.indentLevel--;
EditorGUI.EndProperty();
}
}
But now, I have a problem : whereas the list is "open" or "closed" in the Editor, the space between the list and the rest of the properties remains the same (as shown on the pictures).
(when the list is open) (when the list is closed)
Is there a way to offset the rest of the properties under the list only when I click on the little triangle ?
Regards,
Vincent