ARKitBuildProcessor.cs
8.9 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
#if UNITY_IOS
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.Callbacks;
using UnityEditor.iOS;
using UnityEditor.iOS.Xcode;
using UnityEditor.Rendering;
using UnityEditor.XR.ARSubsystems;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.XR.ARKit;
using OSVersion = UnityEngine.XR.ARKit.OSVersion;
namespace UnityEditor.XR.ARKit
{
internal class ARKitBuildProcessor
{
public static IEnumerable<T> AssetsOfType<T>() where T : UnityEngine.Object
{
foreach(var guid in AssetDatabase.FindAssets("t:" + typeof(T).Name))
{
var path = AssetDatabase.GUIDToAssetPath(guid);
yield return AssetDatabase.LoadAssetAtPath<T>(path);
}
}
class PostProcessor : IPostprocessBuildWithReport
{
// NB: Needs to be > 0 to make sure we remove the shader since the
// Input System overwrites the preloaded assets array
public int callbackOrder => 1;
public void OnPostprocessBuild(BuildReport report)
{
if (report.summary.platform != BuildTarget.iOS)
{
return;
}
BuildHelper.RemoveShaderFromProject(ARKitCameraSubsystem.backgroundShaderName);
HandleARKitRequiredFlag(report.summary.outputPath);
}
static void HandleARKitRequiredFlag(string pathToBuiltProject)
{
var arkitSettings = ARKitSettings.GetOrCreateSettings();
string plistPath = Path.Combine(pathToBuiltProject, "Info.plist");
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict rootDict = plist.root;
// Get or create array to manage device capabilities
const string capsKey = "UIRequiredDeviceCapabilities";
PlistElementArray capsArray;
PlistElement pel;
if (rootDict.values.TryGetValue(capsKey, out pel))
{
capsArray = pel.AsArray();
}
else
{
capsArray = rootDict.CreateArray(capsKey);
}
// Remove any existing "arkit" plist entries
const string arkitStr = "arkit";
capsArray.values.RemoveAll(x => arkitStr.Equals(x.AsString()));
if (arkitSettings.requirement == ARKitSettings.Requirement.Required)
{
// Add "arkit" plist entry
capsArray.AddString(arkitStr);
}
File.WriteAllText(plistPath, plist.WriteToString());
}
}
class Preprocessor : IPreprocessBuildWithReport, IPreprocessShaders
{
// Magic value according to
// https://docs.unity3d.com/ScriptReference/PlayerSettings.GetArchitecture.html
// "0 - None, 1 - ARM64, 2 - Universal."
const int k_TargetArchitectureArm64 = 1;
const int k_TargetArchitectureUniversal = 2;
// The minimum target Xcode version for the plugin
const int k_TargetMinimumMajorXcodeVersion = 11;
const int k_TargetMinimumMinorXcodeVersion = 0;
const int k_TargetMinimumPatchXcodeVersion = 0;
public void OnProcessShader(Shader shader, ShaderSnippetData snippet, IList<ShaderCompilerData> data)
{
// Remove shader variants for the camera background shader that will fail compilation because of package dependencies.
string backgroundShaderName = ARKitCameraSubsystem.backgroundShaderName;
if (backgroundShaderName.Equals(shader.name))
{
foreach (var backgroundShaderKeywordToNotCompile in ARKitCameraSubsystem.backgroundShaderKeywordsToNotCompile)
{
ShaderKeyword shaderKeywordToNotCompile = new ShaderKeyword(shader, backgroundShaderKeywordToNotCompile);
for (int i = (data.Count - 1); i >= 0; --i)
{
if (data[i].shaderKeywordSet.IsEnabled(shaderKeywordToNotCompile))
{
data.RemoveAt(i);
}
}
}
}
}
public void OnPreprocessBuild(BuildReport report)
{
if (report.summary.platform != BuildTarget.iOS)
return;
if (string.IsNullOrEmpty(PlayerSettings.iOS.cameraUsageDescription))
throw new BuildFailedException("ARKit requires a Camera Usage Description (Player Settings > iOS > Other Settings > Camera Usage Description)");
EnsureMinimumXcodeVersion();
EnsureMetalIsFirstApi();
if(ARKitSettings.GetOrCreateSettings().requirement == ARKitSettings.Requirement.Required)
{
EnsureMinimumBuildTarget();
EnsureTargetArchitecturesAreSupported(report.summary.platformGroup);
}
else if (PlayerSettings.GetArchitecture(report.summary.platformGroup) == k_TargetArchitectureUniversal)
{
EnsureOpenGLIsUsed();
}
BuildHelper.AddBackgroundShaderToProject(ARKitCameraSubsystem.backgroundShaderName);
}
void EnsureMinimumBuildTarget()
{
var userSetTargetVersion = OSVersion.Parse(PlayerSettings.iOS.targetOSVersionString);
if (userSetTargetVersion < new OSVersion(11))
{
throw new BuildFailedException($"You have selected a minimum target iOS version of {userSetTargetVersion} and have the ARKit package installed."
+ "ARKit requires at least iOS version 11.0 (See Player Settings > Other Settings > Target minimum iOS Version).");
}
}
void EnsureMinimumXcodeVersion()
{
#if UNITY_EDITOR_OSX
var xcodeIndex = Math.Max(0, XcodeApplications.GetPreferedXcodeIndex());
var xcodeVersion = OSVersion.Parse(XcodeApplications.GetXcodeApplicationPublicName(xcodeIndex));
if (xcodeVersion == new OSVersion(0))
throw new BuildFailedException($"Could not determine which version of Xcode was selected in the Build Settings. Xcode app was computed as \"{XcodeApplications.GetXcodeApplicationPublicName(xcodeIndex)}\".");
if (xcodeVersion < new OSVersion(
k_TargetMinimumMajorXcodeVersion,
k_TargetMinimumMinorXcodeVersion,
k_TargetMinimumPatchXcodeVersion))
throw new BuildFailedException($"The selected Xcode version: {xcodeVersion} is below the minimum Xcode required Xcode version for the Unity ARKit Plugin. Please target at least Xcode version {k_TargetMinimumMajorXcodeVersion}.{k_TargetMinimumMinorXcodeVersion}.{k_TargetMinimumPatchXcodeVersion}.");
#endif
}
void EnsureTargetArchitecturesAreSupported(BuildTargetGroup buildTargetGroup)
{
if (PlayerSettings.GetArchitecture(buildTargetGroup) != k_TargetArchitectureArm64)
throw new BuildFailedException("ARKit XR Plugin only supports the ARM64 architecture. See Player Settings > Other Settings > Architecture.");
}
void EnsureMetalIsFirstApi()
{
var graphicsApis = PlayerSettings.GetGraphicsAPIs(BuildTarget.iOS);
if (graphicsApis.Length > 0)
{
var graphicsApi = graphicsApis[0];
if (graphicsApi != GraphicsDeviceType.Metal)
throw new BuildFailedException($"You currently have {graphicsApi} at the top of the list of Graphics APis. However, Metal needs to be first in the list. (See Player Settings > Other Settings > Graphics APIs)");
}
}
void EnsureOpenGLIsUsed()
{
var graphicsApis = PlayerSettings.GetGraphicsAPIs(BuildTarget.iOS);
if (graphicsApis.Length > 0)
{
if(!graphicsApis.Contains(GraphicsDeviceType.OpenGLES2))
throw new BuildFailedException("To build for 'Universal' architecture, OpenGLES2 is needed. (See Player Settings > Other Settings > Graphics APIs)");
}
}
public int callbackOrder { get { return 0; } }
}
}
}
#endif