Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [2.0.2]
### Changed
- Updated Labels from `CollectionItemPicker` to use black as the default interface
- Added some helpers to help project upgrade
- Added `[DrawAsSOCItem]` attribute to allow drawing of `ScriptableObject, ISOCItem` as a regular CollectionItem, this is only necessary for unity versions bellow 2022, should not affect anything else.

## [2.0.1]
### Changed
- Refactored `CollectionItemPicker` to only use `LongGUID` and also the PropertyDrawer has been remade, now looks nicer 🤩
Expand Down Expand Up @@ -415,6 +421,7 @@ public bool IsValidConsumable(Consumable consumable)
### Added
- First initial working version

[2.0.2]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.2
[2.0.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.1
[2.0.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.0
[1.9.7]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.9.7
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
if (item is ISOCColorizedItem coloredItem)
GUI.backgroundColor = coloredItem.LabelColor;
else
GUI.backgroundColor = originalColor;
GUI.backgroundColor = Color.black;

GUI.Label(labelRect, labelContent, labelStyle);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#if !UNITY_2022_2_OR_NEWER
using System;
using UnityEditor;
using UnityEngine;

namespace BrunoMikoski.ScriptableObjectCollections
{
[CustomPropertyDrawer(typeof(DrawAsSOCItemAttribute))]
public class DrawAsSOCItemAttributePropertyDrawer : PropertyDrawer
{
private SOCItemPropertyDrawer socItemPropertyDrawer;

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (!typeof(ISOCItem).IsAssignableFrom(fieldInfo.FieldType))
throw new Exception("[DrawAsSOCItem] should only be used on ScriptableObjects that implements the ISOCItem interface");

if (socItemPropertyDrawer == null)
{
socItemPropertyDrawer = new SOCItemPropertyDrawer();
socItemPropertyDrawer.OverrideFieldInfo(fieldInfo);
}

socItemPropertyDrawer.OnGUI(position, property, label);
}
}
}
#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 26 additions & 6 deletions Scripts/Editor/Core/PropertyDrawers/SOCItemPropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

namespace BrunoMikoski.ScriptableObjectCollections
{
#if UNITY_2022_2_OR_NEWER
[CustomPropertyDrawer(typeof(ISOCItem), true)]
#else
[CustomPropertyDrawer(typeof(ScriptableObjectCollectionItem), true)]
#endif
public class SOCItemPropertyDrawer : PropertyDrawer
{
public const float BUTTON_WIDTH = 30;
Expand All @@ -24,12 +28,23 @@ private static readonly SOCItemEditorOptionsAttribute DefaultAttribute
private CollectionItemDropdown collectionItemDropdown;
private ScriptableObject item;
private float totalHeight;

private FieldInfo overrideFieldInfo;
private FieldInfo TargetFieldInfo
{
get
{
if (overrideFieldInfo == null)
return fieldInfo;
return overrideFieldInfo;
}
}

private SOCItemEditorOptionsAttribute GetOptionsAttribute()
{
if (fieldInfo == null)
if (TargetFieldInfo == null)
return DefaultAttribute;
object[] attributes = fieldInfo.GetCustomAttributes(typeof(SOCItemEditorOptionsAttribute), false);
object[] attributes = TargetFieldInfo.GetCustomAttributes(typeof(SOCItemEditorOptionsAttribute), false);
if (attributes.Length > 0)
return attributes[0] as SOCItemEditorOptionsAttribute;
return DefaultAttribute;
Expand Down Expand Up @@ -139,15 +154,15 @@ private void Initialize(SerializedProperty property)
return;

Type itemType;
if (fieldInfo == null)
if (TargetFieldInfo == null)
{
Type parentType = property.serializedObject.targetObject.GetType();
itemType = property.GetFieldInfoFromPathByType(parentType).FieldType;
}
else
{
Type arrayOrListType = fieldInfo.FieldType.GetArrayOrListType();
itemType = arrayOrListType ?? fieldInfo.FieldType;
Type arrayOrListType = TargetFieldInfo.FieldType.GetArrayOrListType();
itemType = arrayOrListType ?? TargetFieldInfo.FieldType;
}

Initialize(itemType, property.serializedObject.targetObject, GetOptionsAttribute());
Expand Down Expand Up @@ -199,7 +214,7 @@ private void DrawGotoButton(ref Rect popupRect, ScriptableObject collectionItem)
if (!OptionsAttribute.ShouldDrawGotoButton)
return;

if (!(collectionItem is ISOCItem socItem))
if (collectionItem is not ISOCItem socItem)
return;

Rect buttonRect = popupRect;
Expand Down Expand Up @@ -235,5 +250,10 @@ private void DrawEditFoldoutButton(ref Rect popupRect, ScriptableObject targetIt
ObjectUtility.SetDirty(targetItem);
}
}

public void OverrideFieldInfo(FieldInfo targetFieldInfo)
{
overrideFieldInfo = targetFieldInfo;
}
}
}
14 changes: 14 additions & 0 deletions Scripts/Runtime/Core/DrawAsSOCItemAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using UnityEngine;

namespace BrunoMikoski.ScriptableObjectCollections
{
#if UNITY_2022_2_OR_NEWER
[Obsolete("DrawAsSOCItemAttribute is not needed anymore, since Unity 2022 PropertyDrawers can be applied to interfaces")]
#endif
[AttributeUsage(AttributeTargets.Field)]
public class DrawAsSOCItemAttribute : PropertyAttribute
{

}
}
3 changes: 3 additions & 0 deletions Scripts/Runtime/Core/DrawAsSOCItemAttribute.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Scripts/Runtime/Core/SOCItemEditorOptionsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ public class SOCItemEditorOptionsAttribute : Attribute

public string ValidateMethod { get; set; }
}

//Temporary
[Obsolete("CollectionItemEditorOptions is deprecated, please use SOCItemEditorOptionsAttribute instead.")]
public class CollectionItemEditorOptions : SOCItemEditorOptionsAttribute
{
}
}
14 changes: 6 additions & 8 deletions Scripts/Runtime/Core/ScriptableObjectCollectionItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ public class ScriptableObjectCollectionItem : ScriptableObject, IComparable<Scri
{
[SerializeField, HideInInspector]
private LongGuid guid;


public LongGuid GUID
{
get
Expand Down Expand Up @@ -56,6 +54,12 @@ public void SetCollection(ScriptableObjectCollection collection)
collectionGUID = cachedScriptableObjectCollection.GUID;
ObjectUtility.SetDirty(this);
}

public void GenerateNewGUID()
{
guid = LongGuid.NewGuid();
ObjectUtility.SetDirty(this);
}

public int CompareTo(ScriptableObjectCollectionItem other)
{
Expand Down Expand Up @@ -102,11 +106,5 @@ public override int GetHashCode()
{
return GUID.GetHashCode();
}

public void GenerateNewGUID()
{
guid = LongGuid.NewGuid();
ObjectUtility.SetDirty(this);
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.brunomikoski.scriptableobjectcollection",
"displayName": "Scriptable Object Collection",
"version": "2.0.1",
"version": "2.0.2",
"unity": "2021.2",
"description": "A library to help improve the usability of Unity3D Scriptable Objects by grouping then into a collection and exposing then by code or nice inspectors!",
"keywords": [
Expand Down