OVRConfig.cs 5.1 KB

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