Skip to content

Commit a36f87b

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

File tree

1 file changed

+57
-18
lines changed

1 file changed

+57
-18
lines changed

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

0 commit comments

Comments
 (0)