Skip to content

Commit bce000d

Browse files
author
Bruno Mikoski
committed
fix: simplified display for multiple collections dropdown
1 parent 084bf5f commit bce000d

7 files changed

Lines changed: 69 additions & 199 deletions

File tree

CHANGELOG.MD

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [1.5.1]
8+
### Changed
9+
- Simplified the multiple collection display to be a single line again, showing all the available items inside the `AdvancedDropdown`
10+
711
## [1.5.0]
812
### Changed
913
- Refactored the `Collectable` for `CollectionItem` this has several changes in multiple parts of the code, I tried my best to keep compability with the old version as well.
@@ -209,7 +213,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
209213
### [Unreleased]
210214

211215

212-
216+
[1.5.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.5.1
217+
[1.5.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.5.0
213218
[1.4.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.4.1
214219
[1.4.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.4.0
215220
[1.3.2]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.3.2

Scripts/Editor/Core/CollectionDropdownItem.cs

Lines changed: 0 additions & 19 deletions
This file was deleted.

Scripts/Editor/Core/CollectionItemDropdown.cs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using UnityEditor;
35
using UnityEditor.IMGUI.Controls;
46
using UnityEngine;
@@ -9,29 +11,50 @@ namespace BrunoMikoski.ScriptableObjectCollections
911
public sealed class CollectionItemDropdown : AdvancedDropdown
1012
{
1113
private const string CREATE_NEW_TEXT = "+ Create New";
12-
private readonly ScriptableObjectCollection collection;
1314
private Action<ScriptableObjectCollectionItem> callback;
15+
private readonly List<ScriptableObjectCollection> collections;
1416

15-
public CollectionItemDropdown(AdvancedDropdownState state, ScriptableObjectCollection collection) : base(state)
17+
private Type itemType;
18+
19+
public CollectionItemDropdown(AdvancedDropdownState state, Type targetItemType) : base(state)
1620
{
17-
this.collection = collection;
18-
this.minimumSize = new Vector2(200, 300);
21+
itemType = targetItemType;
22+
collections = CollectionsRegistry.Instance.GetCollectionsByItemType(itemType);
23+
minimumSize = new Vector2(200, 300);
1924
}
2025

2126
protected override AdvancedDropdownItem BuildRoot()
2227
{
23-
Type collectionItemType = collection.GetItemType();
24-
AdvancedDropdownItem root = new AdvancedDropdownItem(collectionItemType.Name);
28+
AdvancedDropdownItem root = new AdvancedDropdownItem(itemType.Name);
2529

2630
root.AddChild(new AdvancedDropdownItem("None"));
2731
root.AddSeparator();
28-
for (int i = 0; i < collection.Count; i++)
32+
33+
AdvancedDropdownItem targetParent = root;
34+
bool multipleCollections = collections.Count > 1;
35+
for (int i = 0; i < collections.Count; i++)
2936
{
30-
ScriptableObjectCollectionItem collectionItem = collection[i];
31-
root.AddChild(new CollectionItemDropdownItem(collectionItem));
37+
ScriptableObjectCollection collection = collections[i];
38+
39+
if (multipleCollections)
40+
{
41+
AdvancedDropdownItem collectionParent = new AdvancedDropdownItem(collection.name);
42+
root.AddChild(collectionParent);
43+
targetParent = collectionParent;
44+
}
45+
46+
for (int j = 0; j < collection.Count; j++)
47+
{
48+
ScriptableObjectCollectionItem collectionItem = collection[j];
49+
targetParent.AddChild(new CollectionItemDropdownItem(collectionItem));
50+
}
51+
}
52+
53+
if (!multipleCollections)
54+
{
55+
root.AddSeparator();
56+
root.AddChild(new AdvancedDropdownItem(CREATE_NEW_TEXT));
3257
}
33-
root.AddSeparator();
34-
root.AddChild(new AdvancedDropdownItem(CREATE_NEW_TEXT));
3558
return root;
3659
}
3760

@@ -41,7 +64,8 @@ protected override void ItemSelected(AdvancedDropdownItem item)
4164

4265
if (item.name.Equals(CREATE_NEW_TEXT, StringComparison.OrdinalIgnoreCase))
4366
{
44-
ScriptableObjectCollectionItem collectionItem = collection.AddNew(collection.GetItemType());
67+
ScriptableObjectCollection collection = collections.First();
68+
ScriptableObjectCollectionItem collectionItem = collection.AddNew(itemType);
4569
callback?.Invoke(collectionItem);
4670
Selection.objects = new Object[] {collection};
4771
CollectionCustomEditor.SetLastAddedEnum(collectionItem);

Scripts/Editor/Core/CollectionItemItemObjectPropertyDrawer.cs

Lines changed: 11 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
42
using UnityEditor;
53
using UnityEditor.IMGUI.Controls;
64
using UnityEngine;
@@ -27,30 +25,12 @@ private CollectionItemEditorOptionsAttribute OptionsAttribute
2725
}
2826

2927
private bool initialized;
30-
private ScriptableObjectCollection collection;
3128

32-
private ScriptableObjectCollectionItem[] options;
33-
private GUIContent[] guiContents;
34-
35-
private ScriptableObjectCollectionItem item;
3629
private Object currentObject;
3730

3831
private CollectionItemDropdown collectionItemDropdown;
32+
private ScriptableObjectCollectionItem item;
3933

40-
private ScriptableObjectCollection[] availableCollections;
41-
private GUIContent[] availableCollectionsGUIContents;
42-
43-
private bool singleCollection;
44-
private CollectionsDropDown collectionsDropdown;
45-
46-
~CollectionItemItemObjectPropertyDrawer()
47-
{
48-
if(item.IsNull())
49-
return;
50-
51-
ObjectUtility.SetDirty(item);
52-
}
53-
5434
private CollectionItemEditorOptionsAttribute GetOptionsAttribute()
5535
{
5636
if (fieldInfo == null)
@@ -62,14 +42,6 @@ object[] attributes
6242
return DefaultAttribute;
6343
}
6444

65-
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
66-
{
67-
if (singleCollection || OptionsAttribute.DrawType == DrawType.AsReference)
68-
return base.GetPropertyHeight(property, label);
69-
70-
return (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing) * 2;
71-
}
72-
7345
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
7446
{
7547
Initialize(property);
@@ -93,13 +65,6 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
9365
int indent = EditorGUI.indentLevel;
9466
EditorGUI.indentLevel = 0;
9567

96-
97-
if (!singleCollection)
98-
{
99-
DrawCollectionDropDown(ref popupRect, property);
100-
popupRect.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
101-
}
102-
10368
if (item != null)
10469
{
10570
DrawEditFoldoutButton(ref popupRect);
@@ -137,97 +102,28 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
137102
}
138103
}
139104

140-
private void DrawCollectionDropDown(ref Rect popupRect, SerializedProperty property)
141-
{
142-
int index = 0;
143-
if (collection != null)
144-
index = Array.IndexOf(availableCollections, collection) + 1;
145-
146-
if (GUI.Button(popupRect, availableCollectionsGUIContents[index], EditorStyles.popup))
147-
{
148-
collectionsDropdown.Show(popupRect, objectCollection =>
149-
{
150-
OnCollectionChanged(objectCollection, property);
151-
}
152-
);
153-
}
154-
}
155-
156-
private void OnCollectionChanged(ScriptableObjectCollection scriptableObjectCollection, SerializedProperty property)
157-
{
158-
if (scriptableObjectCollection == null)
159-
{
160-
collection = null;
161-
property.objectReferenceValue = null;
162-
property.serializedObject.ApplyModifiedProperties();
163-
return;
164-
}
165-
166-
collection = scriptableObjectCollection;
167-
options = collection.Items.ToArray();
168-
List<string> displayOptions = collection.Items.Select(o => o.name).ToList();
169-
displayOptions.Insert(0, CollectionEditorGUI.DEFAULT_NONE_ITEM_TEXT);
170-
171-
string[] optionsNames = displayOptions.ToArray();
172-
guiContents = optionsNames.Select(s => new GUIContent(s)).ToArray();
173-
collectionItemDropdown = new CollectionItemDropdown(new AdvancedDropdownState(), collection);
174-
}
175-
176105
private void Initialize(SerializedProperty property)
177106
{
178107
if (initialized)
179108
return;
180109
Type arrayOrListType = fieldInfo.FieldType.GetArrayOrListType();
181110
Type itemType = arrayOrListType != null ? arrayOrListType : fieldInfo.FieldType;
182-
183-
availableCollections = CollectionsRegistry.Instance.GetCollectionsByItemType(itemType).ToArray();
184-
if (availableCollections.Length == 0)
185-
{
186-
OptionsAttribute.DrawType = DrawType.AsReference;
187-
return;
188-
}
189-
190-
if (availableCollections.Length == 1)
191-
{
192-
singleCollection = true;
193-
OnCollectionChanged(availableCollections.First(), property);
194-
}
195-
else
196-
{
197-
List<GUIContent> collectionsNames = availableCollections
198-
.Select(objectCollection => new GUIContent(objectCollection.name))
199-
.ToList();
200-
201-
collectionsNames.Insert(0, new GUIContent("None"));
202-
availableCollectionsGUIContents = collectionsNames.ToArray();
203-
204-
collectionsDropdown = new CollectionsDropDown(new AdvancedDropdownState(), availableCollections);
205-
206-
if (property.objectReferenceValue != null)
207-
{
208-
if (property.objectReferenceValue is ScriptableObjectCollectionItem collectionItem)
209-
{
210-
OnCollectionChanged(collectionItem.Collection, property);
211-
}
212-
}
213-
}
214111

112+
collectionItemDropdown = new CollectionItemDropdown(
113+
new AdvancedDropdownState(),
114+
itemType
115+
);
116+
215117
currentObject = property.serializedObject.targetObject;
216118
initialized = true;
217119
}
218120

219121
private void DrawCollectionItemDropDown(ref Rect position, SerializedProperty property)
220122
{
221-
bool guiEnabled = GUI.enabled;
222-
if (collection == null)
223-
GUI.enabled = false;
224-
GUIContent displayValue = new GUIContent("Select Collection First");
123+
GUIContent displayValue = new GUIContent("None");
225124

226-
if (collection != null)
227-
{
228-
int selectedIndex = Array.IndexOf(options, item) + 1;
229-
displayValue = guiContents[selectedIndex];
230-
}
125+
if (item != null)
126+
displayValue = new GUIContent(item.name);
231127

232128
if (GUI.Button(position, displayValue, EditorStyles.popup))
233129
{
@@ -237,8 +133,6 @@ private void DrawCollectionItemDropDown(ref Rect position, SerializedProperty pr
237133
property.serializedObject.ApplyModifiedProperties();
238134
});
239135
}
240-
241-
GUI.enabled = guiEnabled;
242136
}
243137

244138
private void DrawGotoButton(ref Rect popupRect)
@@ -250,8 +144,8 @@ private void DrawGotoButton(ref Rect popupRect)
250144
buttonRect.x += popupRect.width;
251145
if (GUI.Button(buttonRect, CollectionEditorGUI.ARROW_RIGHT_CHAR))
252146
{
253-
Selection.activeObject = collection;
254-
CollectionUtility.SetFoldoutOpen(true, item, collection);
147+
Selection.activeObject = item.Collection;
148+
CollectionUtility.SetFoldoutOpen(true, item, item.Collection);
255149
}
256150
}
257151

Scripts/Editor/Core/CollectionsDropDown.cs

Lines changed: 0 additions & 49 deletions
This file was deleted.

Scripts/Runtime/Core/CollectionsRegistry.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,21 @@ private void ValidateCurrentGUIDs()
9494
}
9595
}
9696

97+
public List<ScriptableObjectCollectionItem> GetAllCollectionItemsOfType(Type itemType)
98+
{
99+
List<ScriptableObjectCollectionItem> results = new List<ScriptableObjectCollectionItem>();
100+
for (int i = 0; i < collections.Count; i++)
101+
{
102+
ScriptableObjectCollection scriptableObjectCollection = collections[i];
103+
if (!scriptableObjectCollection.GetItemType().IsAssignableFrom(itemType))
104+
continue;
105+
106+
results.AddRange(scriptableObjectCollection.Items);
107+
}
108+
109+
return results;
110+
}
111+
97112
public List<ScriptableObjectCollection> GetCollectionsByItemType<T>() where T : ScriptableObjectCollectionItem
98113
{
99114
return GetCollectionsByItemType(typeof(T));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "com.brunomikoski.scriptableobjectcollection",
33
"displayName": "Scriptable Object Collection",
4-
"version": "1.5.0",
4+
"version": "1.5.1",
55
"unity": "2018.4",
66
"description": "A library to help improve the usability of Unity3D Scriptable Objects by grouping then into a collection and exposing then by code or nice inspectors!",
77
"keywords": [

0 commit comments

Comments
 (0)