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