Skip to content

Commit 4aeaf16

Browse files
authored
Merge pull request #154 from brunomikoski/feature/added-smarter-load-unloaded-collections
Feature/added smarter load unloaded collections
2 parents 9eca097 + 2478ce5 commit 4aeaf16

File tree

5 files changed

+71
-11
lines changed

5 files changed

+71
-11
lines changed

CHANGELOG.MD

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313
- Fixed issue while renaming one asset could be canceled on arrow keys press
1414
- Fix removing wrong usages of ApplyModifiedProperties
1515
- Fixed issue with the CollectionItemPicker not updating the collection properly on editor mode
16+
- Fixed issue with check if a collection could be partial not working, and newly created collections were not being generated as partial
17+
- Updated the Reload of collections when entering Edit Mode to only load collections that have been removed.
1618

1719
## [2.3.3]
1820
## Added

Scripts/Editor/Core/CodeGenerationUtility.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,16 @@ public static bool CheckIfCanBePartial(ScriptableObjectCollection collection, st
311311
{
312312
string baseClassPath = AssetDatabase.GetAssetPath(MonoScript.FromScriptableObject(collection));
313313
string baseAssembly = CompilationPipeline.GetAssemblyNameFromScriptPath(baseClassPath);
314-
if(string.IsNullOrEmpty(destinationFolder))
315-
destinationFolder = CompilationPipeline.GetAssemblyNameFromScriptPath(SOCSettings.Instance.GetParentFolderPathForCollection(collection));
316-
314+
if (string.IsNullOrEmpty(destinationFolder))
315+
{
316+
destinationFolder = SOCSettings.Instance.GetParentFolderPathForCollection(collection);
317+
}
318+
319+
string destinationFolderAssembly = CompilationPipeline.GetAssemblyNameFromScriptPath(destinationFolder);
320+
317321
// NOTE: If you're not using assemblies for your code, it's expected that 'targetGeneratedCodePath' would
318322
// be the same as 'baseAssembly', but it isn't. 'targetGeneratedCodePath' seems to be empty in that case.
319-
bool canBePartial = baseAssembly.Equals(destinationFolder, StringComparison.Ordinal) ||
323+
bool canBePartial = baseAssembly.Equals(destinationFolderAssembly, StringComparison.Ordinal) ||
320324
string.IsNullOrEmpty(destinationFolder);
321325

322326
return canBePartial;
@@ -628,4 +632,4 @@ public static bool DoesStaticFileForCollectionExist(ScriptableObjectCollection c
628632
$"{SOCSettings.Instance.GetStaticFilenameForCollection(collection)}.g.cs"));
629633
}
630634
}
631-
}
635+
}

Scripts/Editor/Core/EditorBehaviour.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ private static void OnPlayModeStateChanged(PlayModeStateChange playModeStateChan
1818
}
1919
else if (playModeStateChange == PlayModeStateChange.EnteredEditMode)
2020
{
21-
if (CollectionsRegistry.Instance.AutoSearchForCollections)
22-
CollectionsRegistry.Instance.ReloadCollections();
21+
CollectionsRegistry.Instance.ReloadUnloadedCollectionsIfNeeded();
2322
}
2423
}
2524
}
26-
}
25+
}

Scripts/Editor/CustomEditors/CollectionCustomEditor.cs

+6
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ public override VisualElement CreateInspectorGUI()
171171
Toggle automaticLoadToggle = root.Q<Toggle>("automatic-loaded-toggle");
172172
automaticLoadToggle.RegisterValueChangedCallback(evt =>
173173
{
174+
UpdateAutomaticallyLoaded();
174175
UpdateHelpBox();
175176
});
176177

@@ -262,6 +263,11 @@ public override VisualElement CreateInspectorGUI()
262263
return root;
263264
}
264265

266+
private void UpdateAutomaticallyLoaded()
267+
{
268+
CollectionsRegistry.Instance.UpdateAutoSearchForCollections();
269+
}
270+
265271
private VisualElement MakeCollectionItemListItem()
266272
{
267273
TemplateContainer makeCollectionItemListItem = collectionItemVisualTreeAsset.CloneTree();

Scripts/Runtime/Core/CollectionsRegistry.cs

+52-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using UnityEngine;
66
using UnityEngine.Scripting;
77
#if UNITY_EDITOR
8+
using System.Text;
89
using UnityEditor;
910
#endif
1011

@@ -14,12 +15,14 @@ namespace BrunoMikoski.ScriptableObjectCollections
1415
[Preserve]
1516
public class CollectionsRegistry : ResourceScriptableObjectSingleton<CollectionsRegistry>
1617
{
17-
[SerializeField]
18+
private const string NON_AUTO_INITIALIZED_COLLECTIONS_KEY = "NON_AUTO_INITIALIZED_COLLECTIONS";
19+
20+
[SerializeField]
1821
private List<ScriptableObjectCollection> collections = new List<ScriptableObjectCollection>();
1922
public IReadOnlyList<ScriptableObjectCollection> Collections => collections;
2023

21-
[SerializeField]
22-
private bool autoSearchForCollections = true;
24+
[SerializeField, HideInInspector]
25+
private bool autoSearchForCollections;
2326
public bool AutoSearchForCollections => autoSearchForCollections;
2427

2528
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
@@ -371,6 +374,7 @@ public void PreBuildProcess()
371374
public void RemoveNonAutomaticallyInitializedCollections()
372375
{
373376
#if UNITY_EDITOR
377+
StringBuilder removedAssetPaths = new StringBuilder();
374378
bool dirty = false;
375379
for (int i = collections.Count - 1; i >= 0; i--)
376380
{
@@ -380,13 +384,43 @@ public void RemoveNonAutomaticallyInitializedCollections()
380384
continue;
381385

382386
collections.Remove(collection);
387+
removedAssetPaths.Append($"{AssetDatabase.GetAssetPath(collection)}|");
388+
383389
dirty = true;
384390
}
385391

386392
if (dirty)
387393
{
394+
EditorPrefs.SetString(NON_AUTO_INITIALIZED_COLLECTIONS_KEY, removedAssetPaths.ToString());
388395
ObjectUtility.SetDirty(this);
389396
}
397+
else
398+
{
399+
EditorPrefs.DeleteKey(NON_AUTO_INITIALIZED_COLLECTIONS_KEY);
400+
}
401+
#endif
402+
}
403+
404+
public void ReloadUnloadedCollectionsIfNeeded()
405+
{
406+
#if UNITY_EDITOR
407+
string removedAssetPaths = EditorPrefs.GetString(NON_AUTO_INITIALIZED_COLLECTIONS_KEY, string.Empty);
408+
if (string.IsNullOrEmpty(removedAssetPaths))
409+
return;
410+
411+
string[] paths = removedAssetPaths.Split('|', StringSplitOptions.RemoveEmptyEntries);
412+
for (int i = 0; i < paths.Length; i++)
413+
{
414+
string path = paths[i];
415+
ScriptableObjectCollection collection = AssetDatabase.LoadAssetAtPath<ScriptableObjectCollection>(path);
416+
if (collection == null)
417+
continue;
418+
419+
collections.Add(collection);
420+
}
421+
422+
EditorPrefs.DeleteKey(NON_AUTO_INITIALIZED_COLLECTIONS_KEY);
423+
ObjectUtility.SetDirty(this);
390424
#endif
391425
}
392426

@@ -453,5 +487,20 @@ public void SetAutoSearchForCollections(bool isOn)
453487
autoSearchForCollections = isOn;
454488
ObjectUtility.SetDirty(this);
455489
}
490+
491+
public void UpdateAutoSearchForCollections()
492+
{
493+
for (int i = 0; i < Collections.Count; i++)
494+
{
495+
ScriptableObjectCollection collection = Collections[i];
496+
if (!collection.AutomaticallyLoaded)
497+
{
498+
SetAutoSearchForCollections(true);
499+
return;
500+
}
501+
}
502+
503+
SetAutoSearchForCollections(false);
504+
}
456505
}
457506
}

0 commit comments

Comments
 (0)