| 1 | using System.Collections; |
| 2 | using System.Collections.Generic; |
| 3 | using UnityEngine; |
| 4 | using UnityEditor; |
| 5 | using System.IO; |
| 6 | using System; |
| 7 | |
| 8 | #if UNITY_EDITOR |
| 9 | [UnityEditor.InitializeOnLoad] |
| 10 | #endif |
| 11 | public class OVRConfig : ScriptableObject |
| 12 | { |
| 13 | [SerializeField] |
| 14 | private string androidSDKPath = ""; |
| 15 | |
| 16 | [SerializeField] |
| 17 | private string gradlePath = ""; |
| 18 | |
| 19 | [SerializeField] |
| 20 | private string jdkPath = ""; |
| 21 | |
| 22 | private static OVRConfig instance; |
| 23 | |
| 24 | public static OVRConfig Instance |
| 25 | { |
| 26 | get |
| 27 | { |
| 28 | if (instance == null) |
| 29 | { |
| 30 | instance = Resources.Load<OVRConfig>("OVRConfig"); |
| 31 | if (instance == null) |
| 32 | { |
| 33 | instance = ScriptableObject.CreateInstance<OVRConfig>(); |
| 34 | string resourcePath = Path.Combine(UnityEngine.Application.dataPath, "Resources"); |
| 35 | if (!Directory.Exists(resourcePath)) |
| 36 | { |
| 37 | UnityEditor.AssetDatabase.CreateFolder("Assets", "Resources"); |
| 38 | } |
| 39 | |
| 40 | string fullPath = Path.Combine(Path.Combine("Assets", "Resources"), "OVRBuildConfig.asset"); |
| 41 | UnityEditor.AssetDatabase.CreateAsset(instance, fullPath); |
| 42 | } |
| 43 | } |
| 44 | return instance; |
| 45 | } |
| 46 | set |
| 47 | { |
| 48 | instance = value; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Returns the path to the base directory of the Android SDK |
| 53 | public string GetAndroidSDKPath(bool throwError = true) |
| 54 | { |
| 55 | #if UNITY_2019_1_OR_NEWER |
| 56 | // Check for use of embedded path or user defined |
| 57 | bool useEmbedded = EditorPrefs.GetBool("SdkUseEmbedded"); |
| 58 | if (useEmbedded) |
| 59 | { |
| 60 | androidSDKPath = Path.Combine(BuildPipeline.GetPlaybackEngineDirectory(BuildTarget.Android, BuildOptions.None), "SDK"); |
| 61 | } |
| 62 | else |
| 63 | #endif |
| 64 | { |
| 65 | androidSDKPath = EditorPrefs.GetString("AndroidSdkRoot"); |
| 66 | } |
| 67 | |
| 68 | androidSDKPath = androidSDKPath.Replace("/", "\\"); |
| 69 | // Validate sdk path and notify user if path does not exist. |
| 70 | if (!Directory.Exists(androidSDKPath)) |
| 71 | { |
| 72 | androidSDKPath = Environment.GetEnvironmentVariable("ANDROID_SDK_ROOT"); |
| 73 | if (!string.IsNullOrEmpty(androidSDKPath)) |
| 74 | { |
| 75 | return androidSDKPath; |
| 76 | } |
| 77 | |
| 78 | if (throwError) |
| 79 | { |
| 80 | EditorUtility.DisplayDialog("Android SDK not Found", |
| 81 | "Android SDK not found. Please ensure that the path is set correctly in (Edit -> Preferences -> External Tools) or that the Untiy Android module is installed correctly.", |
| 82 | "Ok"); |
| 83 | } |
| 84 | return string.Empty; |
| 85 | } |
| 86 | |
| 87 | EditorUtility.SetDirty(Instance); |
| 88 | return androidSDKPath; |
| 89 | } |
| 90 | |
| 91 | // Returns the path to the gradle-launcher-*.jar |
| 92 | public string GetGradlePath(bool throwError = true) |
| 93 | { |
| 94 | string libPath = ""; |
| 95 | #if UNITY_2019_1_OR_NEWER |
| 96 | // Check for use of embedded path or user defined |
| 97 | bool useEmbedded = EditorPrefs.GetBool("GradleUseEmbedded"); |
| 98 | |
| 99 | if (useEmbedded) |
| 100 | { |
| 101 | libPath = Path.Combine(BuildPipeline.GetPlaybackEngineDirectory(BuildTarget.Android, BuildOptions.None), "Tools\\gradle\\lib"); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | libPath = Path.Combine(EditorPrefs.GetString("GradlePath"), "lib"); |
| 106 | } |
| 107 | #else |
| 108 | libPath = Path.Combine(EditorApplication.applicationContentsPath, "PlaybackEngines\\AndroidPlayer\\Tools\\gradle\\lib"); |
| 109 | #endif |
| 110 | |
| 111 | libPath = libPath.Replace("/", "\\"); |
| 112 | if (!string.IsNullOrEmpty(libPath) && Directory.Exists(libPath)) |
| 113 | { |
| 114 | string[] gradleJar = Directory.GetFiles(libPath, "gradle-launcher-*.jar"); |
| 115 | if (gradleJar.Length == 1) |
| 116 | { |
| 117 | gradlePath = gradleJar[0]; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // Validate gradle path and notify user if path does not exist. |
| 122 | if (!File.Exists(gradlePath)) |
| 123 | { |
| 124 | if (throwError) |
| 125 | { |
| 126 | EditorUtility.DisplayDialog("Gradle not Found", |
| 127 | "Gradle not found. Please ensure that the path is set correctly in (Edit -> Preferences -> External Tools) or that the Untiy Android module is installed correctly.", |
| 128 | "Ok"); |
| 129 | } |
| 130 | return string.Empty; |
| 131 | } |
| 132 | |
| 133 | EditorUtility.SetDirty(Instance); |
| 134 | return gradlePath; |
| 135 | } |
| 136 | |
| 137 | // Returns path to the Java executable in the JDK |
| 138 | public string GetJDKPath(bool throwError = true) |
| 139 | { |
| 140 | #if UNITY_EDITOR_WIN |
| 141 | // Check for use of embedded path or user defined |
| 142 | bool useEmbedded = EditorPrefs.GetBool("JdkUseEmbedded"); |
| 143 | |
| 144 | string exePath = ""; |
| 145 | if (useEmbedded) |
| 146 | { |
| 147 | #if UNITY_2019_1_OR_NEWER |
| 148 | exePath = Path.Combine(BuildPipeline.GetPlaybackEngineDirectory(BuildTarget.Android, BuildOptions.None), "Tools\\OpenJDK\\Windows\\bin"); |
| 149 | #else |
| 150 | exePath = Path.Combine(EditorApplication.applicationContentsPath, "PlaybackEngines\\AndroidPlayer\\Tools\\OpenJDK\\Windows\\bin"); |
| 151 | #endif |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | exePath = Path.Combine(EditorPrefs.GetString("JdkPath"), "bin"); |
| 156 | } |
| 157 | |
| 158 | jdkPath = Path.Combine(exePath, "java.exe"); |
| 159 | jdkPath = jdkPath.Replace("/", "\\"); |
| 160 | |
| 161 | // Validate gradle path and notify user if path does not exist. |
| 162 | if (!File.Exists(jdkPath)) |
| 163 | { |
| 164 | // Check the enviornment variable as a backup to see if the JDK is there. |
| 165 | string javaHome = Environment.GetEnvironmentVariable("JAVA_HOME"); |
| 166 | if(!string.IsNullOrEmpty(javaHome)) |
| 167 | { |
| 168 | jdkPath = Path.Combine(javaHome, "bin\\java.exe"); |
| 169 | if(File.Exists(jdkPath)) |
| 170 | { |
| 171 | EditorUtility.SetDirty(Instance); |
| 172 | return jdkPath; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if (throwError) |
| 177 | { |
| 178 | EditorUtility.DisplayDialog("JDK not Found", |
| 179 | "JDK not found. Please ensure that the path is set correctly in (Edit -> Preferences -> External Tools) or that the Untiy Android module is installed correctly.", |
| 180 | "Ok"); |
| 181 | } |
| 182 | return string.Empty; |
| 183 | } |
| 184 | #endif |
| 185 | EditorUtility.SetDirty(Instance); |
| 186 | return jdkPath; |
| 187 | } |
| 188 | } |