< Summary

Class:ExpandableAttributeDrawer
Assembly:MainEditor
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/Editor/ExpandableAttributeDrawer.cs
Covered lines:0
Uncovered lines:80
Coverable lines:80
Total lines:219
Line coverage:0% (0 of 80)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ExpandableAttributeDrawer()0%2100%
GetPropertyHeight(...)0%42600%
OnGUI(...)0%72800%
DrawBackground(...)0%20400%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/Editor/ExpandableAttributeDrawer.cs

#LineLine coverage
 1using UnityEngine;
 2#if UNITY_EDITOR
 3using System;
 4using System.Collections.Generic;
 5using UnityEditor;
 6
 7#endif
 8
 9#if UNITY_EDITOR
 10/// <summary>
 11/// Draws the property field for any field marked with ExpandableAttribute.
 12/// </summary>
 13[CustomPropertyDrawer(typeof(ExpandableAttribute), true)]
 14public class ExpandableAttributeDrawer : PropertyDrawer
 15{
 16    // Use the following area to change the style of the expandable ScriptableObject drawers;
 17
 18    #region Style Setup
 19
 20    private enum BackgroundStyles
 21    {
 22        None,
 23        HelpBox,
 24        Darken,
 25        Lighten
 26    }
 27
 28    /// <summary>
 29    /// Whether the default editor Script field should be shown.
 30    /// </summary>
 031    private static bool SHOW_SCRIPT_FIELD = false;
 32
 33    /// <summary>
 34    /// The spacing on the inside of the background rect.
 35    /// </summary>
 036    private static float INNER_SPACING = 6.0f;
 37
 38    /// <summary>
 39    /// The spacing on the outside of the background rect.
 40    /// </summary>
 041    private static float OUTER_SPACING = 4.0f;
 42
 43    /// <summary>
 44    /// The style the background uses.
 45    /// </summary>
 046    private static BackgroundStyles BACKGROUND_STYLE = BackgroundStyles.HelpBox;
 47
 48    /// <summary>
 49    /// The colour that is used to darken the background.
 50    /// </summary>
 051    private static Color DARKEN_COLOUR = new Color(0.0f, 0.0f, 0.0f, 0.2f);
 52
 53    /// <summary>
 54    /// The colour that is used to lighten the background.
 55    /// </summary>
 056    private static Color LIGHTEN_COLOUR = new Color(1.0f, 1.0f, 1.0f, 0.2f);
 57
 58    #endregion
 59
 60    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
 61    {
 062        float totalHeight = 0.0f;
 63
 064        totalHeight += EditorGUIUtility.singleLineHeight;
 65
 066        if (property.objectReferenceValue == null)
 067            return totalHeight;
 68
 069        if (!property.isExpanded)
 070            return totalHeight;
 71
 072        SerializedObject targetObject = new SerializedObject(property.objectReferenceValue);
 73
 074        if (targetObject == null)
 075            return totalHeight;
 76
 077        SerializedProperty field = targetObject.GetIterator();
 78
 079        field.NextVisible(true);
 80
 081        if (SHOW_SCRIPT_FIELD)
 82        {
 083            totalHeight += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
 84        }
 85
 086        while (field.NextVisible(false))
 87        {
 088            totalHeight += EditorGUI.GetPropertyHeight(field, true) + EditorGUIUtility.standardVerticalSpacing;
 89        }
 90
 091        totalHeight += INNER_SPACING * 2;
 092        totalHeight += OUTER_SPACING * 2;
 93
 094        return totalHeight;
 95    }
 96
 97    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
 98    {
 099        Rect fieldRect = new Rect(position);
 0100        fieldRect.height = EditorGUIUtility.singleLineHeight;
 101
 0102        EditorGUI.PropertyField(fieldRect, property, label, true);
 103
 0104        if (property.objectReferenceValue == null)
 0105            return;
 106
 0107        property.isExpanded = EditorGUI.Foldout(fieldRect, property.isExpanded, GUIContent.none, true);
 108
 0109        if (!property.isExpanded)
 0110            return;
 111
 0112        SerializedObject targetObject = new SerializedObject(property.objectReferenceValue);
 113
 0114        if (targetObject == null)
 0115            return;
 116
 117
 118        #region Format Field Rects
 119
 0120        List<Rect> propertyRects = new List<Rect>();
 0121        Rect marchingRect = new Rect(fieldRect);
 122
 0123        Rect bodyRect = new Rect(fieldRect);
 0124        bodyRect.xMin += EditorGUI.indentLevel * 14;
 0125        bodyRect.yMin += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing
 126                                                           + OUTER_SPACING;
 127
 0128        SerializedProperty field = targetObject.GetIterator();
 0129        field.NextVisible(true);
 130
 0131        marchingRect.y += INNER_SPACING + OUTER_SPACING;
 132
 0133        if (SHOW_SCRIPT_FIELD)
 134        {
 0135            propertyRects.Add(marchingRect);
 0136            marchingRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
 137        }
 138
 0139        while (field.NextVisible(false))
 140        {
 0141            marchingRect.y += marchingRect.height + EditorGUIUtility.standardVerticalSpacing;
 0142            marchingRect.height = EditorGUI.GetPropertyHeight(field, true);
 0143            propertyRects.Add(marchingRect);
 144        }
 145
 0146        marchingRect.y += INNER_SPACING;
 147
 0148        bodyRect.yMax = marchingRect.yMax;
 149
 150        #endregion
 151
 0152        DrawBackground(bodyRect);
 153
 154        #region Draw Fields
 155
 0156        EditorGUI.indentLevel++;
 157
 0158        int index = 0;
 0159        field = targetObject.GetIterator();
 0160        field.NextVisible(true);
 161
 0162        if (SHOW_SCRIPT_FIELD)
 163        {
 164            //Show the disabled script field
 0165            EditorGUI.BeginDisabledGroup(true);
 0166            EditorGUI.PropertyField(propertyRects[index], field, true);
 0167            EditorGUI.EndDisabledGroup();
 0168            index++;
 169        }
 170
 171        //Replacement for "editor.OnInspectorGUI ();" so we have more control on how we draw the editor
 0172        while (field.NextVisible(false))
 173        {
 174            try
 175            {
 0176                EditorGUI.PropertyField(propertyRects[index], field, true);
 0177            }
 0178            catch (StackOverflowException)
 179            {
 0180                field.objectReferenceValue = null;
 0181                Debug.LogError("Detected self-nesting cauisng a StackOverflowException, avoid using the same " +
 182                               "object iside a nested structure.");
 0183            }
 184
 0185            index++;
 186        }
 187
 0188        targetObject.ApplyModifiedProperties();
 189
 0190        EditorGUI.indentLevel--;
 191
 192        #endregion
 193
 0194    }
 195
 196    /// <summary>
 197    /// Draws the Background
 198    /// </summary>
 199    /// <param name="rect">The Rect where the background is drawn.</param>
 200    private void DrawBackground(Rect rect)
 201    {
 0202        switch (BACKGROUND_STYLE)
 203        {
 204
 205            case BackgroundStyles.HelpBox:
 0206                EditorGUI.HelpBox(rect, "", MessageType.None);
 0207                break;
 208
 209            case BackgroundStyles.Darken:
 0210                EditorGUI.DrawRect(rect, DARKEN_COLOUR);
 0211                break;
 212
 213            case BackgroundStyles.Lighten:
 0214                EditorGUI.DrawRect(rect, LIGHTEN_COLOUR);
 215                break;
 216        }
 0217    }
 218}
 219#endif