Skip to content

Commit 79794cd

Browse files
authored
Fixes googlesamples#322, Use the raw property value when the enum does not contain this value (googlesamples#399)
* Fixes googlesamples#322, Use the raw property value when the enum does not contain this value
1 parent a88a050 commit 79794cd

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

source/AndroidResolver/src/UnityCompat.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,20 @@ private static int AndroidPlatformVersionFallback {
5555
}
5656

5757
// Parses a UnityEditor.AndroidSDKVersion enum for a value.
58-
private static int VersionFromAndroidSDKVersionsEnum(string enumName, string fallbackPrefKey,
58+
private static int VersionFromAndroidSDKVersionsEnum(object enumValue, string fallbackPrefKey,
5959
int fallbackValue) {
60-
// If the enum property has no name it's not possible to parse the version so fallback to
61-
// auto-selection.
62-
if (String.IsNullOrEmpty(enumName)) return -1;
60+
string enumName = null;
61+
try {
62+
enumName = Enum.GetName(typeof(AndroidSdkVersions), enumValue);
63+
} catch (ArgumentException) {
64+
//Fall back on auto if the enum value is not parsable
65+
return -1;
66+
}
67+
68+
// If the enumName is empty then enumValue was not represented in the enum,
69+
// most likely because Unity has not yet added the new version,
70+
// fall back on the raw enumValue
71+
if (String.IsNullOrEmpty(enumName)) return (int)enumValue;
6372

6473
if (enumName.StartsWith(UNITY_ANDROID_VERSION_ENUM_PREFIX)) {
6574
enumName = enumName.Substring(UNITY_ANDROID_VERSION_ENUM_PREFIX.Length);
@@ -91,7 +100,7 @@ private static int VersionFromAndroidSDKVersionsEnum(string enumName, string fal
91100
/// <returns>the sdk value (ie. 24 for Android 7.0 Nouget)</returns>
92101
public static int GetAndroidMinSDKVersion() {
93102
int minSdkVersion = VersionFromAndroidSDKVersionsEnum(
94-
PlayerSettings.Android.minSdkVersion.ToString(),
103+
(object)PlayerSettings.Android.minSdkVersion,
95104
ANDROID_MIN_SDK_FALLBACK_KEY, MinSDKVersionFallback);
96105
if (minSdkVersion == -1)
97106
return MinSDKVersionFallback;
@@ -121,7 +130,7 @@ public static int GetAndroidTargetSDKVersion() {
121130
var property = typeof(UnityEditor.PlayerSettings.Android).GetProperty("targetSdkVersion");
122131
int apiLevel = property == null ? -1 :
123132
VersionFromAndroidSDKVersionsEnum(
124-
Enum.GetName(property.PropertyType, property.GetValue(null, null)),
133+
property.GetValue(null, null),
125134
ANDROID_PLATFORM_FALLBACK_KEY, AndroidPlatformVersionFallback);
126135
if (apiLevel >= 0) return apiLevel;
127136
return FindNewestInstalledAndroidSDKVersion();

0 commit comments

Comments
 (0)