From 1e7cf58f41ea41c7bbf46f2216f00156201799d1 Mon Sep 17 00:00:00 2001 From: Bruno Mikoski Date: Mon, 26 Feb 2024 14:13:17 +0000 Subject: [PATCH 1/3] fix: ordering --- .../CustomEditors/CollectionCustomEditor.cs | 93 +++++++++++-------- .../EditorWindows/CreateCollectionWizard.cs | 20 +++- 2 files changed, 69 insertions(+), 44 deletions(-) diff --git a/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs b/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs index 1f21e98..baa337e 100644 --- a/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs +++ b/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using JetBrains.Annotations; using UnityEditor; using UnityEditor.Callbacks; using UnityEditor.Compilation; @@ -151,10 +152,10 @@ protected void RemoveItemAtIndex(int selectedIndex) { SerializedProperty selectedProperty = reorderableList.serializedProperty.GetArrayElementAtIndex(selectedIndex); Object asset = selectedProperty.objectReferenceValue; - AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(asset)); - AssetDatabase.SaveAssets(); reorderableList.serializedProperty.DeleteArrayElementAtIndex(selectedIndex); reorderableList.serializedProperty.serializedObject.ApplyModifiedProperties(); + AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(asset)); + AssetDatabase.SaveAssets(); } private void OnClickToAddNewItem(Rect buttonRect, ReorderableList list) @@ -239,6 +240,7 @@ private void DrawCollectionItemAtIndex(Rect rect, int index, bool isActive, bool AssetDatabaseUtils.RenameAsset(collectionItemSerializedProperty.objectReferenceValue, newName); AssetDatabase.SaveAssets(); } + return; } } @@ -468,7 +470,9 @@ public override void OnInspectorGUI() { DrawSearchField(); DrawSynchronizeButton(); + reorderableList.DoLayoutList(); + if (Event.current.type == EventType.Repaint) { reorderableListYPosition = GUILayoutUtility.GetLastRect().y; @@ -507,15 +511,15 @@ protected virtual void SynchronizeAssets() private void CheckForKeyboardShortcuts() { + if (Event.current.type != EventType.KeyDown) + return; + if (reorderableList.index == -1) return; if (!reorderableList.HasKeyboardControl()) return; - if (Event.current.type == EventType.Layout || Event.current.type == EventType.Repaint) - return; - if (reorderableList.index > reorderableList.serializedProperty.arraySize - 1) return; @@ -650,7 +654,7 @@ private void AddNewItem() optionsMenu.AddItem(new GUIContent($"Create New/class $NEW : {itemSubClass.Name}"), false, () => { - EditorApplication.delayCall += () => { AddNewItemOfType(itemSubClass); }; + EditorApplication.delayCall += () => { CreateAndAddNewItemOfType(itemSubClass); }; }); } } @@ -668,41 +672,6 @@ private void CreateAndAddNewItemOfType(Type itemSubClass) } }); } - - [DidReloadScripts] - public static void AfterStaticAssemblyReload() - { - if (!IsWaitingForNewTypeBeCreated) - return; - - IsWaitingForNewTypeBeCreated = false; - - string lastGeneratedCollectionScriptPath = - CreateNewCollectionItemFromBaseWizard.LastGeneratedCollectionScriptPath.Value; - string lastCollectionFullName = CreateNewCollectionItemFromBaseWizard.LastCollectionFullName.Value; - - if (string.IsNullOrEmpty(lastGeneratedCollectionScriptPath)) - return; - - CreateNewCollectionItemFromBaseWizard.LastCollectionFullName.Value = string.Empty; - CreateNewCollectionItemFromBaseWizard.LastGeneratedCollectionScriptPath.Value = string.Empty; - - string assemblyName = CompilationPipeline.GetAssemblyNameFromScriptPath(lastGeneratedCollectionScriptPath); - - Type targetType = Type.GetType($"{lastCollectionFullName}, {assemblyName}"); - - if (CollectionsRegistry.Instance.TryGetCollectionFromItemType(targetType, - out ScriptableObjectCollection collection)) - { - Selection.activeObject = null; - LAST_ADDED_COLLECTION_ITEM = collection.AddNew(targetType); - - EditorApplication.delayCall += () => - { - Selection.activeObject = collection; - }; - } - } private void AddNewItemOfType(Type targetType) { @@ -989,5 +958,47 @@ private static bool EditGeneratorValidator(MenuCommand command) Type collectionType = command.context.GetType(); return CollectionGenerators.GetGeneratorTypeForCollection(collectionType) != null; } + + + class CollectionCustomEditorAssetPostProcessor : AssetPostprocessor + { + [UsedImplicitly] + private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload) + { + if (!didDomainReload) + return; + + if (!IsWaitingForNewTypeBeCreated) + return; + + IsWaitingForNewTypeBeCreated = false; + + string lastGeneratedCollectionScriptPath = + CreateNewCollectionItemFromBaseWizard.LastGeneratedCollectionScriptPath.Value; + string lastCollectionFullName = CreateNewCollectionItemFromBaseWizard.LastCollectionFullName.Value; + + if (string.IsNullOrEmpty(lastGeneratedCollectionScriptPath)) + return; + + CreateNewCollectionItemFromBaseWizard.LastCollectionFullName.Value = string.Empty; + CreateNewCollectionItemFromBaseWizard.LastGeneratedCollectionScriptPath.Value = string.Empty; + + string assemblyName = CompilationPipeline.GetAssemblyNameFromScriptPath(lastGeneratedCollectionScriptPath); + + Type targetType = Type.GetType($"{lastCollectionFullName}, {assemblyName}"); + + if (CollectionsRegistry.Instance.TryGetCollectionFromItemType(targetType, + out ScriptableObjectCollection collection)) + { + Selection.activeObject = null; + LAST_ADDED_COLLECTION_ITEM = collection.AddNew(targetType); + + EditorApplication.delayCall += () => + { + Selection.activeObject = collection; + }; + } + } + } } } diff --git a/Scripts/Editor/EditorWindows/CreateCollectionWizard.cs b/Scripts/Editor/EditorWindows/CreateCollectionWizard.cs index cc702c2..fff5491 100644 --- a/Scripts/Editor/EditorWindows/CreateCollectionWizard.cs +++ b/Scripts/Editor/EditorWindows/CreateCollectionWizard.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using JetBrains.Annotations; using UnityEditor; using UnityEditor.Callbacks; using UnityEditor.Compilation; @@ -604,7 +605,7 @@ private void CreateNewCollection() AssetDatabase.Refresh(); if (!scriptsGenerated) - OnAfterScriptsReloading(); + AfterScriptsAreReady(); } private void CreateIndirectAccess() @@ -710,8 +711,7 @@ private bool CheckValidityOfSettings() return isValid; } - [DidReloadScripts] - static void OnAfterScriptsReloading() + private static void AfterScriptsAreReady() { if (!WaitingRecompileForContinue.Value) return; @@ -735,5 +735,19 @@ static void OnAfterScriptsReloading() AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); } + + + class CollectionCustomEditorAssetPostProcessor : AssetPostprocessor + { + [UsedImplicitly] + private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, + bool didDomainReload) + { + if (!didDomainReload) + return; + + AfterScriptsAreReady(); + } + } } } From 3990fbe30c47bde1e4ae42cc0c4a8e76e88efc05 Mon Sep 17 00:00:00 2001 From: Bruno Mikoski Date: Wed, 28 Feb 2024 09:12:13 +0000 Subject: [PATCH 2/3] Update CollectionCustomEditor.cs --- Scripts/Editor/CustomEditors/CollectionCustomEditor.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs b/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs index baa337e..f7e16b3 100644 --- a/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs +++ b/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using JetBrains.Annotations; using UnityEditor; using UnityEditor.Callbacks; using UnityEditor.Compilation; From f673c045f77b0371e9c871feae714458a1d5114b Mon Sep 17 00:00:00 2001 From: Bruno Mikoski Date: Wed, 28 Feb 2024 09:14:30 +0000 Subject: [PATCH 3/3] fix: meta files --- CHANGELOG.MD | 7 +++++++ Scripts/Editor/CustomEditors/CollectionCustomEditor.cs | 1 + package.json | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 147e2e2..20a2e1a 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.2.3] +### Fixes +- `[SOCItemEditorOptions]` now has the option to limit the displayed items to the collection selected in a given field using the ConstrainToCollectionField parameter. +- `[SOCItemEditorOptions]` now has the option to fire a callback when a value is selected using the OnSelectCallbackMethod parameter. +- Replaced the implementation of the `[DidReloadScripts]` by the `AssetPostprocessor` to deal with importing errors on newer unity version +- Fixed issue where trying to add new custom items to a collection was not showing the create custom type properly ## [2.2.2] ### Fixes @@ -507,6 +513,7 @@ public bool IsValidConsumable(Consumable consumable) ### Added - First initial working version +[2.2.3]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.2.3 [2.2.2]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.2.2 [2.1.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.1.0 [2.0.9]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v2.0.9 diff --git a/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs b/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs index f7e16b3..baa337e 100644 --- a/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs +++ b/Scripts/Editor/CustomEditors/CollectionCustomEditor.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using JetBrains.Annotations; using UnityEditor; using UnityEditor.Callbacks; using UnityEditor.Compilation; diff --git a/package.json b/package.json index c1c7225..c157b5c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.brunomikoski.scriptableobjectcollection", "displayName": "Scriptable Object Collection", - "version": "2.2.2", + "version": "2.2.3", "unity": "2021.2", "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!", "keywords": [