XRGeneralBuildProcessor.cs
5.88 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using UnityEditor;
using UnityEditor.Android;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEngine.XR.Management;
namespace UnityEditor.XR.Management
{
class XRGeneralBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport, IPostGenerateGradleAndroidProject
{
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;
}
static private PreInitInfo preInitInfo = null;
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;
// store off some info about the first loader in the list for PreInit boot.config purposes
preInitInfo = null;
XRManagerSettings loaderManager = settings.AssignedSettings;
if (loaderManager != null)
{
List<XRLoader> loaders = loaderManager.loaders;
if (loaders.Count >= 1)
{
preInitInfo = new PreInitInfo(loaders[0] as IXRLoaderPreInit, report.summary.platform, report.summary.platformGroup);
}
}
UnityEngine.Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();
if (!preloadedAssets.Contains(settings))
{
var assets = preloadedAssets.ToList();
assets.Add(settings);
PlayerSettings.SetPreloadedAssets(assets.ToArray());
}
}
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();
if (preInitInfo == null)
return;
// Android build post-processing is handled in OnPostGenerateGradleAndroidProject
if (report.summary.platform != BuildTarget.Android)
{
foreach (BuildFile file in report.files)
{
if (file.role == CommonRoles.bootConfig)
{
try
{
var loader = preInitInfo.loader;
if (loader != null)
{
string preInitLibraryName = loader.GetPreInitLibraryName(preInitInfo.buildTarget,
preInitInfo.buildTargetGroup);
preInitInfo = null;
#if UNITY_2019_3_OR_NEWER
UnityEditor.XR.BootOptions.SetXRSDKPreInitLibrary(file.path,
preInitLibraryName);
#else
UnityEditor.Experimental.XR.BootOptions.SetXRSDKPreInitLibrary(file.path,
preInitLibraryName);
#endif
}
}
catch (Exception e)
{
throw new UnityEditor.Build.BuildFailedException(e);
}
break;
}
}
}
}
public void OnPostGenerateGradleAndroidProject(string path)
{
if (preInitInfo == null)
return;
// android builds move the files to a different location than is in the BuildReport, so we have to manually find the boot.config
string[] paths = { "src", "main", "assets", "bin", "Data", "boot.config" };
string fullPath = System.IO.Path.Combine(path, String.Join(Path.DirectorySeparatorChar.ToString(), paths));
try
{
var loader = preInitInfo.loader;
if (loader != null)
{
string preInitLibraryName = loader.GetPreInitLibraryName(preInitInfo.buildTarget,
preInitInfo.buildTargetGroup);
preInitInfo = null;
#if UNITY_2019_3_OR_NEWER
UnityEditor.XR.BootOptions.SetXRSDKPreInitLibrary(fullPath, preInitLibraryName);
#else
UnityEditor.Experimental.XR.BootOptions.SetXRSDKPreInitLibrary(fullPath, preInitLibraryName);
#endif
}
}
catch (Exception e)
{
throw new UnityEditor.Build.BuildFailedException(e);
}
}
}
}