Skip to content

Commit c480fd8

Browse files
committed
Delay iOS Resolver initialization
Delay iOS Resolver initialization until 1. Current active build target is iOS 2. When the editor is NOT in play mode This is to prevent unnecessary initialization when the play mode start and AppDomain reload. Improves googlesamples#639 Bug: 154730431 Change-Id: Id5611bcfa4136e9808cc7d5fc9e832479622e8d1
1 parent 594ff6d commit c480fd8

File tree

1 file changed

+59
-20
lines changed

1 file changed

+59
-20
lines changed

source/IOSResolver/src/IOSResolver.cs

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -651,54 +651,90 @@ private static Assembly ResolveUnityEditoriOSXcodeExtension(
651651
static IOSResolver() {
652652
// Load log preferences.
653653
VerboseLoggingEnabled = VerboseLoggingEnabled;
654+
654655
// NOTE: We can't reference the UnityEditor.iOS.Xcode module in this
655656
// method as the Mono runtime in Unity 4 and below requires all
656657
// dependencies of a method are loaded before the method is executed
657658
// so we install the DLL loader first then try using the Xcode module.
658659
RemapXcodeExtension();
660+
661+
// Cache the flag to prevent string comparison in every frame during
662+
// PollOnUpdateUntilComplete()
663+
bool isExecuteMethodEnabled = ExecutionEnvironment.ExecuteMethodEnabled;
664+
665+
// Delay initialization until the build target is iOS and the editor is not in play mode.
666+
RunOnMainThread.PollOnUpdateUntilComplete(() => {
667+
if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.iOS ||
668+
EditorApplication.isPlayingOrWillChangePlaymode) {
669+
// If Unity is launched with -executeMethod, in some Unity versions, editor
670+
// update will never be called. As a result, PollOnUpdateUntilComplete() will
671+
// attempt to call this poll function repeating on current thread until it returns
672+
// true. Therefore, return true immediately and stop the polling in executeMethod
673+
// mode.
674+
return isExecuteMethodEnabled;
675+
}
676+
Initialize();
677+
return true;
678+
});
679+
}
680+
681+
/// <summary>
682+
/// Whether iOSResolver have been initialized.
683+
/// </summary>
684+
private static bool isInitialized = false;
685+
686+
/// <summary>
687+
/// Initialize the module. This should be called on the main thread only if
688+
/// current active build target is iOS and not in play mode.
689+
/// </summary>
690+
private static void Initialize() {
691+
if (isInitialized) return;
692+
693+
if ( EditorUserBuildSettings.activeBuildTarget != BuildTarget.iOS ) {
694+
throw new Exception("IOSResolver.Initialize() is called when active build target " +
695+
"is not iOS. This should never happen. If it does, please report to the " +
696+
"developer.");
697+
}
698+
659699
// NOTE: It's not possible to catch exceptions a missing reference
660700
// to the UnityEditor.iOS.Xcode assembly in this method as the runtime
661701
// will attempt to load the assembly before the method is executed so
662702
// we handle exceptions here.
663703
try {
664704
InitializeTargetName();
665705
} catch (Exception exception) {
666-
if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS) {
667-
Log("Failed: " + exception.ToString(), level: LogLevel.Error);
668-
if (exception is FileNotFoundException ||
669-
exception is TypeInitializationException ||
670-
exception is TargetInvocationException) {
671-
// It's likely we failed to load the iOS Xcode extension.
672-
Debug.LogWarning(
673-
"Failed to load the " +
674-
"UnityEditor.iOS.Extensions.Xcode dll. " +
675-
"Is iOS support installed?");
676-
} else {
677-
throw exception;
678-
}
706+
Log("Failed: " + exception.ToString(), level: LogLevel.Error);
707+
if (exception is FileNotFoundException ||
708+
exception is TypeInitializationException ||
709+
exception is TargetInvocationException) {
710+
// It's likely we failed to load the iOS Xcode extension.
711+
Debug.LogWarning(
712+
"Failed to load the " +
713+
"UnityEditor.iOS.Extensions.Xcode dll. " +
714+
"Is iOS support installed?");
715+
} else {
716+
throw exception;
679717
}
680718
}
681719

682720
// If Cocoapod tool auto-installation is enabled try installing on the first update of
683721
// the editor when the editor environment has been initialized.
684-
if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS &&
685-
AutoPodToolInstallInEditorEnabled && CocoapodsIntegrationEnabled &&
722+
if (AutoPodToolInstallInEditorEnabled && CocoapodsIntegrationEnabled &&
686723
!ExecutionEnvironment.InBatchMode) {
687-
RunOnMainThread.Run(() => { AutoInstallCocoapods(); }, runNow: false);
724+
AutoInstallCocoapods();
688725
}
689726
// Install / remove target SDK property poller.
690727
SetEnablePollTargetSdk(PodfileGenerationEnabled);
691728
// Load XML dependencies on the next editor update.
692729
if (PodfileGenerationEnabled) {
693-
RunOnMainThread.Run(RefreshXmlDependencies, runNow: false);
730+
RefreshXmlDependencies();
694731
}
695732

696733
// Prompt the user to use workspaces if they aren't at least using project level
697734
// integration.
698-
if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.iOS &&
699-
(CocoapodsIntegrationMethod)settings.GetInt(PREFERENCE_COCOAPODS_INTEGRATION_METHOD,
735+
if ((CocoapodsIntegrationMethod)settings.GetInt(PREFERENCE_COCOAPODS_INTEGRATION_METHOD,
700736
CocoapodsIntegrationUpgradeDefault) == CocoapodsIntegrationMethod.None &&
701-
!ExecutionEnvironment.InBatchMode && !UpgradeToWorkspaceWarningDisabled) {
737+
!ExecutionEnvironment.InBatchMode && !UpgradeToWorkspaceWarningDisabled) {
702738

703739
DialogWindow.Display(
704740
"Warning: CocoaPods integration is disabled!",
@@ -721,6 +757,9 @@ exception is TypeInitializationException ||
721757
}
722758
});
723759
}
760+
isInitialized = true;
761+
762+
Log("IOSResolver Initialized", verbose: true);
724763
}
725764

726765
/// <summary>

0 commit comments

Comments
 (0)