From 8c70a04f280e96ded1bfc59159ec8ec8e39e8051 Mon Sep 17 00:00:00 2001 From: Bruno Mikoski Date: Wed, 5 Apr 2023 09:22:35 +0100 Subject: [PATCH 1/3] fix: upgrade helpers and default label color to black --- CHANGELOG.MD | 5 +++++ .../PropertyDrawers/CollectionItemPickerPropertyDrawer.cs | 2 +- Scripts/Runtime/Core/SOCItemEditorOptionsAttribute.cs | 7 +++++++ package.json | 2 +- 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 87d831d..3a1fca0 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +## [2.0.2] +### Changed + - Updated Labels from `CollectionItemPicker` to use black as the default interface + - Added some helpers to help project upgrade ## [2.0.1] ### Changed @@ -415,6 +419,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 diff --git a/Scripts/Editor/Core/PropertyDrawers/CollectionItemPickerPropertyDrawer.cs b/Scripts/Editor/Core/PropertyDrawers/CollectionItemPickerPropertyDrawer.cs index f6a0f37..43d2448 100644 --- a/Scripts/Editor/Core/PropertyDrawers/CollectionItemPickerPropertyDrawer.cs +++ b/Scripts/Editor/Core/PropertyDrawers/CollectionItemPickerPropertyDrawer.cs @@ -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); } diff --git a/Scripts/Runtime/Core/SOCItemEditorOptionsAttribute.cs b/Scripts/Runtime/Core/SOCItemEditorOptionsAttribute.cs index 55c9090..946f8fd 100644 --- a/Scripts/Runtime/Core/SOCItemEditorOptionsAttribute.cs +++ b/Scripts/Runtime/Core/SOCItemEditorOptionsAttribute.cs @@ -19,4 +19,11 @@ public class SOCItemEditorOptionsAttribute : Attribute public string ValidateMethod { get; set; } } + + + //Temporary + [Obsolete("CollectionItemEditorOptions is deprecated, please use SOCItemEditorOptionsAttribute instead.")] + public class CollectionItemEditorOptions : SOCItemEditorOptionsAttribute + { + } } diff --git a/package.json b/package.json index e6e0b9a..a0ffa61 100644 --- a/package.json +++ b/package.json @@ -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": [ From 30d35a2ce850354c0df1d7089a80f41b5d852903 Mon Sep 17 00:00:00 2001 From: Bruno Mikoski Date: Wed, 5 Apr 2023 16:57:48 +0100 Subject: [PATCH 2/3] add: fixes --- CHANGELOG.MD | 4 +++ .../DrawAsSOCItemAttributePropertyDrawer.cs | 28 ++++++++++++++++ ...awAsSOCItemAttributePropertyDrawer.cs.meta | 3 ++ .../PropertyDrawers/SOCItemPropertyDrawer.cs | 32 +++++++++++++++---- .../Runtime/Core/DrawAsSOCItemAttribute.cs | 14 ++++++++ .../Core/DrawAsSOCItemAttribute.cs.meta | 3 ++ .../Core/SOCItemEditorOptionsAttribute.cs | 3 +- .../Core/ScriptableObjectCollectionItem.cs | 14 ++++---- 8 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 Scripts/Editor/Core/PropertyDrawers/DrawAsSOCItemAttributePropertyDrawer.cs create mode 100644 Scripts/Editor/Core/PropertyDrawers/DrawAsSOCItemAttributePropertyDrawer.cs.meta create mode 100644 Scripts/Runtime/Core/DrawAsSOCItemAttribute.cs create mode 100644 Scripts/Runtime/Core/DrawAsSOCItemAttribute.cs.meta diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 3a1fca0..ad05cb4 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +## [2.0.3] +### Changed +- Added `[DrawAsSOCItem]` attribute to allow drawing of `ScriptableObject, ISOCItem` should not affect anything else, than users that have custom ScriptableObjects that implement `ISOCItem`, and this is only needed for versions bellow 2022. + ## [2.0.2] ### Changed - Updated Labels from `CollectionItemPicker` to use black as the default interface diff --git a/Scripts/Editor/Core/PropertyDrawers/DrawAsSOCItemAttributePropertyDrawer.cs b/Scripts/Editor/Core/PropertyDrawers/DrawAsSOCItemAttributePropertyDrawer.cs new file mode 100644 index 0000000..cba64db --- /dev/null +++ b/Scripts/Editor/Core/PropertyDrawers/DrawAsSOCItemAttributePropertyDrawer.cs @@ -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 diff --git a/Scripts/Editor/Core/PropertyDrawers/DrawAsSOCItemAttributePropertyDrawer.cs.meta b/Scripts/Editor/Core/PropertyDrawers/DrawAsSOCItemAttributePropertyDrawer.cs.meta new file mode 100644 index 0000000..3b1356d --- /dev/null +++ b/Scripts/Editor/Core/PropertyDrawers/DrawAsSOCItemAttributePropertyDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 765e1c72fdcf445b84854fa235a6054b +timeCreated: 1680708775 \ No newline at end of file diff --git a/Scripts/Editor/Core/PropertyDrawers/SOCItemPropertyDrawer.cs b/Scripts/Editor/Core/PropertyDrawers/SOCItemPropertyDrawer.cs index 4f79f60..8ac63e8 100644 --- a/Scripts/Editor/Core/PropertyDrawers/SOCItemPropertyDrawer.cs +++ b/Scripts/Editor/Core/PropertyDrawers/SOCItemPropertyDrawer.cs @@ -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; @@ -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; @@ -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()); @@ -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; @@ -235,5 +250,10 @@ private void DrawEditFoldoutButton(ref Rect popupRect, ScriptableObject targetIt ObjectUtility.SetDirty(targetItem); } } + + public void OverrideFieldInfo(FieldInfo targetFieldInfo) + { + overrideFieldInfo = targetFieldInfo; + } } } diff --git a/Scripts/Runtime/Core/DrawAsSOCItemAttribute.cs b/Scripts/Runtime/Core/DrawAsSOCItemAttribute.cs new file mode 100644 index 0000000..0c6b3f7 --- /dev/null +++ b/Scripts/Runtime/Core/DrawAsSOCItemAttribute.cs @@ -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 + { + + } +} \ No newline at end of file diff --git a/Scripts/Runtime/Core/DrawAsSOCItemAttribute.cs.meta b/Scripts/Runtime/Core/DrawAsSOCItemAttribute.cs.meta new file mode 100644 index 0000000..61e71db --- /dev/null +++ b/Scripts/Runtime/Core/DrawAsSOCItemAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 382e5f3b7524429684dd50fd2892def2 +timeCreated: 1680709413 \ No newline at end of file diff --git a/Scripts/Runtime/Core/SOCItemEditorOptionsAttribute.cs b/Scripts/Runtime/Core/SOCItemEditorOptionsAttribute.cs index 946f8fd..fe700c2 100644 --- a/Scripts/Runtime/Core/SOCItemEditorOptionsAttribute.cs +++ b/Scripts/Runtime/Core/SOCItemEditorOptionsAttribute.cs @@ -19,8 +19,7 @@ public class SOCItemEditorOptionsAttribute : Attribute public string ValidateMethod { get; set; } } - - + //Temporary [Obsolete("CollectionItemEditorOptions is deprecated, please use SOCItemEditorOptionsAttribute instead.")] public class CollectionItemEditorOptions : SOCItemEditorOptionsAttribute diff --git a/Scripts/Runtime/Core/ScriptableObjectCollectionItem.cs b/Scripts/Runtime/Core/ScriptableObjectCollectionItem.cs index f269717..aca401a 100644 --- a/Scripts/Runtime/Core/ScriptableObjectCollectionItem.cs +++ b/Scripts/Runtime/Core/ScriptableObjectCollectionItem.cs @@ -7,8 +7,6 @@ public class ScriptableObjectCollectionItem : ScriptableObject, IComparable Date: Wed, 5 Apr 2023 18:52:53 +0100 Subject: [PATCH 3/3] Update CHANGELOG.MD --- CHANGELOG.MD | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index ad05cb4..4527601 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -5,14 +5,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] -## [2.0.3] -### Changed -- Added `[DrawAsSOCItem]` attribute to allow drawing of `ScriptableObject, ISOCItem` should not affect anything else, than users that have custom ScriptableObjects that implement `ISOCItem`, and this is only needed for versions bellow 2022. ## [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