Skip to content

Commit 743ff72

Browse files
authored
Merge pull request #137 from brunomikoski/bugfix/old-fixes
Bugfix/old fixes
2 parents cc21402 + 1fc50a9 commit 743ff72

File tree

5 files changed

+60
-35
lines changed

5 files changed

+60
-35
lines changed

Scripts/Editor/CustomEditors/CollectionCustomEditor.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -630,8 +630,8 @@ private void AddNewItem()
630630
Type itemSubClass = itemsSubclasses[i];
631631
if (itemSubClass.IsAbstract)
632632
continue;
633-
634-
AddMenuOption(optionsMenu, itemSubClass.Name, () =>
633+
634+
optionsMenu.AddItem(new GUIContent(itemSubClass.Name), false, () =>
635635
{
636636
EditorApplication.delayCall += () => { AddNewItemOfType(itemSubClass); };
637637
});
@@ -648,9 +648,9 @@ private void AddNewItem()
648648
if (itemSubClass.IsSealed)
649649
continue;
650650

651-
AddMenuOption(optionsMenu, $"Create New/class $NEW : {itemSubClass.Name}", () =>
651+
optionsMenu.AddItem(new GUIContent($"Create New/class $NEW : {itemSubClass.Name}"), false, () =>
652652
{
653-
EditorApplication.delayCall += () => { CreateAndAddNewItemOfType(itemSubClass); };
653+
EditorApplication.delayCall += () => { AddNewItemOfType(itemSubClass); };
654654
});
655655
}
656656
}
@@ -713,11 +713,6 @@ private void AddNewItemOfType(Type targetType)
713713
arrayElementAtIndex.isExpanded = true;
714714
}
715715

716-
private void AddMenuOption(GenericMenu optionsMenu, string displayName, Action action)
717-
{
718-
optionsMenu.AddItem(new GUIContent(displayName), false, action.Invoke);
719-
}
720-
721716
private void DrawSearchField()
722717
{
723718
Rect searchRect =

Scripts/Editor/PropertyDrawers/CollectionItemPickerPropertyDrawer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ private void Initialize(SerializedProperty property)
256256

257257
if (!CollectionsRegistry.Instance.TryGetCollectionsOfItemType(itemType, out possibleCollections))
258258
throw new Exception($"No collection found for item type {itemType}");
259-
260259

261260
popupList.Clear();
262261
availableItems.Clear();
@@ -267,7 +266,7 @@ private void Initialize(SerializedProperty property)
267266
ScriptableObject scriptableObject = possibleCollections[i][j];
268267
Type scriptableObjectType = scriptableObject.GetType();
269268

270-
if (scriptableObjectType != itemType || scriptableObjectType.IsSubclassOf(itemType))
269+
if (scriptableObjectType != itemType && !scriptableObjectType.IsSubclassOf(itemType))
271270
continue;
272271

273272
availableItems.Add(scriptableObject);

Scripts/Editor/PropertyDrawers/SOCItemPropertyDrawer.cs

Lines changed: 24 additions & 8 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 System.Reflection;
35
using UnityEditor;
46
using UnityEditor.IMGUI.Controls;
@@ -38,6 +40,9 @@ private FieldInfo TargetFieldInfo
3840
return overrideFieldInfo;
3941
}
4042
}
43+
44+
private Type currentItemType;
45+
4146

4247
private SOCItemEditorOptionsAttribute GetOptionsAttribute()
4348
{
@@ -90,9 +95,9 @@ internal void DrawCollectionItemDrawer(ref Rect position, ScriptableObject colle
9095
currentObject = collectionItem;
9196

9297
DrawEditFoldoutButton(ref prefixPosition, collectionItem);
93-
DrawGotoButton(ref prefixPosition, collectionItem);
9498
}
95-
99+
100+
DrawGotoButton(ref prefixPosition, collectionItem);
96101
DrawCollectionItemDropDown(ref prefixPosition, collectionItem, callback);
97102
DrawEditorPreview(ref position, collectionItem);
98103
EditorGUI.indentLevel = indent;
@@ -190,7 +195,7 @@ internal void Initialize(Type collectionItemType, Object obj, SOCItemEditorOptio
190195
OptionsAttribute,
191196
obj
192197
);
193-
198+
currentItemType = collectionItemType;
194199
currentObject = obj;
195200
initialized = true;
196201

@@ -215,18 +220,29 @@ private void DrawGotoButton(ref Rect popupRect, ScriptableObject collectionItem)
215220
if (!OptionsAttribute.ShouldDrawGotoButton)
216221
return;
217222

218-
if (collectionItem is not ISOCItem socItem)
219-
return;
220-
221223
Rect buttonRect = popupRect;
222224
buttonRect.width = BUTTON_WIDTH;
223225
buttonRect.height = 18;
224226
popupRect.width -= buttonRect.width;
225227
buttonRect.x += popupRect.width;
226228
if (GUI.Button(buttonRect, CollectionEditorGUI.ARROW_RIGHT_CHAR))
227229
{
228-
Selection.activeObject = socItem.Collection;
229-
CollectionUtility.SetOnlyCollectionItemExpanded(socItem, socItem.Collection);
230+
231+
if (collectionItem == null)
232+
{
233+
if (CollectionsRegistry.Instance.TryGetCollectionsOfItemType(currentItemType, out List<ScriptableObjectCollection> possibleCollections))
234+
{
235+
Selection.activeObject = possibleCollections.First();
236+
}
237+
}
238+
else
239+
{
240+
if (collectionItem is not ISOCItem socItem)
241+
return;
242+
243+
Selection.activeObject = socItem.Collection;
244+
CollectionUtility.SetOnlyCollectionItemExpanded(socItem, socItem.Collection);
245+
}
230246
}
231247
}
232248

Scripts/Runtime/Core/CollectionItemPicker.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,23 @@ private List<CollectionItemIndirectReference<TItemType>> indirectReferences
2828
//Backwards compability with old system
2929
if (itemsGuids.Count > 0)
3030
{
31-
CollectionsRegistry.Instance.TryGetCollectionsOfItemType(out List<ScriptableObjectCollection<TItemType>> results);
32-
33-
for (int i = 0; i < itemsGuids.Count; i++)
31+
if (CollectionsRegistry.Instance.TryGetCollectionsOfItemType(out List<ScriptableObjectCollection<TItemType>> results))
3432
{
35-
LongGuid itemGuid = itemsGuids[i];
36-
for (int j = 0; j < results.Count; j++)
33+
for (int i = 0; i < itemsGuids.Count; i++)
3734
{
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;
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+
}
4445
}
46+
itemsGuids.Clear();
4547
}
46-
itemsGuids.Clear();
4748
}
4849

4950
return cachedIndirectReferences;

Scripts/Runtime/Core/CollectionsRegistry.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public void UnregisterCollection(ScriptableObjectCollection targetCollection)
5555
if (!collections.Contains(targetCollection))
5656
return;
5757

58-
collections.Remove(targetCollection);
58+
if (!collections.Remove(targetCollection))
59+
return;
5960

6061
ObjectUtility.SetDirty(this);
6162
}
@@ -230,6 +231,12 @@ public bool TryGetCollectionOfType(Type type, out ScriptableObjectCollection res
230231
for (int i = 0; i < collections.Count; i++)
231232
{
232233
ScriptableObjectCollection scriptableObjectCollection = collections[i];
234+
if (scriptableObjectCollection == null)
235+
{
236+
ValidateCollections();
237+
resultCollection = null;
238+
return false;
239+
}
233240
if (scriptableObjectCollection.GetType() == type)
234241
{
235242
resultCollection = scriptableObjectCollection;
@@ -366,6 +373,7 @@ public void PreBuildProcess()
366373
public void RemoveNonAutomaticallyInitializedCollections()
367374
{
368375
#if UNITY_EDITOR
376+
bool dirty = false;
369377
for (int i = collections.Count - 1; i >= 0; i--)
370378
{
371379
ScriptableObjectCollection collection = collections[i];
@@ -374,8 +382,13 @@ public void RemoveNonAutomaticallyInitializedCollections()
374382
continue;
375383

376384
collections.Remove(collection);
385+
dirty = true;
386+
}
387+
388+
if (dirty)
389+
{
390+
ObjectUtility.SetDirty(this);
377391
}
378-
ObjectUtility.SetDirty(this);
379392
#endif
380393
}
381394

@@ -436,11 +449,12 @@ public void ValidateCollections()
436449

437450
public void SetAutoSearchForCollections(bool isOn)
438451
{
452+
if (isOn == autoSearchForCollections)
453+
return;
454+
439455
autoSearchForCollections = isOn;
440456
ObjectUtility.SetDirty(this);
441457
}
442-
443-
444458
}
445459
}
446460

0 commit comments

Comments
 (0)