Skip to content

Commit

Permalink
Merge pull request #154 from brunomikoski/feature/added-smarter-load-…
Browse files Browse the repository at this point in the history
…unloaded-collections

Feature/added smarter load unloaded collections
  • Loading branch information
brunomikoski authored Aug 29, 2024
2 parents 9eca097 + 2478ce5 commit 4aeaf16
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed issue while renaming one asset could be canceled on arrow keys press
- Fix removing wrong usages of ApplyModifiedProperties
- Fixed issue with the CollectionItemPicker not updating the collection properly on editor mode
- Fixed issue with check if a collection could be partial not working, and newly created collections were not being generated as partial
- Updated the Reload of collections when entering Edit Mode to only load collections that have been removed.

## [2.3.3]
## Added
Expand Down
14 changes: 9 additions & 5 deletions Scripts/Editor/Core/CodeGenerationUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,16 @@ public static bool CheckIfCanBePartial(ScriptableObjectCollection collection, st
{
string baseClassPath = AssetDatabase.GetAssetPath(MonoScript.FromScriptableObject(collection));
string baseAssembly = CompilationPipeline.GetAssemblyNameFromScriptPath(baseClassPath);
if(string.IsNullOrEmpty(destinationFolder))
destinationFolder = CompilationPipeline.GetAssemblyNameFromScriptPath(SOCSettings.Instance.GetParentFolderPathForCollection(collection));

if (string.IsNullOrEmpty(destinationFolder))
{
destinationFolder = SOCSettings.Instance.GetParentFolderPathForCollection(collection);
}

string destinationFolderAssembly = CompilationPipeline.GetAssemblyNameFromScriptPath(destinationFolder);

// NOTE: If you're not using assemblies for your code, it's expected that 'targetGeneratedCodePath' would
// be the same as 'baseAssembly', but it isn't. 'targetGeneratedCodePath' seems to be empty in that case.
bool canBePartial = baseAssembly.Equals(destinationFolder, StringComparison.Ordinal) ||
bool canBePartial = baseAssembly.Equals(destinationFolderAssembly, StringComparison.Ordinal) ||
string.IsNullOrEmpty(destinationFolder);

return canBePartial;
Expand Down Expand Up @@ -628,4 +632,4 @@ public static bool DoesStaticFileForCollectionExist(ScriptableObjectCollection c
$"{SOCSettings.Instance.GetStaticFilenameForCollection(collection)}.g.cs"));
}
}
}
}
5 changes: 2 additions & 3 deletions Scripts/Editor/Core/EditorBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ private static void OnPlayModeStateChanged(PlayModeStateChange playModeStateChan
}
else if (playModeStateChange == PlayModeStateChange.EnteredEditMode)
{
if (CollectionsRegistry.Instance.AutoSearchForCollections)
CollectionsRegistry.Instance.ReloadCollections();
CollectionsRegistry.Instance.ReloadUnloadedCollectionsIfNeeded();
}
}
}
}
}
6 changes: 6 additions & 0 deletions Scripts/Editor/CustomEditors/CollectionCustomEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public override VisualElement CreateInspectorGUI()
Toggle automaticLoadToggle = root.Q<Toggle>("automatic-loaded-toggle");
automaticLoadToggle.RegisterValueChangedCallback(evt =>
{
UpdateAutomaticallyLoaded();
UpdateHelpBox();
});

Expand Down Expand Up @@ -262,6 +263,11 @@ public override VisualElement CreateInspectorGUI()
return root;
}

private void UpdateAutomaticallyLoaded()
{
CollectionsRegistry.Instance.UpdateAutoSearchForCollections();
}

private VisualElement MakeCollectionItemListItem()
{
TemplateContainer makeCollectionItemListItem = collectionItemVisualTreeAsset.CloneTree();
Expand Down
55 changes: 52 additions & 3 deletions Scripts/Runtime/Core/CollectionsRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using UnityEngine;
using UnityEngine.Scripting;
#if UNITY_EDITOR
using System.Text;
using UnityEditor;
#endif

Expand All @@ -14,12 +15,14 @@ namespace BrunoMikoski.ScriptableObjectCollections
[Preserve]
public class CollectionsRegistry : ResourceScriptableObjectSingleton<CollectionsRegistry>
{
[SerializeField]
private const string NON_AUTO_INITIALIZED_COLLECTIONS_KEY = "NON_AUTO_INITIALIZED_COLLECTIONS";

[SerializeField]
private List<ScriptableObjectCollection> collections = new List<ScriptableObjectCollection>();
public IReadOnlyList<ScriptableObjectCollection> Collections => collections;

[SerializeField]
private bool autoSearchForCollections = true;
[SerializeField, HideInInspector]
private bool autoSearchForCollections;
public bool AutoSearchForCollections => autoSearchForCollections;

[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
Expand Down Expand Up @@ -371,6 +374,7 @@ public void PreBuildProcess()
public void RemoveNonAutomaticallyInitializedCollections()
{
#if UNITY_EDITOR
StringBuilder removedAssetPaths = new StringBuilder();
bool dirty = false;
for (int i = collections.Count - 1; i >= 0; i--)
{
Expand All @@ -380,13 +384,43 @@ public void RemoveNonAutomaticallyInitializedCollections()
continue;

collections.Remove(collection);
removedAssetPaths.Append($"{AssetDatabase.GetAssetPath(collection)}|");

dirty = true;
}

if (dirty)
{
EditorPrefs.SetString(NON_AUTO_INITIALIZED_COLLECTIONS_KEY, removedAssetPaths.ToString());
ObjectUtility.SetDirty(this);
}
else
{
EditorPrefs.DeleteKey(NON_AUTO_INITIALIZED_COLLECTIONS_KEY);
}
#endif
}

public void ReloadUnloadedCollectionsIfNeeded()
{
#if UNITY_EDITOR
string removedAssetPaths = EditorPrefs.GetString(NON_AUTO_INITIALIZED_COLLECTIONS_KEY, string.Empty);
if (string.IsNullOrEmpty(removedAssetPaths))
return;

string[] paths = removedAssetPaths.Split('|', StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < paths.Length; i++)
{
string path = paths[i];
ScriptableObjectCollection collection = AssetDatabase.LoadAssetAtPath<ScriptableObjectCollection>(path);
if (collection == null)
continue;

collections.Add(collection);
}

EditorPrefs.DeleteKey(NON_AUTO_INITIALIZED_COLLECTIONS_KEY);
ObjectUtility.SetDirty(this);
#endif
}

Expand Down Expand Up @@ -453,5 +487,20 @@ public void SetAutoSearchForCollections(bool isOn)
autoSearchForCollections = isOn;
ObjectUtility.SetDirty(this);
}

public void UpdateAutoSearchForCollections()
{
for (int i = 0; i < Collections.Count; i++)
{
ScriptableObjectCollection collection = Collections[i];
if (!collection.AutomaticallyLoaded)
{
SetAutoSearchForCollections(true);
return;
}
}

SetAutoSearchForCollections(false);
}
}
}

0 comments on commit 4aeaf16

Please sign in to comment.