Skip to content

Commit 6edbb45

Browse files
Merge pull request #141 from brunomikoski/feature/item-order-protection
Item Order Protection
2 parents 6430bb6 + 352b718 commit 6edbb45

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Scripts/Editor/CustomEditors/CollectionCustomEditor.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@ public class CollectionCustomEditor : BaseEditor<CollectionCustomEditor>
3838

3939
private bool IsAutoGenerated => generatorType != null;
4040

41-
protected virtual bool CanBeReorderable => true;
41+
protected virtual bool CanBeReorderable
42+
{
43+
get
44+
{
45+
// If we are supposed to protect the item order, do not allow items to be reordered by dragging.
46+
if (collection != null && collection.ShouldProtectItemOrder)
47+
return false;
48+
49+
return true;
50+
}
51+
}
4252

4353
protected virtual bool DisplayAddButton
4454
{
@@ -60,6 +70,11 @@ protected virtual bool DisplayRemoveButton
6070
// doesn't make sense for you to remove items because they will be added back next time you generate.
6171
if (IsAutoGenerated && generator.ShouldRemoveNonGeneratedItems)
6272
return false;
73+
74+
// If we are supposed to protect the item order, do not allow items to be removed, otherwise you could
75+
// remove items from the middle and change the order.
76+
if (collection != null && collection.ShouldProtectItemOrder)
77+
return false;
6378

6479
return true;
6580
}

Scripts/Editor/PropertyDrawers/SOCItemPropertyDrawer.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ namespace BrunoMikoski.ScriptableObjectCollections
1111
{
1212
#if UNITY_2022_2_OR_NEWER
1313
[CustomPropertyDrawer(typeof(ISOCItem), true)]
14-
#else
15-
[CustomPropertyDrawer(typeof(ScriptableObjectCollectionItem), true)]
1614
#endif
15+
[CustomPropertyDrawer(typeof(ScriptableObjectCollectionItem), true)]
1716
public class SOCItemPropertyDrawer : PropertyDrawer
1817
{
1918
private const float BUTTON_WIDTH = 30;

Scripts/Runtime/Core/ScriptableObjectCollection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public void CopyTo(List<ScriptableObject> list)
103103
public bool IsFixedSize => false;
104104
public bool IsReadOnly => false;
105105

106+
public virtual bool ShouldProtectItemOrder => false;
107+
106108

107109
public int Add(object value)
108110
{

0 commit comments

Comments
 (0)