Skip to content

Commit

Permalink
Merge pull request #2 from badawe/feature/AllowLazyCollectionInitiali…
Browse files Browse the repository at this point in the history
…zation

Feature/allow lazy collection initialization
  • Loading branch information
brunomikoski authored Jul 25, 2020
2 parents 07151b8 + e268615 commit 2b1714c
Show file tree
Hide file tree
Showing 18 changed files with 513 additions and 97 deletions.
17 changes: 15 additions & 2 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@ All notable changes to this project will be documented in this file.
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).

## [1.1.0]
### Added
- Added support for non automatically loaded Collections, this allow you initialize collections when they are necessary, avoiding adding all the collectable
references into the resources bundle
- Added `InitializeCollections` that can initialize Collection in regular MonoBehaviours
- New type of script static script generation for Non automatically loaded collections
- Added new Custom Editor for the registry where you can define witch collection are auto loaded, and the static generation type
- PreProcess that removes the non automatically loaded collections before build

### Changed
- General bug fixes and optimizations
- Fixed issue with the dropdown on property drawer


## [1.0.1]
### Added
- Support for SubClasses support for adding new items
- Asset Modification Processor to prevent issue when deleting `Collectable` and `Collections`
- If you delete a `Collection` now, all the `Collectables` will be deleted as well and removed from the registry



## [1.0.0]
### Added
- First initial working version
Expand All @@ -20,5 +32,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[1.0.0]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.0.0
[1.0.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.0.1
[1.0.1]: https://github.com/badawe/ScriptableObjectCollection/releases/tag/v1.1.0


5 changes: 4 additions & 1 deletion Scripts/Editor/CollectableScriptableObjectPropertyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
{
if (CollectionUtility.IsFoldoutOpen(foldoutObject))
{
EditorGUI.indentLevel++;
using (new EditorGUILayout.VerticalScope("Box"))
{
Editor editor = CollectionUtility.GetEditorForItem(collectableItem);
Expand All @@ -123,6 +124,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
property.serializedObject.ApplyModifiedProperties();
}
}
EditorGUI.indentLevel--;
}
}

Expand Down Expand Up @@ -157,6 +159,7 @@ private void Initialize(SerializedProperty property)
options = collection.Items.ToArray();
List<string> displayOptions = collection.Items.Select(o => o.name).ToList();
displayOptions.Insert(0, CollectionEditorGUI.DEFAULT_NONE_ITEM_TEXT);

optionsNames = displayOptions.ToArray();
guiContents = optionsNames.Select(s => new GUIContent(s)).ToArray();

Expand All @@ -168,7 +171,7 @@ private void DrawDropDown(Rect position, SerializedProperty property)
{
using (EditorGUI.ChangeCheckScope changedCheck = new EditorGUI.ChangeCheckScope())
{
int selectedIndex = -1;
int selectedIndex = 0;

if (collectableItem != null)
selectedIndex = Array.IndexOf(options, collectableItem) + 1;
Expand Down
4 changes: 1 addition & 3 deletions Scripts/Editor/CollectionAssetsModificationProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ public static AssetDeleteResult OnWillDeleteAsset(string targetAssetPath, Remove
ScriptableObjectCollection collection =
AssetDatabase.LoadAssetAtPath<ScriptableObjectCollection>(targetAssetPath);

CollectionsRegistry.Instance.RemoveCollection(collection);
CollectionsRegistry.Instance.DeleteCollection(collection);
return AssetDeleteResult.DidNotDelete;
}



return AssetDeleteResult.DidNotDelete;
}
Expand Down
20 changes: 20 additions & 0 deletions Scripts/Editor/CollectionPreprocessBuild.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using UnityEditor.Build;
using UnityEditor.Build.Reporting;

namespace BrunoMikoski.ScriptableObjectCollections
{
public class CollectionPreprocessBuild : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }

void IPostprocessBuildWithReport.OnPostprocessBuild(BuildReport report)
{
CollectionsRegistry.Instance.PostBuildProcess();
}

void IPreprocessBuildWithReport.OnPreprocessBuild(BuildReport report)
{
CollectionsRegistry.Instance.PreBuildProcess();
}
}
}
3 changes: 3 additions & 0 deletions Scripts/Editor/CollectionPreprocessBuild.cs.meta

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

64 changes: 64 additions & 0 deletions Scripts/Editor/CollectionRegistryCustomEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using UnityEditor;
using UnityEngine;

namespace BrunoMikoski.ScriptableObjectCollections
{
[CustomEditor(typeof(CollectionsRegistry))]
public class CollectionRegistryCustomEditor : Editor
{
private SerializedProperty collections;

private void OnEnable()
{
collections = serializedObject.FindProperty("collections");
}

public override void OnInspectorGUI()
{
DrawCollections();
}

private void DrawCollections()
{
for (int i = 0; i < collections.arraySize; i++)
{
DrawCollection((ScriptableObjectCollection) collections.GetArrayElementAtIndex(i).objectReferenceValue);
}
}

private void DrawCollection(ScriptableObjectCollection collection)
{
using (new EditorGUILayout.VerticalScope("Box"))
{
using (new EditorGUILayout.HorizontalScope())
{
CollectionUtility.SetFoldoutOpen(collection,
EditorGUILayout.Toggle(GUIContent.none, CollectionUtility.IsFoldoutOpen(collection),
EditorStyles.foldout,
GUILayout.Width(13)));

EditorGUILayout.LabelField(collection.name, CollectionEditorGUI.ItemNameStyle,
GUILayout.ExpandWidth(true));
}

if (CollectionUtility.IsFoldoutOpen(collection))
{
EditorGUI.indentLevel++;
SerializedObject serializedObject = new SerializedObject(collection);
using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
{
EditorGUILayout.PropertyField(serializedObject.FindProperty("automaticallyLoaded"));
EditorGUILayout.PropertyField(serializedObject.FindProperty("staticFileGenerationType"));

if (changeCheck.changed)
{
ObjectUtility.SetDirty(collection);
serializedObject.ApplyModifiedProperties();
}
}
EditorGUI.indentLevel--;
}
}
}
}
}
3 changes: 3 additions & 0 deletions Scripts/Editor/CollectionRegistryCustomEditor.cs.meta

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

26 changes: 26 additions & 0 deletions Scripts/Editor/RegistryEditorBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using UnityEditor;

namespace BrunoMikoski.ScriptableObjectCollections
{
[InitializeOnLoad]
public class RegistryEditorBehaviour
{
static RegistryEditorBehaviour()
{

EditorApplication.playModeStateChanged += EditorApplicationOnplayModeStateChanged;
}

private static void EditorApplicationOnplayModeStateChanged(PlayModeStateChange playModeStateChange)
{
if (playModeStateChange == PlayModeStateChange.EnteredPlayMode)
{
CollectionsRegistry.Instance.RemoveNonAutomaticallyInitializedCollections();
}
else if (playModeStateChange == PlayModeStateChange.EnteredEditMode)
{
CollectionsRegistry.Instance.ReloadCollections();
}
}
}
}
3 changes: 3 additions & 0 deletions Scripts/Editor/RegistryEditorBehaviour.cs.meta

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

16 changes: 13 additions & 3 deletions Scripts/Editor/ScriptableObjectCollectionCustomEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using Object = UnityEngine.Object;

namespace BrunoMikoski.ScriptableObjectCollections
{
Expand All @@ -18,6 +19,8 @@ public class ScriptableObjectCollectionCustomEditor : Editor
private bool filteredItemListDirty = true;
private SearchField searchField;

private Object settingsFoldoutObject = new Object();

public void OnEnable()
{
collection = (ScriptableObjectCollection)target;
Expand Down Expand Up @@ -67,7 +70,6 @@ public override void OnInspectorGUI()
{
UpdateFilteredItemList();
DrawSearchField();
EditorGUILayout.Space();
DrawItems();
DrawBottomMenu();
}
Expand Down Expand Up @@ -106,7 +108,7 @@ private void DrawBottomMenu()
AddNewItem();
}

if (GUILayout.Button($"Generate {collection.name}Static.cs", EditorStyles.miniButtonRight))
if (GUILayout.Button($"Generate Static File", EditorStyles.miniButtonRight))
{
CodeGenerationUtility.GenerateStaticCollectionScript(collection);
}
Expand Down Expand Up @@ -216,8 +218,10 @@ private void DrawItem(int index)
DrawMoveItemUpButton(collectionItem);
DrawDeleteButton(collectionItem);
}

if (CollectionUtility.IsFoldoutOpen(collectionItem))
{
EditorGUI.indentLevel++;
Editor editor = CollectionUtility.GetEditorForItem(collectionItem);
using (EditorGUI.ChangeCheckScope changeCheck = new EditorGUI.ChangeCheckScope())
{
Expand All @@ -226,8 +230,14 @@ private void DrawItem(int index)
EditorGUILayout.Space();

if (changeCheck.changed)
filteredSerializedList[index].ApplyModifiedProperties();
{
if (index > filteredSerializedList.Count - 1 || filteredSerializedList[index] == null)
filteredItemListDirty = true;
else
filteredSerializedList[index].ApplyModifiedProperties();
}
}
EditorGUI.indentLevel--;
}
}
}
Expand Down
Loading

0 comments on commit 2b1714c

Please sign in to comment.