Skip to content

Commit a06084d

Browse files
committed
add: loading collections through addressables
1 parent 6d12646 commit a06084d

File tree

4 files changed

+100
-15
lines changed

4 files changed

+100
-15
lines changed

Scripts/Editor/BrunoMikoski.ScriptableObjectCollection.Editor.asmdef

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"name": "BrunoMikoski.ScriptableObjectCollection.Editor",
33
"rootNamespace": "",
44
"references": [
5-
"BrunoMikoski.ScriptableObjectCollection"
5+
"BrunoMikoski.ScriptableObjectCollection",
6+
"Unity.Addressables.Editor",
7+
"Unity.Addressables"
68
],
79
"includePlatforms": [
810
"Editor"
@@ -13,6 +15,12 @@
1315
"precompiledReferences": [],
1416
"autoReferenced": true,
1517
"defineConstraints": [],
16-
"versionDefines": [],
18+
"versionDefines": [
19+
{
20+
"name": "com.unity.addressables",
21+
"expression": "",
22+
"define": "ADDRESSABLES_ENABLED"
23+
}
24+
],
1725
"noEngineReferences": false
1826
}

Scripts/Editor/Core/CodeGenerationUtility.cs

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,20 @@
77
using UnityEditor.Compilation;
88
using UnityEditorInternal;
99
using UnityEngine;
10+
#if ADDRESSABLES_ENABLED
11+
using UnityEditor.AddressableAssets;
12+
using UnityEditor.AddressableAssets.Settings;
13+
#endif
1014

1115
namespace BrunoMikoski.ScriptableObjectCollections
1216
{
1317
public static class CodeGenerationUtility
1418
{
19+
private const string PrivateValuesName = "cachedValues";
20+
private const string PublicValuesName = "Values";
21+
private const string HasCachedValuesName = "hasCachedValues";
22+
23+
1524
public static bool CreateNewScript(
1625
string fileName, string parentFolder, string nameSpace, string[] directives, params string[] lines)
1726
{
@@ -401,6 +410,12 @@ public static void GenerateStaticCollectionScript(ScriptableObjectCollection col
401410

402411
WriteDirectAccessCollectionStatic(collection, writer, ref indentation, useBaseClass);
403412

413+
if (!collection.AutomaticallyLoaded)
414+
{
415+
WriteNonAutomaticallyLoadedCollectionItems(collection, writer, ref indentation, useBaseClass);
416+
}
417+
418+
404419
indentation--;
405420
AppendFooter(writer, ref indentation, nameSpace);
406421
}
@@ -409,6 +424,8 @@ public static void GenerateStaticCollectionScript(ScriptableObjectCollection col
409424
AssetDatabase.Refresh();
410425
}
411426

427+
428+
412429
private static bool CanGenerateStaticFile(ScriptableObjectCollection collection, out string errorMessage)
413430
{
414431
CollectionsRegistry.Instance.ValidateCollections();
@@ -454,16 +471,22 @@ private static string[] GetCollectionDirectives(ScriptableObjectCollection colle
454471
for (int i = 0; i < collection.Count; i++)
455472
directives.Add(collection[i].GetType().Namespace);
456473

474+
if (!collection.AutomaticallyLoaded)
475+
{
476+
#if ADDRESSABLES_ENABLED
477+
directives.Add("UnityEngine.AddressableAssets");
478+
directives.Add("UnityEngine.ResourceManagement.AsyncOperations");
479+
#endif
480+
}
481+
457482
return directives.ToArray();
458483
}
459484

460485
private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection collection, StreamWriter writer,
461486
ref int indentation, bool useBaseClass)
462487
{
463-
string cachedValuesName = "values";
464-
string hasCachedValuesNames = "hasCachedValues";
465-
AppendLine(writer, indentation, $"private static bool {hasCachedValuesNames};");
466-
AppendLine(writer, indentation, $"private static {collection.GetType().Name} {cachedValuesName};");
488+
AppendLine(writer, indentation, $"private static bool {HasCachedValuesName};");
489+
AppendLine(writer, indentation, $"private static {collection.GetType().Name} {PrivateValuesName};");
467490

468491
AppendLine(writer, indentation);
469492

@@ -479,23 +502,22 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
479502

480503
AppendLine(writer, indentation);
481504

482-
string valuesName = $"Values";
483505

484506
AppendLine(writer, indentation,
485-
$"public static {collection.GetType().FullName} {valuesName}");
507+
$"public static {collection.GetType().FullName} {PublicValuesName}");
486508

487509
AppendLine(writer, indentation, "{");
488510
indentation++;
489511
AppendLine(writer, indentation, "get");
490512
AppendLine(writer, indentation, "{");
491513
indentation++;
492-
AppendLine(writer, indentation, $"if (!{hasCachedValuesNames})");
514+
AppendLine(writer, indentation, $"if (!{HasCachedValuesName})");
493515
indentation++;
494516
(long, long) collectionGUIDValues = collection.GUID.GetRawValues();
495517
AppendLine(writer, indentation,
496-
$"{hasCachedValuesNames} = CollectionsRegistry.Instance.TryGetCollectionByGUID(new LongGuid({collectionGUIDValues.Item1}, {collectionGUIDValues.Item2}), out {cachedValuesName});");
518+
$"{HasCachedValuesName} = CollectionsRegistry.Instance.TryGetCollectionByGUID(new LongGuid({collectionGUIDValues.Item1}, {collectionGUIDValues.Item2}), out {PrivateValuesName});");
497519
indentation--;
498-
AppendLine(writer, indentation, $"return {cachedValuesName};");
520+
AppendLine(writer, indentation, $"return {PrivateValuesName};");
499521
indentation--;
500522
AppendLine(writer, indentation, "}");
501523
indentation--;
@@ -527,7 +549,7 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
527549
indentation++;
528550
(long, long) collectionItemGUIDValues = socItem.GUID.GetRawValues();
529551
AppendLine(writer, indentation,
530-
$"{privateHasCachedName} = Values.TryGetItemByGUID(new LongGuid({collectionItemGUIDValues.Item1}, {collectionItemGUIDValues.Item2}), out {privateStaticCachedName});");
552+
$"{privateHasCachedName} = {PublicValuesName}.TryGetItemByGUID(new LongGuid({collectionItemGUIDValues.Item1}, {collectionItemGUIDValues.Item2}), out {privateStaticCachedName});");
531553
indentation--;
532554
AppendLine(writer, indentation, $"return {privateStaticCachedName};");
533555
indentation--;
@@ -539,6 +561,53 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
539561

540562
AppendLine(writer, indentation);
541563
}
564+
565+
private static void WriteNonAutomaticallyLoadedCollectionItems(ScriptableObjectCollection collection, StreamWriter writer, ref int indentation, bool useBaseClass)
566+
{
567+
AppendLine(writer, indentation,
568+
$"public static bool IsCollectionLoaded()");
569+
570+
AppendLine(writer, indentation, "{");
571+
indentation++;
572+
AppendLine(writer, indentation, $"return {PublicValuesName} != null;");
573+
indentation--;
574+
AppendLine(writer, indentation, "}");
575+
576+
AppendLine(writer, indentation);
577+
578+
579+
#if ADDRESSABLES_ENABLED
580+
string assetPath = AssetDatabase.GetAssetPath(collection);
581+
AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject.Settings;
582+
AddressableAssetEntry entry = settings.FindAssetEntry(AssetDatabase.AssetPathToGUID(assetPath));
583+
584+
if (entry != null)
585+
{
586+
AppendLine(writer, indentation, $"private static AsyncOperationHandle<{collection.GetType().FullName}> collectionHandle;");
587+
AppendLine(writer, indentation, $"public static AsyncOperationHandle<{collection.GetType().FullName}> LoadCollectionAsync()");
588+
AppendLine(writer, indentation, "{");
589+
indentation++;
590+
AppendLine(writer, indentation, $"collectionHandle = Addressables.LoadAssetAsync<{collection.GetType().FullName}>(\"{entry.guid}\");");
591+
AppendLine(writer, indentation, "return collectionHandle;");
592+
indentation--;
593+
AppendLine(writer, indentation, "}");
594+
AppendLine(writer, indentation);
595+
596+
AppendLine(writer, indentation, "public static void UnloadCollection()");
597+
AppendLine(writer, indentation, "{");
598+
indentation++;
599+
AppendLine(writer, indentation, $"CollectionsRegistry.Instance.UnregisterCollection({PublicValuesName});");
600+
AppendLine(writer, indentation, $"{HasCachedValuesName} = false;");
601+
AppendLine(writer, indentation, $"{PrivateValuesName} = null;");
602+
603+
AppendLine(writer, indentation, "Addressables.Release(collectionHandle);");
604+
indentation--;
605+
AppendLine(writer, indentation, "}");
606+
607+
}
608+
#endif
609+
}
610+
542611

543612
public static bool DoesStaticFileForCollectionExist(ScriptableObjectCollection collection)
544613
{
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
{
22
"name": "BrunoMikoski.ScriptableObjectCollection",
33
"rootNamespace": "",
4-
"references": [],
4+
"references": [
5+
"GUID:9e24947de15b9834991c9d8411ea37cf"
6+
],
57
"includePlatforms": [],
68
"excludePlatforms": [],
79
"allowUnsafeCode": false,
810
"overrideReferences": false,
911
"precompiledReferences": [],
1012
"autoReferenced": true,
1113
"defineConstraints": [],
12-
"versionDefines": [],
14+
"versionDefines": [
15+
{
16+
"name": "com.unity.addressables",
17+
"expression": "",
18+
"define": "ADDRESSABLES_ENABLED"
19+
}
20+
],
1321
"noEngineReferences": false
1422
}

Scripts/Runtime/Core/ScriptableObjectCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public LongGuid GUID
3333

3434
[SerializeField, HideInInspector]
3535
private bool automaticallyLoaded = true;
36-
internal bool AutomaticallyLoaded => automaticallyLoaded;
36+
public bool AutomaticallyLoaded => automaticallyLoaded;
3737

3838
public ScriptableObject this[int index]
3939
{

0 commit comments

Comments
 (0)