7
7
using UnityEditor . Compilation ;
8
8
using UnityEditorInternal ;
9
9
using UnityEngine ;
10
+ #if ADDRESSABLES_ENABLED
11
+ using UnityEditor . AddressableAssets ;
12
+ using UnityEditor . AddressableAssets . Settings ;
13
+ #endif
10
14
11
15
namespace BrunoMikoski . ScriptableObjectCollections
12
16
{
13
17
public static class CodeGenerationUtility
14
18
{
19
+ private const string PrivateValuesName = "cachedValues" ;
20
+ private const string PublicValuesName = "Values" ;
21
+ private const string HasCachedValuesName = "hasCachedValues" ;
22
+
23
+
15
24
public static bool CreateNewScript (
16
25
string fileName , string parentFolder , string nameSpace , string [ ] directives , params string [ ] lines )
17
26
{
@@ -401,6 +410,12 @@ public static void GenerateStaticCollectionScript(ScriptableObjectCollection col
401
410
402
411
WriteDirectAccessCollectionStatic ( collection , writer , ref indentation , useBaseClass ) ;
403
412
413
+ if ( ! collection . AutomaticallyLoaded )
414
+ {
415
+ WriteNonAutomaticallyLoadedCollectionItems ( collection , writer , ref indentation , useBaseClass ) ;
416
+ }
417
+
418
+
404
419
indentation -- ;
405
420
AppendFooter ( writer , ref indentation , nameSpace ) ;
406
421
}
@@ -409,6 +424,8 @@ public static void GenerateStaticCollectionScript(ScriptableObjectCollection col
409
424
AssetDatabase . Refresh ( ) ;
410
425
}
411
426
427
+
428
+
412
429
private static bool CanGenerateStaticFile ( ScriptableObjectCollection collection , out string errorMessage )
413
430
{
414
431
CollectionsRegistry . Instance . ValidateCollections ( ) ;
@@ -454,16 +471,22 @@ private static string[] GetCollectionDirectives(ScriptableObjectCollection colle
454
471
for ( int i = 0 ; i < collection . Count ; i ++ )
455
472
directives . Add ( collection [ i ] . GetType ( ) . Namespace ) ;
456
473
474
+ if ( ! collection . AutomaticallyLoaded )
475
+ {
476
+ #if ADDRESSABLES_ENABLED
477
+ directives . Add ( "UnityEngine.AddressableAssets" ) ;
478
+ directives . Add ( "UnityEngine.ResourceManagement.AsyncOperations" ) ;
479
+ #endif
480
+ }
481
+
457
482
return directives . ToArray ( ) ;
458
483
}
459
484
460
485
private static void WriteDirectAccessCollectionStatic ( ScriptableObjectCollection collection , StreamWriter writer ,
461
486
ref int indentation , bool useBaseClass )
462
487
{
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 } ;") ;
467
490
468
491
AppendLine ( writer , indentation ) ;
469
492
@@ -479,23 +502,22 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
479
502
480
503
AppendLine ( writer , indentation ) ;
481
504
482
- string valuesName = $ "Values";
483
505
484
506
AppendLine ( writer , indentation ,
485
- $ "public static { collection . GetType ( ) . FullName } { valuesName } ") ;
507
+ $ "public static { collection . GetType ( ) . FullName } { PublicValuesName } ") ;
486
508
487
509
AppendLine ( writer , indentation , "{" ) ;
488
510
indentation ++ ;
489
511
AppendLine ( writer , indentation , "get" ) ;
490
512
AppendLine ( writer , indentation , "{" ) ;
491
513
indentation ++ ;
492
- AppendLine ( writer , indentation , $ "if (!{ hasCachedValuesNames } )") ;
514
+ AppendLine ( writer , indentation , $ "if (!{ HasCachedValuesName } )") ;
493
515
indentation ++ ;
494
516
( long , long ) collectionGUIDValues = collection . GUID . GetRawValues ( ) ;
495
517
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 } );") ;
497
519
indentation -- ;
498
- AppendLine ( writer , indentation , $ "return { cachedValuesName } ;") ;
520
+ AppendLine ( writer , indentation , $ "return { PrivateValuesName } ;") ;
499
521
indentation -- ;
500
522
AppendLine ( writer , indentation , "}" ) ;
501
523
indentation -- ;
@@ -527,7 +549,7 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
527
549
indentation ++ ;
528
550
( long , long ) collectionItemGUIDValues = socItem . GUID . GetRawValues ( ) ;
529
551
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 } );") ;
531
553
indentation -- ;
532
554
AppendLine ( writer , indentation , $ "return { privateStaticCachedName } ;") ;
533
555
indentation -- ;
@@ -539,6 +561,53 @@ private static void WriteDirectAccessCollectionStatic(ScriptableObjectCollection
539
561
540
562
AppendLine ( writer , indentation ) ;
541
563
}
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
+
542
611
543
612
public static bool DoesStaticFileForCollectionExist ( ScriptableObjectCollection collection )
544
613
{
0 commit comments