Skip to content

Commit ca6d4ba

Browse files
chkuang-gGerrit Code Review
authored andcommitted
Merge changes I7b0add0b,Id6388347,Id5611bcf
* changes: Update README Delay Android Resolver initialization Delay iOS Resolver initialization
2 parents db42039 + 310a5c2 commit ca6d4ba

File tree

3 files changed

+118
-39
lines changed

3 files changed

+118
-39
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ package:
172172
you add the `-gvh_disable` option.
173173
1. Export your plugin by [running Unity from the command line](https://docs.unity3d.com/Manual/CommandLineArguments.html), ensuring that
174174
you:
175-
- Include the contents of the `Assets/PlayServicesResolver` directory.
175+
- Include the contents of the `Assets/PlayServicesResolver` and
176+
`Assets/ExternalDependencyManager` directory.
176177
- Add the `-gvh_disable` option.
177178

178179
You **must** specify the `-gvh_disable` option in order for the Version

source/AndroidResolver/src/PlayServicesResolver.cs

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -901,35 +901,74 @@ public static bool AutomaticResolutionEnabled {
901901
/// Initializes the <see cref="GooglePlayServices.PlayServicesResolver"/> class.
902902
/// </summary>
903903
static PlayServicesResolver() {
904-
// Create the resolver.
905-
if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android) {
906-
gradleResolver = new GradleResolver();
907-
// Monitor Android dependency XML files to perform auto-resolution.
908-
AddAutoResolutionFilePatterns(xmlDependencies.fileRegularExpressions);
904+
// Cache the flag to prevent string comparison in every frame during
905+
// PollOnUpdateUntilComplete()
906+
bool isExecuteMethodEnabled = ExecutionEnvironment.ExecuteMethodEnabled;
907+
908+
// Delay initialization until the build target is iOS and the editor is not in play
909+
// mode.
910+
RunOnMainThread.PollOnUpdateUntilComplete(() => {
911+
if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android ||
912+
EditorApplication.isPlayingOrWillChangePlaymode) {
913+
// If Unity is launched with -executeMethod, in some Unity versions, editor
914+
// update will never be called. As a result, PollOnUpdateUntilComplete() will
915+
// attempt to call this poll function repeating on current thread until it
916+
// returns true. Therefore, return true immediately and stop the polling in
917+
// executeMethod mode.
918+
return isExecuteMethodEnabled;
919+
}
920+
Initialize();
921+
return true;
922+
});
923+
924+
}
909925

910-
svcSupport = PlayServicesSupport.CreateInstance(
911-
"PlayServicesResolver",
912-
AndroidSdkRoot,
913-
"ProjectSettings",
914-
logMessageWithLevel: LogDelegate);
926+
/// <summary>
927+
/// Whether Android Resolver have been initialized.
928+
/// </summary>
929+
private static bool isInitialized = false;
930+
931+
/// <summary>
932+
/// Initialize the module. This should be called on the main thread only if
933+
/// current active build target is Android and not in play mode.
934+
/// </summary>
935+
private static void Initialize() {
936+
if (isInitialized) return;
937+
938+
if ( EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android ) {
939+
throw new Exception("PlayServiceResolver.Initialize() is called when active " +
940+
"build target is not Android. This should never happen. If it does, " +
941+
"please report to the developer.");
915942
}
943+
944+
// Create the resolver.
945+
gradleResolver = new GradleResolver();
946+
// Monitor Android dependency XML files to perform auto-resolution.
947+
AddAutoResolutionFilePatterns(xmlDependencies.fileRegularExpressions);
948+
949+
svcSupport = PlayServicesSupport.CreateInstance(
950+
"PlayServicesResolver",
951+
AndroidSdkRoot,
952+
"ProjectSettings",
953+
logMessageWithLevel: LogDelegate);
954+
916955
RunOnMainThread.OnUpdate -= PollBundleId;
917956
RunOnMainThread.OnUpdate += PollBundleId;
918957

919958
// Initialize settings and resolve if required.
920959
OnSettingsChanged();
921960

922961
// Setup events for auto resolution.
923-
if (EditorUserBuildSettings.activeBuildTarget == BuildTarget.Android) {
924-
BundleIdChanged += ResolveOnBundleIdChanged;
925-
AndroidBuildSystemChanged += ResolveOnBuildSystemChanged;
926-
AndroidAbisChanged += ResolveOnAndroidAbisChanged;
927-
AndroidSdkRootChanged += ResolveOnAndroidSdkRootChange;
928-
Reresolve();
962+
BundleIdChanged += ResolveOnBundleIdChanged;
963+
AndroidBuildSystemChanged += ResolveOnBuildSystemChanged;
964+
AndroidAbisChanged += ResolveOnAndroidAbisChanged;
965+
AndroidSdkRootChanged += ResolveOnAndroidSdkRootChange;
966+
Reresolve();
929967

930-
if (SettingsDialogObj.EnableAutoResolution) LinkAutoResolution();
931-
}
968+
if (SettingsDialogObj.EnableAutoResolution) LinkAutoResolution();
932969

970+
isInitialized = true;
971+
Log("Android Resolver Initialized", level: LogLevel.Verbose);
933972
}
934973

935974
// Unregister events to monitor build system changes for the Android Resolver and other

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)