1- using UnityEditor ;
1+ using System ;
2+ using System . Collections . Generic ;
3+ using UnityEditor ;
24using UnityEditor . Build ;
35using UnityEditor . Build . Reporting ;
46using UnityEngine ;
57
68// This is a bit of a hack, but we need to ensure that our metadata is not stripped on build,
7- // so at build time we clone them into a temporary resources directory. Afterwards we delete the directory.
8- // Cloning works since each metadata object holds a reference to the thing it belongs to and we can
9- // just use the clone at runtime instead.
9+ // so at build time we clone them into a temporary resources directory. Cloning works since
10+ // each metadata object holds a reference to the thing it belongs to and we can just use the
11+ // clone at runtime instead. After we've created the build we delete the directory .
1012public class MetadataLookupPreprocessBuild : IPreprocessBuildWithReport
1113{
1214 public int callbackOrder { get { return 0 ; } }
1315
1416 public static bool HadResourcesDirectory = false ;
1517
18+ CustomAssetMetadata CloneCustomAssetMetadata ( CustomAssetMetadata assetMetadata , string basePath , ref int index )
19+ {
20+ var clone = UnityEngine . Object . Instantiate ( assetMetadata ) ;
21+ var name = $ "{ assetMetadata . GetInstanceID ( ) } -{ index } "; index ++ ;
22+ AssetDatabase . CreateAsset ( clone , $ "{ basePath } /{ name } .asset") ;
23+ return clone ;
24+ }
25+
1626 public void OnPreprocessBuild ( BuildReport report )
1727 {
1828 const string basePath = "Assets/Resources/" + MetadataLookup . kResourcePath ;
@@ -22,17 +32,34 @@ public void OnPreprocessBuild(BuildReport report)
2232 System . IO . Directory . Delete ( basePath , true ) ;
2333 System . IO . Directory . CreateDirectory ( basePath ) ;
2434 var list = ScriptableObject . CreateInstance < MetadataLookupAsset > ( ) ;
25- var allMetadata = Resources . FindObjectsOfTypeAll < CustomAssetMetadata > ( ) ;
35+
36+ var allMetadata = new List < CustomAssetMetadata > ( ) ;
37+
38+ var allPaths = UnityEditor . AssetDatabase . FindAssets ( $ "t:Material") ; // TODO: support more than just materials
39+ foreach ( var assetPath in allPaths )
40+ {
41+ foreach ( var item in UnityEditor . AssetDatabase . LoadAllAssetRepresentationsAtPath ( assetPath ) )
42+ {
43+ if ( item is not CustomAssetMetadata metadata )
44+ continue ;
45+ allMetadata . Add ( metadata ) ;
46+ }
47+ UnityEditor . EditorUtility . UnloadUnusedAssetsImmediate ( ) ;
48+ }
49+
50+ // This doesn't work since Resources.FindObjectsOfTypeAll only works on
51+ // assets currently loaded into memory.
52+ //allMetadata.AddRange(Resources.FindObjectsOfTypeAll<CustomAssetMetadata>());
53+
2654 int index = 0 ;
27- for ( int i = 0 ; i < allMetadata . Length ; i ++ )
28- {
29- var clone = UnityEngine . Object . Instantiate ( allMetadata [ i ] ) ;
30- var name = $ "{ allMetadata [ i ] . GetInstanceID ( ) } -{ index } "; index ++ ;
31- AssetDatabase . CreateAsset ( clone , $ "{ basePath } /{ name } .asset") ;
32- allMetadata [ i ] = clone ;
55+ for ( int i = 0 ; i < allMetadata . Count ; i ++ )
56+ {
57+ allMetadata [ i ] = CloneCustomAssetMetadata ( allMetadata [ i ] , basePath , ref index ) ;
3358 }
34- list . allMetadata = allMetadata ;
35- AssetDatabase . CreateAsset ( list , $ "{ basePath } /{ MetadataLookup . kAssetName } .asset") ;
59+ UnityEditor . EditorUtility . UnloadUnusedAssetsImmediate ( ) ;
60+
61+ list . allMetadata = allMetadata . ToArray ( ) ;
62+ AssetDatabase . CreateAsset ( list , $ "{ basePath } /{ MetadataLookup . kAssetName } .asset") ;
3663 AssetDatabase . StopAssetEditing ( ) ;
3764 }
3865
0 commit comments