1414using UnityEditor . AddressableAssets . Build ;
1515using UnityEditor . AddressableAssets . Settings ;
1616using UnityEditor . AddressableAssets . Settings . GroupSchemas ;
17+ using UnityEditor . Compilation ;
1718using UnityEditor . SceneManagement ;
1819using UnityEngine ;
1920using System . Reflection ;
@@ -48,11 +49,23 @@ private sealed class AddressablesBuildStateSnapshot
4849 public ScriptingImplementation StandaloneScriptingBackend ;
4950 }
5051
52+ public sealed class ExportRequiresRecompileException : InvalidOperationException
53+ {
54+ public ExportRequiresRecompileException ( string message )
55+ : base ( message )
56+ {
57+ }
58+ }
59+
5160 private static bool EditorCompiledWithJsonCatalog
5261 {
5362 get
5463 {
64+ #if ENABLE_JSON_CATALOG
5565 return true ;
66+ #else
67+ return false ;
68+ #endif
5669 }
5770 }
5871
@@ -67,13 +80,21 @@ public AssetExporter(MTIONSDKAssetBase assetBase, ExportLocationOptions exportLo
6780 this . exportLocationOptions = exportLocationOptions ;
6881 }
6982
83+ public static void EnsureProjectReadyForJsonCatalogExport ( )
84+ {
85+ AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject . GetSettings ( true ) ;
86+ EnsureJsonCatalogConfiguration ( settings ) ;
87+ }
88+
7089 public void ExportSDKAsset ( )
7190 {
7291 if ( assetBase == null )
7392 {
7493 throw new ArgumentNullException ( ) ;
7594 }
7695
96+ EnsureProjectReadyForJsonCatalogExport ( ) ;
97+
7798 SDKServerManager . VerifyAssetGuid ( assetBase ) ;
7899
79100
@@ -406,20 +427,19 @@ public void RemoveAddressableGroup()
406427
407428 public void ExportAsAddressableAssetBundle ( Action onExportComplete )
408429 {
430+ AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject . GetSettings ( true ) ;
431+
409432 var localUnityDirectory = SDKUtil . GetSDKLocalUnityBuildPath ( assetBase , exportLocationOptions ) ;
410433 if ( Directory . Exists ( localUnityDirectory ) )
411434 {
412435 Directory . Delete ( localUnityDirectory , true ) ;
413436 }
414437 Directory . CreateDirectory ( localUnityDirectory ) ;
415438
416- AddressableAssetSettings settings = AddressableAssetSettingsDefaultObject . GetSettings ( true ) ;
417439 AddressablesBuildStateSnapshot buildState = CaptureAddressablesBuildState ( settings ) ;
418440
419441 try
420442 {
421- EnsureJsonCatalogConfiguration ( settings ) ;
422-
423443 settings . BuildRemoteCatalog = true ;
424444 settings . DisableCatalogUpdateOnStartup = true ;
425445 settings . ContiguousBundles = true ;
@@ -440,13 +460,12 @@ public void ExportAsAddressableAssetBundle(Action onExportComplete)
440460
441461 foreach ( Tuple < BuildTargetGroup , BuildTarget > target in targets )
442462 {
443- while ( EditorApplication . isUpdating )
463+ if ( EditorUserBuildSettings . activeBuildTarget != target . Item2 )
444464 {
445- Thread . Sleep ( 10 ) ;
465+ throw new InvalidOperationException (
466+ $ "The active build target must remain { target . Item2 } during export. Wait for Unity to finish updating, then start the export again.") ;
446467 }
447468
448- EditorUserBuildSettings . SwitchActiveBuildTarget ( target . Item1 , target . Item2 ) ;
449-
450469 AddressableAssetProfileSettings profile = settings . profileSettings ;
451470 string profileId = profile . GetProfileId ( groupProfileName ) ;
452471 if ( string . IsNullOrEmpty ( profileId ) )
@@ -735,50 +754,128 @@ private static string FindBestCatalogCandidate(string targetPath, string extensi
735754 . FirstOrDefault ( ) ;
736755 }
737756
757+
738758 private static void EnsureJsonCatalogConfiguration ( AddressableAssetSettings settings )
739759 {
740760 if ( settings == null )
741761 {
742762 throw new InvalidOperationException ( "Addressables settings are missing." ) ;
743763 }
744764
765+ if ( EditorApplication . isCompiling || EditorApplication . isUpdating )
766+ {
767+ throw new ExportRequiresRecompileException (
768+ "Unity is still compiling or updating scripts. Wait for Unity to finish, then start the export again." ) ;
769+ }
770+
771+ List < string > changes = new List < string > ( ) ;
745772 bool changedSettings = false ;
746773 if ( ! settings . EnableJsonCatalog )
747774 {
748775 settings . EnableJsonCatalog = true ;
749776 changedSettings = true ;
777+ changes . Add ( "enabled Addressables 'Enable Json Catalog'" ) ;
750778 }
751779
752- if ( TryGetAddressablesBool ( settings , "BundleLocalCatalog" ) != false )
780+ bool ? bundleLocalCatalog = TryGetAddressablesBool ( settings , "BundleLocalCatalog" ) ;
781+ if ( bundleLocalCatalog . HasValue && bundleLocalCatalog . Value )
753782 {
754- TrySetAddressablesBool ( settings , "BundleLocalCatalog" , false ) ;
755- changedSettings = true ;
783+ if ( TrySetAddressablesBool ( settings , "BundleLocalCatalog" , false ) )
784+ {
785+ changedSettings = true ;
786+ changes . Add ( "disabled Addressables 'Bundle Local Catalog'" ) ;
787+ }
756788 }
757789
758790 if ( changedSettings )
759791 {
792+ EditorUtility . SetDirty ( settings ) ;
760793 AssetDatabase . SaveAssets ( ) ;
761794 }
762795
763- string standaloneSymbols = PlayerSettings . GetScriptingDefineSymbolsForGroup ( BuildTargetGroup . Standalone ) ?? string . Empty ;
796+ string standaloneSymbols = GetStandaloneScriptingDefineSymbols ( ) ?? string . Empty ;
764797 if ( ! HasDefineSymbol ( standaloneSymbols , EnableJsonCatalogDefine ) )
765798 {
766799 string updatedSymbols = string . IsNullOrWhiteSpace ( standaloneSymbols )
767800 ? EnableJsonCatalogDefine
768801 : $ "{ standaloneSymbols } ;{ EnableJsonCatalogDefine } ";
769- PlayerSettings . SetScriptingDefineSymbolsForGroup ( BuildTargetGroup . Standalone , updatedSymbols ) ;
770- AssetDatabase . SaveAssets ( ) ;
771- throw new InvalidOperationException (
772- $ "Enabled the '{ EnableJsonCatalogDefine } ' scripting define for Standalone. Wait for Unity to finish recompiling scripts, then rerun export.") ;
802+ SetStandaloneScriptingDefineSymbols ( updatedSymbols ) ;
803+ changes . Add ( $ "added Standalone scripting define '{ EnableJsonCatalogDefine } '") ;
804+ }
805+
806+ if ( EnsureStandaloneWindows64Target ( ) )
807+ {
808+ changes . Add ( $ "switched the active build target to { BuildTarget . StandaloneWindows64 } ") ;
809+ }
810+
811+ if ( changes . Count > 0 )
812+ {
813+ RequestEditorScriptRecompile ( ) ;
814+ throw new ExportRequiresRecompileException ( BuildRecompileRequiredMessage ( changes ) ) ;
773815 }
774816
775817 if ( ! EditorCompiledWithJsonCatalog )
776818 {
777- throw new InvalidOperationException (
778- $ "The editor is currently compiled without '{ EnableJsonCatalogDefine } '. Wait for Unity to finish recompiling scripts, then rerun export.") ;
819+ RequestEditorScriptRecompile ( ) ;
820+ throw new ExportRequiresRecompileException (
821+ $ "The SDK detected that Unity is still compiled without '{ EnableJsonCatalogDefine } '. The SDK requested a script recompile. Wait for Unity to finish recompiling, then start the export again.") ;
779822 }
780823 }
781824
825+ private static bool EnsureStandaloneWindows64Target ( )
826+ {
827+ if ( EditorUserBuildSettings . activeBuildTarget == BuildTarget . StandaloneWindows64 )
828+ {
829+ EditorUserBuildSettings . selectedStandaloneTarget = BuildTarget . StandaloneWindows64 ;
830+ return false ;
831+ }
832+
833+ EditorUserBuildSettings . selectedStandaloneTarget = BuildTarget . StandaloneWindows64 ;
834+ if ( ! EditorUserBuildSettings . SwitchActiveBuildTarget ( BuildTargetGroup . Standalone , BuildTarget . StandaloneWindows64 ) )
835+ {
836+ throw new InvalidOperationException ( "The SDK could not switch the active build target to StandaloneWindows64." ) ;
837+ }
838+
839+ return true ;
840+ }
841+
842+ private static string BuildRecompileRequiredMessage ( IEnumerable < string > changes )
843+ {
844+ string changeSummary = string . Join ( ", " , changes ) ;
845+ return $ "The SDK updated the project for JSON catalog exports ({ changeSummary } ) and requested a script recompile. Wait for Unity to finish recompiling, then start the export again.";
846+ }
847+
848+ private static void RequestEditorScriptRecompile ( )
849+ {
850+ AssetDatabase . SaveAssets ( ) ;
851+ AssetDatabase . Refresh ( ) ;
852+
853+ if ( EditorApplication . isCompiling || EditorApplication . isUpdating )
854+ {
855+ return ;
856+ }
857+
858+ CompilationPipeline . RequestScriptCompilation ( RequestScriptCompilationOptions . CleanBuildCache ) ;
859+ }
860+
861+ private static string GetStandaloneScriptingDefineSymbols ( )
862+ {
863+ #if UNITY_2021_2_OR_NEWER
864+ return PlayerSettings . GetScriptingDefineSymbols ( UnityEditor . Build . NamedBuildTarget . Standalone ) ;
865+ #else
866+ return PlayerSettings . GetScriptingDefineSymbolsForGroup ( BuildTargetGroup . Standalone ) ;
867+ #endif
868+ }
869+
870+ private static void SetStandaloneScriptingDefineSymbols ( string defineSymbols )
871+ {
872+ #if UNITY_2021_2_OR_NEWER
873+ PlayerSettings . SetScriptingDefineSymbols ( UnityEditor . Build . NamedBuildTarget . Standalone , defineSymbols ) ;
874+ #else
875+ PlayerSettings . SetScriptingDefineSymbolsForGroup ( BuildTargetGroup . Standalone , defineSymbols ) ;
876+ #endif
877+ }
878+
782879 private static bool HasDefineSymbol ( string defineSymbols , string symbol )
783880 {
784881 return defineSymbols . Split ( new [ ] { ';' } , StringSplitOptions . RemoveEmptyEntries )
0 commit comments