diff --git a/Assets/NaughtyAttributes/Documentation~/BoxGroup_Inspector.gif b/Assets/NaughtyAttributes/Documentation~/BoxGroup_Inspector.gif new file mode 100644 index 00000000..56fec06e Binary files /dev/null and b/Assets/NaughtyAttributes/Documentation~/BoxGroup_Inspector.gif differ diff --git a/Assets/NaughtyAttributes/README.html b/Assets/NaughtyAttributes/README.html index d5c88728..be0f02f9 100644 --- a/Assets/NaughtyAttributes/README.html +++ b/Assets/NaughtyAttributes/README.html @@ -1403,7 +1403,7 @@

Give the fields meta data. A field can have more than one meta attributes.

BoxGroup

-

Surrounds grouped fields with a box.

+

Surrounds grouped fields with a box and optionally a foldout.

public class NaughtyComponent : MonoBehaviour
 {
 	[BoxGroup("Integers")]
@@ -1411,12 +1411,12 @@ 

[BoxGroup("Integers")] public int secondInt; - [BoxGroup("Floats")] + [BoxGroup("Floats" true)] public float firstFloat; - [BoxGroup("Floats")] + [BoxGroup("Floats" true)] public float secondFloat; }

-

inspector

+

inspector

Foldout

Makes a foldout group.

diff --git a/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/BoxGroupAttribute.cs b/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/BoxGroupAttribute.cs index 5f49cd54..2546c5d9 100644 --- a/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/BoxGroupAttribute.cs +++ b/Assets/NaughtyAttributes/Scripts/Core/MetaAttributes/BoxGroupAttribute.cs @@ -6,10 +6,12 @@ namespace NaughtyAttributes public class BoxGroupAttribute : MetaAttribute, IGroupAttribute { public string Name { get; private set; } + public bool Foldable { get; private set; } - public BoxGroupAttribute(string name = "") + public BoxGroupAttribute(string name = "", bool foldable = false) { Name = name; + Foldable = foldable; } } } diff --git a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs index cce935b4..7b7ff917 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/NaughtyInspector.cs @@ -15,6 +15,7 @@ public class NaughtyInspector : UnityEditor.Editor private IEnumerable _nativeProperties; private IEnumerable _methods; private Dictionary _foldouts = new Dictionary(); + private Dictionary _boxFoldouts = new Dictionary(); protected virtual void OnEnable() { @@ -97,12 +98,29 @@ protected void DrawSerializedProperties() continue; } - NaughtyEditorGUI.BeginBoxGroup_Layout(group.Key); - foreach (var property in visibleProperties) + var boxRect = NaughtyEditorGUI.BeginBoxGroup_Layout(group.Key.name); + bool unfolded = true; //Default to true so when the Foldable optional isn't being used, we'll default to showing the properties. + + if (group.Key.foldable) { - NaughtyEditorGUI.PropertyField_Layout(property, includeChildren: true); + if (!_boxFoldouts.ContainsKey(group.Key.name)) + { + _boxFoldouts[group.Key.name] = new SavedBool($"{target.GetInstanceID()}.{group.Key}", false); + } + //Align Foldout icon to the Label Rect created in BeginBoxGroup_Layout(), but with width as wide as the inspector, allowing us to click anywhere on the label to toggle folding. + boxRect.x -= .75f * (boxRect.height = EditorGUIUtility.singleLineHeight * 1.25f); + boxRect.width = EditorGUIUtility.currentViewWidth; + unfolded = EditorGUI.Foldout(boxRect, _boxFoldouts[group.Key.name].Value, "", true); + _boxFoldouts[group.Key.name].Value = unfolded; } + if (unfolded) + { + foreach (var property in visibleProperties) + { + NaughtyEditorGUI.PropertyField_Layout(property, includeChildren: true); + } + } NaughtyEditorGUI.EndBoxGroup_Layout(); } @@ -194,14 +212,14 @@ private static IEnumerable GetNonGroupedProperties(IEnumerab { return properties.Where(p => PropertyUtility.GetAttribute(p) == null); } - - private static IEnumerable> GetGroupedProperties(IEnumerable properties) + + private static IEnumerable> GetGroupedProperties(IEnumerable properties) { return properties .Where(p => PropertyUtility.GetAttribute(p) != null) - .GroupBy(p => PropertyUtility.GetAttribute(p).Name); + .GroupBy(p => (PropertyUtility.GetAttribute(p).Name, PropertyUtility.GetAttribute(p).Foldable)); } - + private static IEnumerable> GetFoldoutProperties(IEnumerable properties) { return properties diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs index 78d8c679..351b9061 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs @@ -87,17 +87,22 @@ public static float GetIndentLength(Rect sourceRect) return indentLength; } - public static void BeginBoxGroup_Layout(string label = "") + public static Rect BeginBoxGroup_Layout(string label = "") { - EditorGUILayout.BeginVertical(GUI.skin.box); + var boxRect = EditorGUILayout.BeginVertical(GUI.skin.box); + if (!string.IsNullOrEmpty(label)) { EditorGUILayout.LabelField(label, EditorStyles.boldLabel); } + //Indent so the properties in our Box group are fully contained within it, including sub-foldouts. + EditorGUI.indentLevel++; + return boxRect; } public static void EndBoxGroup_Layout() { + EditorGUI.indentLevel--; EditorGUILayout.EndVertical(); } diff --git a/README.md b/README.md index fa85e167..732c5c0a 100644 --- a/README.md +++ b/README.md @@ -421,7 +421,7 @@ public class NaughtyComponent : MonoBehaviour Give the fields meta data. A field can have more than one meta attributes. ### BoxGroup -Surrounds grouped fields with a box. +Surrounds grouped fields with a box and optionally a foldout. ```csharp public class NaughtyComponent : MonoBehaviour @@ -431,14 +431,14 @@ public class NaughtyComponent : MonoBehaviour [BoxGroup("Integers")] public int secondInt; - [BoxGroup("Floats")] + [BoxGroup("Floats", true)] public float firstFloat; - [BoxGroup("Floats")] + [BoxGroup("Floats", true)] public float secondFloat; } ``` -![inspector](https://github.com/dbrizov/NaughtyAttributes/blob/master/Assets/NaughtyAttributes/Documentation~/BoxGroup_Inspector.png) +![inspector](https://github.com/dbrizov/NaughtyAttributes/blob/master/Assets/NaughtyAttributes/Documentation~/BoxGroup_Inspector.gif) ### Foldout Makes a foldout group.