Skip to content

Commit 1face07

Browse files
authored
Merge pull request #127 from brunomikoski/feature/fix-editor-serialization-issues
add: changes
2 parents dc993ff + b155e2d commit 1face07

6 files changed

Lines changed: 71 additions & 38 deletions

File tree

CHANGELOG.MD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
10+
## [2.0.9]
11+
### Changed
12+
- Editor serialization issues with new drawing methods.
13+
- Added sorting options for collections
14+
- Added option to make the collection not reorderable
15+
- Fixed issue with post processing and recent imported files
16+
917
## [2.0.8]
1018
### Changed
1119
- Fixed a small typo in the package description.
@@ -468,6 +476,7 @@ public bool IsValidConsumable(Consumable consumable)
468476
### Added
469477
- First initial working version
470478

479+
[2.0.9]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.9
471480
[2.0.8]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.8
472481
[2.0.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.7
473482
[2.0.6]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.6

Scripts/Editor/CustomEditors/CollectionCustomEditor.cs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public class CollectionCustomEditor : BaseEditor<CollectionCustomEditor>
3030
protected int lastCheckedForValidItemsArraySize;
3131
private readonly Dictionary<int, Rect> itemIndexToRect = new();
3232
private float? reorderableListYPosition;
33+
private Dictionary<Object, SerializedObject> collectionItemSerializedObjectCache = new();
3334

35+
protected virtual bool CanBeReorderable => true;
3436
protected virtual bool DisplayAddButton => true;
3537
protected virtual bool DisplayRemoveButton => true;
3638
protected virtual bool AllowCustomTypeCreation => true;
@@ -61,7 +63,7 @@ protected virtual void OnEnable()
6163

6264
private void CreateReorderableList()
6365
{
64-
reorderableList = new ReorderableList(serializedObject, itemsSerializedProperty, true, true, DisplayAddButton, DisplayRemoveButton);
66+
reorderableList = new ReorderableList(serializedObject, itemsSerializedProperty, CanBeReorderable, true, DisplayAddButton, DisplayRemoveButton);
6567
reorderableList.drawElementCallback += DrawCollectionItemAtIndex;
6668
reorderableList.elementHeightCallback += GetCollectionItemHeight;
6769
reorderableList.onAddDropdownCallback += OnClickToAddNewItem;
@@ -141,7 +143,7 @@ private float GetCollectionItemHeight(int index)
141143
private void DrawCollectionItemAtIndex(Rect rect, int index, bool isActive, bool isFocused)
142144
{
143145
SerializedProperty collectionItemSerializedProperty = reorderableList.serializedProperty.GetArrayElementAtIndex(index);
144-
146+
145147
if (itemHidden[index] || collectionItemSerializedProperty.objectReferenceValue == null)
146148
return;
147149

@@ -212,7 +214,7 @@ private void DrawCollectionItemAtIndex(Rect rect, int index, bool isActive, bool
212214
if (collectionItemSerializedProperty.isExpanded)
213215
{
214216
rect.y += EditorGUIUtility.standardVerticalSpacing;
215-
217+
216218
if (SOCSettings.Instance.ShouldDrawUsingCustomEditor(collection))
217219
DrawCustomEditor(ref rect, index, collectionItemSerializedProperty);
218220
else
@@ -228,11 +230,18 @@ private void DrawProperties(ref Rect rect, int index, SerializedProperty collect
228230
{
229231
EditorGUI.indentLevel++;
230232

231-
SerializedObject collectionItemSerializedObject = new SerializedObject(collectionItemSerializedProperty.objectReferenceValue);
232-
SerializedProperty iterator = collectionItemSerializedObject.GetIterator();
233+
if (!collectionItemSerializedObjectCache.TryGetValue(collectionItemSerializedProperty.objectReferenceValue, out SerializedObject serializedObject))
234+
{
235+
serializedObject = new SerializedObject(collectionItemSerializedProperty.objectReferenceValue);
236+
collectionItemSerializedObjectCache.Add(collectionItemSerializedProperty.objectReferenceValue, serializedObject);
237+
}
233238

239+
serializedObject.Update();
240+
234241
using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
235242
{
243+
SerializedProperty iterator = serializedObject.GetIterator();
244+
236245
for (bool enterChildren = true; iterator.NextVisible(enterChildren); enterChildren = false)
237246
{
238247
bool guiEnabled = GUI.enabled;
@@ -241,14 +250,15 @@ private void DrawProperties(ref Rect rect, int index, SerializedProperty collect
241250

242251
EditorGUI.PropertyField(rect, iterator, true);
243252
GUI.enabled = guiEnabled;
244-
253+
245254
rect.y += EditorGUI.GetPropertyHeight(iterator, true) +
246255
EditorGUIUtility.standardVerticalSpacing;
247256
}
248-
257+
249258
if (changeCheck.changed)
250-
iterator.serializedObject.ApplyModifiedProperties();
259+
serializedObject.ApplyModifiedProperties();
251260
}
261+
252262
EditorGUI.indentLevel--;
253263
}
254264

@@ -268,16 +278,16 @@ private void DrawCustomEditor(ref Rect rect, int index, SerializedProperty colle
268278
GUILayout.BeginArea(actualRect);
269279
EditorGUI.indentLevel++;
270280

271-
EditorGUI.BeginChangeCheck();
281+
// EditorGUI.BeginChangeCheck();
272282
editor.OnInspectorGUI();
273283

274284
EditorGUI.indentLevel--;
275285
GUILayout.EndArea();
276286

277-
if (EditorGUI.EndChangeCheck())
278-
{
279-
collectionItemSerializedProperty.serializedObject.ApplyModifiedProperties();
280-
}
287+
// if (EditorGUI.EndChangeCheck())
288+
// {
289+
// collectionItemSerializedProperty.serializedObject.ApplyModifiedProperties();
290+
// }
281291

282292
rect.y += actualRect.height;
283293
}
@@ -415,6 +425,7 @@ private void DuplicateItem(int index)
415425
public override void OnInspectorGUI()
416426
{
417427
base.BeginInspector();
428+
EditorGUI.BeginChangeCheck();
418429
HideProperties();
419430

420431
ValidateCollectionItems();
@@ -434,6 +445,8 @@ public override void OnInspectorGUI()
434445
DrawSettings();
435446
CheckForKeyboardShortcuts();
436447
DrawRemainingPropertiesInInspector();
448+
if (EditorGUI.EndChangeCheck())
449+
serializedObject.ApplyModifiedProperties();
437450
}
438451

439452
private void CheckHeightsAndHiddenArraySizes()
@@ -449,12 +462,16 @@ private void DrawSynchronizeButton()
449462
{
450463
if (GUILayout.Button("Synchronize Assets"))
451464
{
452-
collection.RefreshCollection();
453-
serializedObject.Update();
454-
CheckHeightsAndHiddenArraySizes();
465+
SynchronizeAssets();
455466
}
456467
}
457468

469+
protected virtual void SynchronizeAssets()
470+
{
471+
collection.RefreshCollection();
472+
CheckHeightsAndHiddenArraySizes();
473+
}
474+
458475
private void CheckForKeyboardShortcuts()
459476
{
460477
if (reorderableList.index == -1)

Scripts/Editor/Processors/CollectionsAssetsPostProcessor.cs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,22 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse
3535
{
3636
if (socItem.Collection == null)
3737
{
38-
Debug.LogError(
39-
$"CollectionItem ({collectionItem.name}) has null Collection, please assign it some Collection",
40-
collectionItem
41-
);
38+
continue;
4239
}
43-
else
40+
41+
if (!socItem.Collection.Contains(collectionItem))
4442
{
45-
if (!socItem.Collection.Contains(collectionItem))
43+
if (socItem.Collection.TryGetItemByGUID(socItem.GUID, out _))
4644
{
47-
if (socItem.Collection.TryGetItemByGUID(socItem.GUID, out _))
48-
{
49-
Debug.LogWarning(
50-
$"Collection already contains one item with the same GUID" +
51-
$" ({socItem.GUID}) but different name ({socItem.name}), generating new GUID");
52-
socItem.GenerateNewGUID();
53-
}
54-
55-
socItem.Collection.Add(collectionItem);
56-
Debug.Log($"{collectionItem.name} has collection assigned "
57-
+ $"{socItem.Collection} but its missing from collection list, adding it");
45+
Debug.LogWarning(
46+
$"Collection already contains one item with the same GUID" +
47+
$" ({socItem.GUID}) but different name ({socItem.name}), generating new GUID");
48+
socItem.GenerateNewGUID();
5849
}
50+
51+
socItem.Collection.Add(collectionItem);
52+
Debug.Log($"{collectionItem.name} has collection assigned "
53+
+ $"{socItem.Collection} but its missing from collection list, adding it");
5954
}
6055
}
6156
}

Scripts/Editor/Utils/EditorCache.cs.meta

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

Scripts/Runtime/Core/ScriptableObjectCollection.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,15 @@ private Type GetGenericItemType()
204204
return null;
205205
}
206206

207-
public virtual void Sort()
207+
public void OrderByName()
208208
{
209-
items.Sort();
209+
items = items.OrderBy(o => o.name).ToList();
210210
ObjectUtility.SetDirty(this);
211211
}
212212

213-
public void OrderByName()
213+
public void Sort(IComparer<ScriptableObject> comparer)
214214
{
215-
items = items.OrderBy(o => o.name).ToList();
215+
items.Sort(comparer);
216216
ObjectUtility.SetDirty(this);
217217
}
218218

@@ -573,6 +573,7 @@ public bool Remove(TObjectType item)
573573
return remove;
574574
}
575575

576+
576577
IEnumerator<TObjectType> IEnumerable<TObjectType>.GetEnumerator()
577578
{
578579
using (IEnumerator<ScriptableObject> enumerator = base.GetEnumerator())

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": "2.0.8",
4+
"version": "2.0.9",
55
"unity": "2021.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": [

0 commit comments

Comments
 (0)