Skip to content

Commit 6166425

Browse files
authored
Merge pull request #42 from brunomikoski/feature/searcheable_popup
Feature/searcheable popup
2 parents 8f5ee34 + 1fb1e53 commit 6166425

6 files changed

Lines changed: 114 additions & 18 deletions

CHANGELOG.MD

Lines changed: 5 additions & 0 deletions
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.3.0]
8+
### Added
9+
- Advanced dropdown for better searching on big collections;
10+
711
## [1.2.9]
812
### Changed
913
- Fixed issue with the Foldout issue, now showing multiple inherited collectables should work fine
@@ -154,6 +158,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
154158

155159
### [Unreleased]
156160

161+
[1.3.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.3.0
157162
[1.2.9]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.9
158163
[1.2.8]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.8
159164
[1.2.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.2.7
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using System.Linq;
3+
using UnityEditor.IMGUI.Controls;
4+
using UnityEngine;
5+
6+
namespace BrunoMikoski.ScriptableObjectCollections
7+
{
8+
public sealed class CollectableDropdown : AdvancedDropdown
9+
{
10+
private ScriptableObjectCollection collection;
11+
private Action<CollectableScriptableObject> callback;
12+
13+
public CollectableDropdown(AdvancedDropdownState state, ScriptableObjectCollection collection) : base(state)
14+
{
15+
this.collection = collection;
16+
this.minimumSize = new Vector2(200, 300);
17+
}
18+
19+
protected override AdvancedDropdownItem BuildRoot()
20+
{
21+
Type collectableType = collection.GetCollectionType();
22+
AdvancedDropdownItem root = new AdvancedDropdownItem(collectableType.Name);
23+
24+
root.AddChild(new AdvancedDropdownItem("None"));
25+
for (int i = 0; i < collection.Items.Count; i++)
26+
{
27+
CollectableScriptableObject collectionItem = collection.Items[i];
28+
if (collectionItem.GetType() == collectableType)
29+
root.AddChild(new CollectableDropdownItem(collectionItem));
30+
else
31+
{
32+
AdvancedDropdownItem parent = GetOrCreateDropdownItemForType(root, collectionItem);
33+
parent.AddChild(new CollectableDropdownItem(collectionItem));
34+
}
35+
}
36+
return root;
37+
}
38+
39+
private AdvancedDropdownItem GetOrCreateDropdownItemForType(AdvancedDropdownItem root,
40+
CollectableScriptableObject collectionItem)
41+
{
42+
AdvancedDropdownItem item = root.children.FirstOrDefault(dropdownItem =>
43+
dropdownItem.name.Equals(collectionItem.GetType().Name, StringComparison.Ordinal));
44+
if (item == null)
45+
{
46+
item = new AdvancedDropdownItem(collectionItem.GetType().Name);
47+
root.AddChild(item);
48+
}
49+
50+
return item;
51+
}
52+
53+
protected override void ItemSelected(AdvancedDropdownItem item)
54+
{
55+
base.ItemSelected(item);
56+
if (item is CollectableDropdownItem collectableDropdownItem)
57+
callback?.Invoke(collectableDropdownItem.Collectable);
58+
else
59+
callback?.Invoke(null);
60+
}
61+
62+
public void Show(Rect rect, Action<CollectableScriptableObject> onSelectedCallback)
63+
{
64+
callback = onSelectedCallback;
65+
base.Show(rect);
66+
}
67+
}
68+
}

Scripts/Editor/CollectableDropdown.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using UnityEditor.IMGUI.Controls;
2+
3+
namespace BrunoMikoski.ScriptableObjectCollections
4+
{
5+
public sealed class CollectableDropdownItem : AdvancedDropdownItem
6+
{
7+
internal readonly CollectableScriptableObject Collectable;
8+
9+
public CollectableDropdownItem(CollectableScriptableObject targetCollectable) : base(targetCollectable.name)
10+
{
11+
Collectable = targetCollectable;
12+
}
13+
}
14+
}

Scripts/Editor/CollectableDropdownItem.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Editor/CollectableScriptableObjectPropertyDrawer.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using UnityEditor;
5+
using UnityEditor.IMGUI.Controls;
56
using UnityEngine;
67
using Object = UnityEngine.Object;
78

@@ -35,6 +36,8 @@ private CollectableEditorOptionsAttribute optionsAttribute
3536
private CollectableScriptableObject collectableItem;
3637
private Object currentObject;
3738

39+
private CollectableDropdown dropDown;
40+
3841
~CollectableScriptableObjectPropertyDrawer()
3942
{
4043
if(collectableItem.IsNull())
@@ -92,7 +95,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
9295
{
9396
EditorGUI.showMixedValue = property.hasMultipleDifferentValues;
9497

95-
DrawDropDown(popupRect, property);
98+
DrawSearchablePopup(popupRect, property);
9699

97100
break;
98101
}
@@ -155,37 +158,37 @@ private void Initialize(SerializedProperty property)
155158
collection = resultCollection;
156159

157160
options = collection.Items.ToArray();
158-
List<string> displayOptions = collection.Items.Select(o => o.name).ToList();
161+
List<string> displayOptions = GetDisplayOptions();
159162
displayOptions.Insert(0, CollectionEditorGUI.DEFAULT_NONE_ITEM_TEXT);
160163

161164
optionsNames = displayOptions.ToArray();
162165
GUIContents = optionsNames.Select(s => new GUIContent(s)).ToArray();
163166

164167
currentObject = property.serializedObject.targetObject;
165168
initialized = true;
169+
170+
dropDown = new CollectableDropdown(new AdvancedDropdownState(), collection);
166171
}
167172

168-
private void DrawDropDown(Rect position, SerializedProperty property)
173+
private List<string> GetDisplayOptions()
169174
{
170-
using (EditorGUI.ChangeCheckScope changedCheck = new EditorGUI.ChangeCheckScope())
171-
{
172-
int selectedIndex = 0;
173-
174-
if (collectableItem != null)
175-
selectedIndex = Array.IndexOf(options, collectableItem) + 1;
176-
177-
178-
int newSelectedIndex = EditorGUI.Popup(position, selectedIndex,
179-
GUIContents, EditorStyles.popup);
175+
return collection.Items.Select(o => o.name).ToList();
176+
}
180177

181-
newSelectedIndex -= 1;
178+
private void DrawSearchablePopup(Rect position, SerializedProperty property)
179+
{
180+
int selectedIndex = 0;
182181

183-
collectableItem = newSelectedIndex >= 0 ? options[newSelectedIndex] : null;
184-
if (changedCheck.changed)
182+
if (collectableItem != null)
183+
selectedIndex = Array.IndexOf(options, collectableItem) + 1;
184+
185+
if (GUI.Button(position, GUIContents[selectedIndex], EditorStyles.popup))
186+
{
187+
dropDown.Show(position, o =>
185188
{
186-
property.objectReferenceValue = collectableItem;
189+
property.objectReferenceValue = o;
187190
property.serializedObject.ApplyModifiedProperties();
188-
}
191+
});
189192
}
190193
}
191194

0 commit comments

Comments
 (0)