XRGeneralBuildProcessor.cs
9.53 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.XR.Management;
[assembly: InternalsVisibleTo("Unity.XR.Management.EditorTests")]
namespace UnityEditor.XR.Management
{
/// <summary>
/// Small utility class for reading, updating and writing boot config.
/// </summary>
internal class BootConfig
{
static readonly string kXrBootSettingsKey = "xr-boot-settings";
Dictionary<string, string> bootConfigSettings;
BuildReport buildReport;
string bootConfigPath;
internal BootConfig(BuildReport report)
{
buildReport = report;
}
internal void ReadBootConfig()
{
bootConfigSettings = new Dictionary<string, string>();
string buildTargetName = BuildPipeline.GetBuildTargetName(buildReport.summary.platform);
string xrBootSettings = UnityEditor.EditorUserBuildSettings.GetPlatformSettings(buildTargetName, kXrBootSettingsKey);
if (!String.IsNullOrEmpty(xrBootSettings))
{
// boot settings string format
// <boot setting>:<value>[;<boot setting>:<value>]*
var bootSettings = xrBootSettings.Split(';');
foreach (var bootSetting in bootSettings)
{
var setting = bootSetting.Split(':');
if (setting.Length == 2 && !String.IsNullOrEmpty(setting[0]) && !String.IsNullOrEmpty(setting[1]))
{
bootConfigSettings.Add(setting[0], setting[1]);
}
}
}
}
internal void SetValueForKey(string key, string value, bool replace = false)
{
if (bootConfigSettings.ContainsKey(key))
{
bootConfigSettings[key] = value;
}
else
{
bootConfigSettings.Add(key, value);
}
}
internal void WriteBootConfig()
{
// boot settings string format
// <boot setting>:<value>[;<boot setting>:<value>]*
bool firstEntry = true;
var sb = new System.Text.StringBuilder();
foreach (var kvp in bootConfigSettings)
{
if (!firstEntry)
{
sb.Append(";");
}
sb.Append($"{kvp.Key}:{kvp.Value}");
firstEntry = false;
}
string buildTargetName = BuildPipeline.GetBuildTargetName(buildReport.summary.platform);
EditorUserBuildSettings.SetPlatformSettings(buildTargetName, kXrBootSettingsKey, sb.ToString());
}
}
class XRGeneralBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
static readonly string kPreInitLibraryKey = "xrsdk-pre-init-library";
class PreInitInfo
{
public PreInitInfo(IXRLoaderPreInit loader, BuildTarget buildTarget, BuildTargetGroup buildTargetGroup)
{
this.loader = loader;
this.buildTarget = buildTarget;
this.buildTargetGroup = buildTargetGroup;
}
public IXRLoaderPreInit loader;
public BuildTarget buildTarget;
public BuildTargetGroup buildTargetGroup;
}
public int callbackOrder
{
get { return 0; }
}
void CleanOldSettings()
{
BuildHelpers.CleanOldSettings<XRGeneralSettings>();
}
public void OnPreprocessBuild(BuildReport report)
{
// Always remember to cleanup preloaded assets after build to make sure we don't
// dirty later builds with assets that may not be needed or are out of date.
CleanOldSettings();
XRGeneralSettingsPerBuildTarget buildTargetSettings = null;
EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out buildTargetSettings);
if (buildTargetSettings == null)
return;
XRGeneralSettings settings = buildTargetSettings.SettingsForBuildTarget(report.summary.platformGroup);
if (settings == null)
return;
XRManagerSettings loaderManager = settings.AssignedSettings;
if (loaderManager != null)
{
// If there are no loaders present in the current manager instance, then the settings will not be included in the current build.
if (loaderManager.activeLoaders.Count == 0)
return;
// chances are that our devices won't fall back to graphics device types later in the list so it's better to assume the device will be created with the first gfx api in the list.
// furthermore, we have no way to influence falling back to other graphics API types unless we automatically change settings underneath the user which is no good!
GraphicsDeviceType[] deviceTypes = PlayerSettings.GetGraphicsAPIs(report.summary.platform);
if (deviceTypes.Length > 0)
{
VerifyGraphicsAPICompatibility(loaderManager, deviceTypes[0]);
}
else
{
Debug.LogWarning("No Graphics APIs have been configured in Player Settings.");
}
PreInitInfo preInitInfo = null;
var loaders = loaderManager.activeLoaders;
if (loaders.Count >= 1)
{
preInitInfo = new PreInitInfo(loaders[0] as IXRLoaderPreInit, report.summary.platform, report.summary.platformGroup);
}
var loader = preInitInfo?.loader ?? null;
if (loader != null)
{
BootConfig bootConfig = new BootConfig(report);
bootConfig.ReadBootConfig();
string preInitLibraryName = loader.GetPreInitLibraryName(preInitInfo.buildTarget, preInitInfo.buildTargetGroup);
bootConfig.SetValueForKey(kPreInitLibraryKey, preInitLibraryName);
bootConfig.WriteBootConfig();
}
}
UnityEngine.Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();
var settingsIncludedInPreloadedAssets = preloadedAssets.Contains(settings);
// If there are no loaders present in the current manager instance, then the settings will not be included in the current build.
if (!settingsIncludedInPreloadedAssets && loaderManager.activeLoaders.Count > 0)
{
var assets = preloadedAssets.ToList();
assets.Add(settings);
PlayerSettings.SetPreloadedAssets(assets.ToArray());
}
else
{
CleanOldSettings();
}
}
public static void VerifyGraphicsAPICompatibility(XRManagerSettings loaderManager, GraphicsDeviceType selectedDeviceType)
{
HashSet<GraphicsDeviceType> allLoaderGraphicsDeviceTypes = new HashSet<GraphicsDeviceType>();
foreach (var loader in loaderManager.activeLoaders)
{
List<GraphicsDeviceType> supporteDeviceTypes = loader.GetSupportedGraphicsDeviceTypes(true);
// To help with backward compatibility, if we find that any of the compatibility lists are empty we assume that at least one of the loaders does not implement the GetSupportedGraphicsDeviceTypes method
// Therefore we revert to the previous behavior of building the app regardless of gfx api settings.
if (supporteDeviceTypes.Count == 0)
{
allLoaderGraphicsDeviceTypes.Clear();
break;
}
foreach (var supportedGraphicsDeviceType in supporteDeviceTypes)
{
allLoaderGraphicsDeviceTypes.Add(supportedGraphicsDeviceType);
}
}
if (allLoaderGraphicsDeviceTypes.Count > 0 && !allLoaderGraphicsDeviceTypes.Contains(selectedDeviceType))
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendFormat(
"The selected grpahics API, {0}, is not supported by any of the current loaders. Please change the preferred Graphics API setting in Player Settings.\n",
selectedDeviceType);
foreach (var loader in loaderManager.activeLoaders)
{
stringBuilder.AppendLine(loader.name + " supports:");
foreach (var supportedGraphicsDeviceType in loader.GetSupportedGraphicsDeviceTypes(true))
{
stringBuilder.AppendLine("\t -" + supportedGraphicsDeviceType);
}
}
throw new BuildFailedException(stringBuilder.ToString());
}
}
public void OnPostprocessBuild(BuildReport report)
{
// Always remember to cleanup preloaded assets after build to make sure we don't
// dirty later builds with assets that may not be needed or are out of date.
CleanOldSettings();
}
}
}