Skip to content

Commit b0b8d91

Browse files
committed
Bump version 0.20.2
1 parent 9342f1e commit b0b8d91

28 files changed

Lines changed: 417 additions & 77 deletions

Editor/BuildScripts/AssetExporter.cs

Lines changed: 114 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using UnityEditor.AddressableAssets.Build;
1515
using UnityEditor.AddressableAssets.Settings;
1616
using UnityEditor.AddressableAssets.Settings.GroupSchemas;
17+
using UnityEditor.Compilation;
1718
using UnityEditor.SceneManagement;
1819
using UnityEngine;
1920
using 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)

Editor/BuildScripts/BuildManager.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public float ExportPercentComplete
103103

104104
private void BuildApplicationAssetBundle()
105105
{
106+
AssetExporter.EnsureProjectReadyForJsonCatalogExport();
107+
106108
var builder = AddressableAssetSettingsDefaultObject.Settings.ActivePlayerDataBuilder;
107109
AddressableAssetSettings.CleanPlayerContent(builder);
108110
AddressableAssetSettings.BuildPlayerContent();
@@ -136,6 +138,16 @@ private void BuildAndExportSdkScene()
136138
roomObject.transform.rotation == Quaternion.identity &&
137139
roomObject.transform.localScale == Vector3.one)
138140
{
141+
try
142+
{
143+
AssetExporter.EnsureProjectReadyForJsonCatalogExport();
144+
}
145+
catch (AssetExporter.ExportRequiresRecompileException ex)
146+
{
147+
EditorUtility.DisplayDialog("SDK Export Recompile Required", ex.Message, "Close");
148+
return;
149+
}
150+
139151
exportStartingScene = EditorSceneManager.GetActiveScene();
140152
exportTaskRunning = true;
141153
sceneExporter = new SceneExporter(sceneObjectDescriptor);
@@ -150,8 +162,16 @@ private void BuildAndExportSdkScene()
150162
if (sceneExporter != null && !sceneExporter.IsCompleted)
151163
{
152164
sceneExporter.FailReport(ex);
153-
Debug.LogException(ex);
154-
EditorUtility.DisplayDialog("SDK Export Failed", ex.Message, "Close");
165+
166+
if (ex is AssetExporter.ExportRequiresRecompileException)
167+
{
168+
EditorUtility.DisplayDialog("SDK Export Recompile Required", ex.Message, "Close");
169+
}
170+
else
171+
{
172+
Debug.LogException(ex);
173+
EditorUtility.DisplayDialog("SDK Export Failed", ex.Message, "Close");
174+
}
155175
}
156176
}
157177
}
@@ -167,8 +187,15 @@ private void OnExportFinished(SceneExporter exporter, Exception ex)
167187

168188
if (ex != null)
169189
{
170-
Debug.LogException(ex);
171-
EditorUtility.DisplayDialog("SDK Export Failed", ex.Message, "Close");
190+
if (ex is AssetExporter.ExportRequiresRecompileException)
191+
{
192+
EditorUtility.DisplayDialog("SDK Export Recompile Required", ex.Message, "Close");
193+
}
194+
else
195+
{
196+
Debug.LogException(ex);
197+
EditorUtility.DisplayDialog("SDK Export Failed", ex.Message, "Close");
198+
}
172199
}
173200

174201
if (exportStartingScene.IsValid() && exportStartingScene.isLoaded)

Editor/BuildScripts/SceneExporter.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,12 @@ private void ExportRoomSceneData(MTIONSDKDescriptorSceneBase sceneBase)
625625
DeleteAssetExport(sceneBase, sceneBase.LocationOption);
626626
RestoreAssetExportBackup(sceneBase, sceneBase.LocationOption);
627627
DeleteAssetExportBackup(sceneBase, sceneBase.LocationOption);
628+
629+
if (ex is AssetExporter.ExportRequiresRecompileException)
630+
{
631+
throw;
632+
}
633+
628634
throw new Exception($"Error while exporting ROOM {internalId}, restored backup: {ex}");
629635
}
630636
finally
@@ -680,6 +686,12 @@ private void ExportEnvironmentSceneData(MTIONSDKDescriptorSceneBase sceneBase)
680686
DeleteAssetExport(sceneBase, sceneBase.LocationOption);
681687
RestoreAssetExportBackup(sceneBase, sceneBase.LocationOption);
682688
DeleteAssetExportBackup(sceneBase, sceneBase.LocationOption);
689+
690+
if (ex is AssetExporter.ExportRequiresRecompileException)
691+
{
692+
throw;
693+
}
694+
683695
throw new Exception($"Error while exporting ENVIRONMENT {internalId}, restored backup: {ex}");
684696
}
685697
finally
@@ -747,6 +759,12 @@ private void ExportVirtualAssetData(MTIONSDKAssetBase assetBase)
747759
DeleteAssetExport(backupTarget, LocationOptions);
748760
RestoreAssetExportBackup(backupTarget, LocationOptions);
749761
DeleteAssetExportBackup(backupTarget, LocationOptions);
762+
763+
if (ex is AssetExporter.ExportRequiresRecompileException)
764+
{
765+
throw;
766+
}
767+
750768
throw new Exception($"Error while exporting ASSET {internalId}, restored backup: {ex}");
751769
}
752770
finally

0 commit comments

Comments
 (0)