Skip to content

Commit 9e7f397

Browse files
committed
fix: picker stuff
1 parent 795e2fe commit 9e7f397

File tree

6 files changed

+55
-67
lines changed

6 files changed

+55
-67
lines changed

CHANGELOG.MD

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [2.3.3]
10+
## Added
11+
- Added new plus button for `ItemPicker` allowing you to add new items to the collection from the Drawer itself.
12+
13+
## Removed
14+
- Removed old `itemsGuids` that was marked as obsolete since last year. If you are upgrading from an older version, please update to a previous version before upgrading to 2.3.3.
15+
16+
## Changed
17+
- Small cleanups and fixes
18+
919
## [2.3.2]
1020
## Added
1121
- Added tooltip for the Generate Addressables Method toggle
@@ -562,6 +572,7 @@ public bool IsValidConsumable(Consumable consumable)
562572
### Added
563573
- First initial working version
564574

575+
[2.3.3]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.3
565576
[2.3.2]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.2
566577
[2.3.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.1
567578
[2.3.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.3.0
@@ -635,4 +646,4 @@ public bool IsValidConsumable(Consumable consumable)
635646
[1.1.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.1
636647
[1.1.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.0
637648
[1.0.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.0.1
638-
[1.0.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.0.0
649+
[1.0.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.0.0

Scripts/Editor/CustomEditors/CollectionCustomEditor.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -389,13 +389,6 @@ private void OnEnable()
389389
// Need to cache this before the reorderable list is created, because it affects how the list is displayed.
390390
generatorType = CollectionGenerators.GetGeneratorTypeForCollection(collection.GetType());
391391
generator = generatorType == null ? null : CollectionGenerators.GetGenerator(generatorType);
392-
393-
if (LAST_ADDED_COLLECTION_ITEM != null)
394-
{
395-
int targetIndex = collection.IndexOf(LAST_ADDED_COLLECTION_ITEM);
396-
RenameItemAtIndex(targetIndex);
397-
LAST_ADDED_COLLECTION_ITEM = null;
398-
}
399392
}
400393

401394
private void OnDisable()
@@ -453,6 +446,13 @@ private void OnVisualTreeCreated()
453446
UpdateHelpBox();
454447
UpdateGenerateStaticFileButtonState();
455448
UpdateWriteAddressablesMethodsState();
449+
450+
if (LAST_ADDED_COLLECTION_ITEM != null)
451+
{
452+
int targetIndex = collection.IndexOf(LAST_ADDED_COLLECTION_ITEM);
453+
RenameItemAtIndex(targetIndex);
454+
LAST_ADDED_COLLECTION_ITEM = null;
455+
}
456456
}
457457

458458
private void UpdateWriteAddressablesMethodsState()

Scripts/Editor/PropertyDrawers/CollectionItemPickerPropertyDrawer.cs

+31-19
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,9 @@ public class CollectionItemPickerPropertyDrawer : PropertyDrawer
3232
private readonly string name;
3333
public string Name => name;
3434

35-
private readonly LongGuid collectionGUID;
36-
public LongGuid CollectionGuid => collectionGUID;
37-
38-
private readonly LongGuid socItemGUID;
39-
public LongGuid SocItemGuid => socItemGUID;
40-
41-
4235
public PopupItem(ScriptableObject scriptableObject)
4336
{
4437
name = scriptableObject.name;
45-
if (scriptableObject is ISOCItem socItem)
46-
{
47-
collectionGUID = socItem.Collection.GUID;
48-
socItemGUID = socItem.GUID;
49-
return;
50-
}
51-
52-
collectionGUID = default;
53-
socItemGUID = default;
5438
}
5539
}
5640

@@ -73,6 +57,13 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
7357
totalPosition.height = buttonHeight;
7458
buttonRect.height = buttonHeight;
7559

60+
// Calculate the rect for the + button
61+
float buttonWidth = 20f;
62+
Rect plusButtonRect = new Rect(position.xMax - buttonWidth, position.y, buttonWidth, buttonHeight);
63+
64+
// Adjust the total position rect to exclude the + button area
65+
totalPosition.width -= buttonWidth;
66+
7667
if (!popupList.IsOpen)
7768
SetSelectedValuesOnPopup(property);
7869

@@ -89,6 +80,14 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
8980
PopupWindow.Show(buttonRect, popupList);
9081
}
9182

83+
using (new EditorGUI.DisabledScope(possibleCollections.Count > 1))
84+
{
85+
if (GUI.Button(plusButtonRect, "+"))
86+
{
87+
CreatAndAddNewItems(property);
88+
}
89+
}
90+
9291
buttonRect.width = 0;
9392

9493
Rect labelRect = buttonRect;
@@ -98,7 +97,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
9897

9998
float currentLineWidth = position.x + 4;
10099
float maxHeight = 0;
101-
float inspectorWidth = EditorGUIUtility.currentViewWidth;
100+
float inspectorWidth = EditorGUIUtility.currentViewWidth - 88;
102101
float currentLineMaxHeight = 0;
103102

104103
Color originalColor = GUI.backgroundColor;
@@ -130,7 +129,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
130129
GUI.backgroundColor = coloredItem.LabelColor;
131130
else
132131
GUI.backgroundColor = Color.black;
133-
132+
134133
GUI.Label(labelRect, labelContent, labelStyle);
135134
}
136135

@@ -144,6 +143,19 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
144143
EditorGUI.EndProperty();
145144
}
146145

146+
private void CreatAndAddNewItems(SerializedProperty property)
147+
{
148+
ScriptableObjectCollection scriptableObjectCollection = possibleCollections.First();
149+
ScriptableObjectCollection collection = scriptableObjectCollection;
150+
151+
ScriptableObject newItem = CollectionCustomEditor.AddNewItem(collection, scriptableObjectCollection.GetItemType());
152+
SerializedProperty itemsProperty = property.FindPropertyRelative(ITEMS_PROPERTY_NAME);
153+
itemsProperty.arraySize++;
154+
155+
AssignItemGUIDToProperty(newItem, itemsProperty.GetArrayElementAtIndex(itemsProperty.arraySize - 1));
156+
itemsProperty.serializedObject.ApplyModifiedProperties();
157+
}
158+
147159
private void GetValuesFromPopup(SerializedProperty property)
148160
{
149161
SerializedProperty itemsProperty = property.FindPropertyRelative(ITEMS_PROPERTY_NAME);
@@ -280,4 +292,4 @@ private void Initialize(SerializedProperty property)
280292
initialized = true;
281293
}
282294
}
283-
}
295+
}

Scripts/Runtime/Core/CollectionItemPicker.cs

+2-36
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,10 @@ namespace BrunoMikoski.ScriptableObjectCollections.Picker
1313
public class CollectionItemPicker<TItemType> : IList<TItemType>, IEquatable<IList<TItemType>>, IEquatable<CollectionItemPicker<TItemType>>
1414
where TItemType : ScriptableObject, ISOCItem
1515
{
16-
[SerializeField, Obsolete("Will be removed soon")]
17-
private List<LongGuid> itemsGuids = new List<LongGuid>();
18-
1916
[SerializeField]
2017
private List<CollectionItemIndirectReference<TItemType>> cachedIndirectReferences = new();
2118

22-
#pragma warning disable CS0618 // Type or member is obsolete
23-
24-
private List<CollectionItemIndirectReference<TItemType>> indirectReferences
25-
{
26-
get
27-
{
28-
//Backwards compability with old system
29-
if (itemsGuids.Count > 0)
30-
{
31-
if (CollectionsRegistry.Instance.TryGetCollectionsOfItemType(out List<ScriptableObjectCollection<TItemType>> results))
32-
{
33-
for (int i = 0; i < itemsGuids.Count; i++)
34-
{
35-
LongGuid itemGuid = itemsGuids[i];
36-
for (int j = 0; j < results.Count; j++)
37-
{
38-
ScriptableObjectCollection<TItemType> collection = results[j];
39-
if (!collection.TryGetItemByGUID(itemGuid, out TItemType result))
40-
continue;
41-
42-
cachedIndirectReferences.Add(new CollectionItemIndirectReference<TItemType>(result));
43-
break;
44-
}
45-
}
46-
itemsGuids.Clear();
47-
}
48-
}
49-
50-
return cachedIndirectReferences;
51-
}
52-
}
53-
#pragma warning restore CS0618 // Type or member is obsolete
19+
private List<CollectionItemIndirectReference<TItemType>> indirectReferences => cachedIndirectReferences;
5420

5521
public event Action<TItemType> OnItemTypeAddedEvent;
5622
public event Action<TItemType> OnItemTypeRemovedEvent;
@@ -365,4 +331,4 @@ public bool Equals(CollectionItemPicker<TItemType> other)
365331
return true;
366332
}
367333
}
368-
}
334+
}

Scripts/Runtime/Core/CollectionsRegistry.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -459,5 +459,4 @@ public void SetAutoSearchForCollections(bool isOn)
459459
ObjectUtility.SetDirty(this);
460460
}
461461
}
462-
}
463-
462+
}

package.json

+2-2
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": "2.3.2",
4+
"version": "2.3.3",
55
"unity": "2022.2",
66
"description": "A library to help improve the usability of Unity3D Scriptable Objects by grouping them into a collection and exposing them by code or nice inspectors!",
77
"keywords": [
@@ -28,4 +28,4 @@
2828
"path": "Samples~/AddressablesCollection"
2929
}
3030
]
31-
}
31+
}

0 commit comments

Comments
 (0)