Skip to content

Commit

Permalink
Merge pull request #139 from brunomikoski/feature/fix-unity-2022-3
Browse files Browse the repository at this point in the history
Feature/fix unity 2022 3
  • Loading branch information
brunomikoski committed Feb 28, 2024
2 parents f71a598 + f673c04 commit 6430bb6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 45 deletions.
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.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
Expand Down Expand Up @@ -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
Expand Down
93 changes: 52 additions & 41 deletions Scripts/Editor/CustomEditors/CollectionCustomEditor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using JetBrains.Annotations;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.Compilation;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -239,6 +240,7 @@ private void DrawCollectionItemAtIndex(Rect rect, int index, bool isActive, bool
AssetDatabaseUtils.RenameAsset(collectionItemSerializedProperty.objectReferenceValue, newName);
AssetDatabase.SaveAssets();
}
return;
}
}

Expand Down Expand Up @@ -468,7 +470,9 @@ public override void OnInspectorGUI()
{
DrawSearchField();
DrawSynchronizeButton();

reorderableList.DoLayoutList();

if (Event.current.type == EventType.Repaint)
{
reorderableListYPosition = GUILayoutUtility.GetLastRect().y;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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); };
});
}
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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;
};
}
}
}
}
}
20 changes: 17 additions & 3 deletions Scripts/Editor/EditorWindows/CreateCollectionWizard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -604,7 +605,7 @@ private void CreateNewCollection()
AssetDatabase.Refresh();

if (!scriptsGenerated)
OnAfterScriptsReloading();
AfterScriptsAreReady();
}

private void CreateIndirectAccess()
Expand Down Expand Up @@ -710,8 +711,7 @@ private bool CheckValidityOfSettings()
return isValid;
}

[DidReloadScripts]
static void OnAfterScriptsReloading()
private static void AfterScriptsAreReady()
{
if (!WaitingRecompileForContinue.Value)
return;
Expand All @@ -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();
}
}
}
}
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.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": [
Expand Down

0 comments on commit 6430bb6

Please sign in to comment.