Newer
Older
Hierarchical-Task-Network-Unity-3D / Assets / Editor / SectionHandlerEditor.cs
#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;

[CustomEditor(typeof(SectionHandler))]
public class SectionHandlerEditor : Editor
{
    private SerializedProperty _gpuInstancing;
    private SerializedProperty _sectionCapacities;
    private SerializedProperty _mesh;
    private SerializedProperty _material;
    private SerializedProperty _objectScale;

    private ReorderableList _sectionCapacitiesList;

    private void OnEnable()
    {
        _gpuInstancing = serializedObject.FindProperty("GPUInstancing");
        _sectionCapacities = serializedObject.FindProperty("sectionCapacities");
        _mesh = serializedObject.FindProperty("mesh");
        _material = serializedObject.FindProperty("material");
        _objectScale = serializedObject.FindProperty("objectScale");

        _sectionCapacitiesList = new ReorderableList(serializedObject, _sectionCapacities, true, true, true, true);

        _sectionCapacitiesList.drawHeaderCallback = rect =>
        {
            EditorGUI.LabelField(rect, "Section Capacities");
        };

        _sectionCapacitiesList.drawElementCallback = (rect, index, isActive, isFocused) =>
        {
            SerializedProperty element = _sectionCapacities.GetArrayElementAtIndex(index);
            rect.y += 2;

            SerializedProperty positionsProp = element.FindPropertyRelative("positions");
            SerializedProperty thresholdProp = element.FindPropertyRelative("amountThreshold");

            float lineHeight = EditorGUIUtility.singleLineHeight;
            float spacing = EditorGUIUtility.standardVerticalSpacing;

            EditorGUI.PropertyField(
                new Rect(rect.x, rect.y, rect.width, lineHeight),
                positionsProp, new GUIContent("Positions"));

            EditorGUI.PropertyField(
                new Rect(rect.x, rect.y + lineHeight + spacing, rect.width, lineHeight),
                thresholdProp, new GUIContent("Amount Threshold"));
        };

        _sectionCapacitiesList.elementHeightCallback = index =>
        {
            return EditorGUIUtility.singleLineHeight * 2 + EditorGUIUtility.standardVerticalSpacing * 3;
        };
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        DrawPropertiesExcluding(serializedObject, "sectionCapacities", "GPUInstancing", "mesh", "material", "objectScale");

        EditorGUILayout.PropertyField(_gpuInstancing);

        if (_gpuInstancing.boolValue)
        {
            EditorGUILayout.PropertyField(_mesh);
            EditorGUILayout.PropertyField(_material);
            EditorGUILayout.PropertyField(_objectScale);

            _sectionCapacitiesList.DoLayoutList();
        }

        serializedObject.ApplyModifiedProperties();
    }
}
#endif