OVREngineConfigurationUpdater.cs
10.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/************************************************************************************
Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
Licensed under the Oculus SDK License Version 3.4.1 (the "License");
you may not use the Oculus SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
https://developer.oculus.com/licenses/sdk-3.4.1
Unless required by applicable law or agreed to in writing, the Oculus SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
************************************************************************************/
#if USING_XR_MANAGEMENT && USING_XR_SDK_OCULUS
#define USING_XR_SDK
#endif
#if UNITY_2020_1_OR_NEWER
#define REQUIRES_XR_SDK
#endif
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System;
using System.IO;
[InitializeOnLoad]
class OVREngineConfigurationUpdater
{
private const string prefName = "OVREngineConfigurationUpdater_Enabled";
private const string menuItemName = "Oculus/Tools/Use Required Project Settings";
private const string androidAssetsPath = "Assets/Plugins/Android/assets";
private const string androidManifestPath = "Assets/Plugins/Android/AndroidManifest.xml";
static bool setPrefsForUtilities;
[MenuItem(menuItemName)]
static void ToggleUtilities()
{
setPrefsForUtilities = !setPrefsForUtilities;
Menu.SetChecked(menuItemName, setPrefsForUtilities);
int newValue = (setPrefsForUtilities) ? 1 : 0;
PlayerPrefs.SetInt(prefName, newValue);
PlayerPrefs.Save();
Debug.Log("Using required project settings: " + setPrefsForUtilities);
}
private static readonly string dashSupportEnableConfirmedKey = "Oculus_Utilities_OVREngineConfiguration_DashSupportEnableConfirmed_" + Application.unityVersion + OVRManager.utilitiesVersion;
private static bool dashSupportEnableConfirmed
{
get
{
return PlayerPrefs.GetInt(dashSupportEnableConfirmedKey, 0) == 1;
}
set
{
PlayerPrefs.SetInt(dashSupportEnableConfirmedKey, value ? 1 : 0);
}
}
static OVREngineConfigurationUpdater()
{
EditorApplication.delayCall += OnDelayCall;
EditorApplication.update += OnUpdate;
}
static void OnDelayCall()
{
setPrefsForUtilities = PlayerPrefs.GetInt(prefName, 1) != 0;
Menu.SetChecked(menuItemName, setPrefsForUtilities);
if (!setPrefsForUtilities)
return;
OVRPlugin.AddCustomMetadata("build_target", EditorUserBuildSettings.activeBuildTarget.ToString());
EnforceAndroidSettings();
EnforceInputManagerBindings();
}
static void OnUpdate()
{
if (!setPrefsForUtilities)
return;
EnforceBundleId();
EnforceVRSupport();
EnforceInstallLocation();
}
static void EnforceAndroidSettings()
{
if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android)
return;
if (PlayerSettings.defaultInterfaceOrientation != UIOrientation.LandscapeLeft)
{
Debug.Log("OVREngineConfigurationUpdater: Setting orientation to Landscape Left");
// Default screen orientation must be set to landscape left.
PlayerSettings.defaultInterfaceOrientation = UIOrientation.LandscapeLeft;
}
#if !USING_XR_SDK && !REQUIRES_XR_SDK
#pragma warning disable 618
if (!PlayerSettings.virtualRealitySupported)
#pragma warning restore 618
{
// NOTE: This value should not affect the main window surface
// when Built-in VR support is enabled.
// NOTE: On Adreno Lollipop, it is an error to have antiAliasing set on the
// main window surface with front buffer rendering enabled. The view will
// render black.
// On Adreno KitKat, some tiling control modes will cause the view to render
// black.
if (QualitySettings.antiAliasing != 0 && QualitySettings.antiAliasing != 1)
{
Debug.Log("OVREngineConfigurationUpdater: Disabling antiAliasing");
QualitySettings.antiAliasing = 1;
}
}
#endif
if (QualitySettings.vSyncCount != 0)
{
Debug.Log("OVREngineConfigurationUpdater: Setting vsyncCount to 0");
// We sync in the TimeWarp, so we don't want unity syncing elsewhere.
QualitySettings.vSyncCount = 0;
}
}
static void EnforceVRSupport()
{
#if !USING_XR_SDK && !REQUIRES_XR_SDK
#pragma warning disable 618
if (PlayerSettings.virtualRealitySupported)
#pragma warning restore 618
return;
var mgrs = GameObject.FindObjectsOfType<OVRManager>();
for (int i = 0; i < mgrs.Length; ++i)
{
if (mgrs [i].isActiveAndEnabled)
{
Debug.Log ("Enabling Unity VR support");
#pragma warning disable 618
PlayerSettings.virtualRealitySupported = true;
#pragma warning restore 618
bool oculusFound = false;
foreach (var device in UnityEngine.XR.XRSettings.supportedDevices)
oculusFound |= (device == "Oculus");
if (!oculusFound)
Debug.LogError("Please add Oculus to the list of supported devices to use the Utilities.");
return;
}
}
#endif
}
private static void EnforceBundleId()
{
#if !USING_XR_SDK && !REQUIRES_XR_SDK
#pragma warning disable 618
if (!PlayerSettings.virtualRealitySupported)
{
return;
}
#pragma warning restore 618
#endif
#if USING_XR_SDK || !REQUIRES_XR_SDK
if (PlayerSettings.applicationIdentifier == "" || PlayerSettings.applicationIdentifier == "com.Company.ProductName")
{
string defaultBundleId = "com.oculus.UnitySample";
Debug.LogWarning("\"" + PlayerSettings.applicationIdentifier + "\" is not a valid bundle identifier. Defaulting to \"" + defaultBundleId + "\".");
PlayerSettings.applicationIdentifier = defaultBundleId;
}
#endif
}
private static void EnforceInstallLocation()
{
if (PlayerSettings.Android.preferredInstallLocation != AndroidPreferredInstallLocation.Auto)
PlayerSettings.Android.preferredInstallLocation = AndroidPreferredInstallLocation.Auto;
}
private static void EnforceInputManagerBindings()
{
try
{
BindAxis(new Axis() { name = "Oculus_CrossPlatform_Button2", positiveButton = "joystick button 0", gravity = 1000f, sensitivity = 1000f, type = 0 });
BindAxis(new Axis() { name = "Oculus_CrossPlatform_Button4", positiveButton = "joystick button 2", gravity = 1000f, sensitivity = 1000f, type = 0 });
BindAxis(new Axis() { name = "Oculus_CrossPlatform_PrimaryThumbstick", positiveButton = "joystick button 8", gravity = 0f, dead = 0f, sensitivity = 0.1f, type = 0 });
BindAxis(new Axis() { name = "Oculus_CrossPlatform_SecondaryThumbstick", positiveButton = "joystick button 9", gravity = 0f, dead = 0f, sensitivity = 0.1f, type = 0 });
BindAxis(new Axis() { name = "Oculus_CrossPlatform_PrimaryIndexTrigger", dead = 0.19f, type = 2, axis = 8, joyNum = 0 });
BindAxis(new Axis() { name = "Oculus_CrossPlatform_SecondaryIndexTrigger", dead = 0.19f, type = 2, axis = 9, joyNum = 0 });
BindAxis(new Axis() { name = "Oculus_CrossPlatform_PrimaryHandTrigger", dead = 0.19f, type = 2, axis = 10, joyNum = 0 });
BindAxis(new Axis() { name = "Oculus_CrossPlatform_SecondaryHandTrigger", dead = 0.19f, type = 2, axis = 11, joyNum = 0 });
BindAxis(new Axis() { name = "Oculus_CrossPlatform_PrimaryThumbstickHorizontal", dead = 0.19f, type = 2, axis = 0, joyNum = 0 });
BindAxis(new Axis() { name = "Oculus_CrossPlatform_PrimaryThumbstickVertical", dead = 0.19f, type = 2, axis = 1, joyNum = 0, invert = true });
BindAxis(new Axis() { name = "Oculus_CrossPlatform_SecondaryThumbstickHorizontal", dead = 0.19f, type = 2, axis = 3, joyNum = 0 });
BindAxis(new Axis() { name = "Oculus_CrossPlatform_SecondaryThumbstickVertical", dead = 0.19f, type = 2, axis = 4, joyNum = 0, invert = true });
}
catch
{
Debug.LogError("Failed to apply Oculus input manager bindings.");
}
}
private class Axis
{
public string name = String.Empty;
public string descriptiveName = String.Empty;
public string descriptiveNegativeName = String.Empty;
public string negativeButton = String.Empty;
public string positiveButton = String.Empty;
public string altNegativeButton = String.Empty;
public string altPositiveButton = String.Empty;
public float gravity = 0.0f;
public float dead = 0.001f;
public float sensitivity = 1.0f;
public bool snap = false;
public bool invert = false;
public int type = 2;
public int axis = 0;
public int joyNum = 0;
}
private static void BindAxis(Axis axis)
{
SerializedObject serializedObject = new SerializedObject(AssetDatabase.LoadAllAssetsAtPath("ProjectSettings/InputManager.asset")[0]);
SerializedProperty axesProperty = serializedObject.FindProperty("m_Axes");
SerializedProperty axisIter = axesProperty.Copy();
axisIter.Next(true);
axisIter.Next(true);
while (axisIter.Next(false))
{
if (axisIter.FindPropertyRelative("m_Name").stringValue == axis.name)
{
// Axis already exists. Don't create binding.
return;
}
}
axesProperty.arraySize++;
serializedObject.ApplyModifiedProperties();
SerializedProperty axisProperty = axesProperty.GetArrayElementAtIndex(axesProperty.arraySize - 1);
axisProperty.FindPropertyRelative("m_Name").stringValue = axis.name;
axisProperty.FindPropertyRelative("descriptiveName").stringValue = axis.descriptiveName;
axisProperty.FindPropertyRelative("descriptiveNegativeName").stringValue = axis.descriptiveNegativeName;
axisProperty.FindPropertyRelative("negativeButton").stringValue = axis.negativeButton;
axisProperty.FindPropertyRelative("positiveButton").stringValue = axis.positiveButton;
axisProperty.FindPropertyRelative("altNegativeButton").stringValue = axis.altNegativeButton;
axisProperty.FindPropertyRelative("altPositiveButton").stringValue = axis.altPositiveButton;
axisProperty.FindPropertyRelative("gravity").floatValue = axis.gravity;
axisProperty.FindPropertyRelative("dead").floatValue = axis.dead;
axisProperty.FindPropertyRelative("sensitivity").floatValue = axis.sensitivity;
axisProperty.FindPropertyRelative("snap").boolValue = axis.snap;
axisProperty.FindPropertyRelative("invert").boolValue = axis.invert;
axisProperty.FindPropertyRelative("type").intValue = axis.type;
axisProperty.FindPropertyRelative("axis").intValue = axis.axis;
axisProperty.FindPropertyRelative("joyNum").intValue = axis.joyNum;
serializedObject.ApplyModifiedProperties();
}
}