-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathUnityARBuildPostprocessor.cs
380 lines (324 loc) · 15.2 KB
/
UnityARBuildPostprocessor.cs
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif
using UnityEngine.XR.iOS;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System;
public class UnityARBuildPostprocessor
{
static List<ARReferenceImagesSet> imageSets = new List<ARReferenceImagesSet>();
static List<ARReferenceObjectsSetAsset> objectSets = new List<ARReferenceObjectsSetAsset>();
// Build postprocessor. Currently only needed on:
// - iOS: no dynamic libraries, so plugin source files have to be copied into Xcode project
[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
if (target == BuildTarget.iOS)
OnPostprocessBuildIOS(pathToBuiltProject);
}
[PostProcessScene]
public static void OnPostProcessScene()
{
if (!BuildPipeline.isBuildingPlayer)
return;
foreach (ARReferenceImagesSet ar in UnityEngine.Resources.FindObjectsOfTypeAll<ARReferenceImagesSet>())
{
if (!imageSets.Contains(ar))
{
imageSets.Add(ar);
}
}
foreach (ARReferenceObjectsSetAsset ar in UnityEngine.Resources.FindObjectsOfTypeAll<ARReferenceObjectsSetAsset>())
{
if (!objectSets.Contains(ar))
{
objectSets.Add(ar);
}
}
}
private static UnityARKitPluginSettings LoadSettings()
{
UnityARKitPluginSettings loadedSettings = Resources.Load<UnityARKitPluginSettings>("UnityARKitPlugin/ARKitSettings");
if (loadedSettings == null)
{
loadedSettings = ScriptableObject.CreateInstance<UnityARKitPluginSettings>();
}
return loadedSettings;
}
// Replaces the first C++ macro with the given name in the source file. Only changes
// single-line macro declarations, if multi-line macro declaration is detected, the
// function returns without changing it. Macro name must be a valid C++ identifier.
internal static bool ReplaceCppMacro(string[] lines, string name, string newValue)
{
bool replaced = false;
Regex matchRegex = new Regex(@"^.*#\s*define\s+" + name);
Regex replaceRegex = new Regex(@"^.*#\s*define\s+" + name + @"(:?|\s|\s.*[^\\])$");
for (int i = 0; i < lines.Count(); i++)
{
if (matchRegex.Match(lines[i]).Success)
{
lines[i] = replaceRegex.Replace(lines[i], "#define " + name + " " + newValue);
replaced = true;
}
}
return replaced;
}
internal static void AddOrReplaceCppMacro(ref string[] lines, string name, string newValue)
{
if (ReplaceCppMacro(lines, name, newValue) == false)
{
Array.Resize(ref lines, lines.Length + 1);
lines[lines.Length - 1] = "#define " + name + " " + newValue;
}
}
static void UpdateDefinesInFile(string file, Dictionary<string, bool> valuesToUpdate)
{
string[] src = File.ReadAllLines(file);
var copy = (string[])src.Clone();
foreach (var kvp in valuesToUpdate)
AddOrReplaceCppMacro(ref copy, kvp.Key, kvp.Value ? "1" : "0");
if (!copy.SequenceEqual(src))
File.WriteAllLines(file, copy);
}
#if UNITY_IOS
static void AddReferenceImageToResourceGroup(ARReferenceImage arri, string parentFolderFullPath, string projectRelativePath, PBXProject project)
{
ARResourceContents resourceContents = new ARResourceContents ();
resourceContents.info = new ARResourceInfo ();
resourceContents.info.author = "unity";
resourceContents.info.version = 1;
resourceContents.images = new ARResourceFilename[1];
resourceContents.images [0] = new ARResourceFilename ();
resourceContents.images [0].idiom = "universal";
resourceContents.properties = new ARResourceProperties ();
resourceContents.properties.width = arri.physicalSize;
//add folder for reference image
string folderToCreate = arri.imageName + ".arreferenceimage";
string folderFullPath = Path.Combine (parentFolderFullPath, folderToCreate);
string projectRelativeFolder = Path.Combine (projectRelativePath, folderToCreate);
Directory.CreateDirectory (folderFullPath);
project.AddFolderReference (folderFullPath, projectRelativeFolder);
//copy file from texture asset
string imagePath = AssetDatabase.GetAssetPath(arri.imageTexture);
string imageFilename = Path.GetFileName (imagePath);
var dstPath = Path.Combine(folderFullPath, imageFilename);
File.Copy(imagePath, dstPath, true);
project.AddFile (dstPath, Path.Combine (projectRelativeFolder, imageFilename));
resourceContents.images [0].filename = imageFilename;
//add contents.json file
string contentsJsonPath = Path.Combine(folderFullPath, "Contents.json");
File.WriteAllText (contentsJsonPath, JsonUtility.ToJson (resourceContents, true));
project.AddFile (contentsJsonPath, Path.Combine (projectRelativeFolder, "Contents.json"));
}
static void AddReferenceImagesSetToAssetCatalog(ARReferenceImagesSet aris, string pathToBuiltProject, PBXProject project)
{
List<ARReferenceImage> processedImages = new List<ARReferenceImage> ();
ARResourceGroupContents groupContents = new ARResourceGroupContents();
groupContents.info = new ARResourceGroupInfo ();
groupContents.info.author = "unity";
groupContents.info.version = 1;
string folderToCreate = "Unity-iPhone/Images.xcassets/" + aris.resourceGroupName + ".arresourcegroup";
string folderFullPath = Path.Combine (pathToBuiltProject, folderToCreate);
Directory.CreateDirectory (folderFullPath);
project.AddFolderReference (folderFullPath, folderToCreate);
foreach (ARReferenceImage arri in aris.referenceImages) {
if (!processedImages.Contains (arri)) {
processedImages.Add (arri); //get rid of dupes
AddReferenceImageToResourceGroup(arri, folderFullPath, folderToCreate, project);
}
}
groupContents.resources = new ARResourceGroupResource[processedImages.Count];
int index = 0;
foreach (ARReferenceImage arri in processedImages) {
groupContents.resources [index] = new ARResourceGroupResource ();
groupContents.resources [index].filename = arri.imageName + ".arreferenceimage";
index++;
}
string contentsJsonPath = Path.Combine(folderFullPath, "Contents.json");
File.WriteAllText (contentsJsonPath, JsonUtility.ToJson (groupContents, true));
project.AddFile (contentsJsonPath, Path.Combine (folderToCreate, "Contents.json"));
}
static void AddReferenceObjectAssetToStreamingAssets(ARReferenceObjectAsset arro, string parentFolderFullPath, string projectRelativePath)
{
ARReferenceObjectResourceContents resourceContents = new ARReferenceObjectResourceContents ();
resourceContents.info = new ARResourceInfo ();
resourceContents.info.author = "unity";
resourceContents.info.version = 1;
resourceContents.objects = new ARResourceFilename[1];
resourceContents.objects [0] = new ARResourceFilename ();
resourceContents.objects [0].idiom = "universal";
resourceContents.referenceObjectName = arro.objectName;
//add folder for reference image
string folderToCreate = arro.objectName + ".arreferenceobject";
string folderFullPath = Path.Combine (parentFolderFullPath, folderToCreate);
string projectRelativeFolder = Path.Combine (projectRelativePath, folderToCreate);
Directory.CreateDirectory (folderFullPath);
//copy file from refobject asset
string objectPath = AssetDatabase.GetAssetPath(arro.referenceObject);
string objectFilename = Path.GetFileName (objectPath);
var dstPath = Path.Combine(folderFullPath, objectFilename);
File.Copy(objectPath, dstPath, true);
resourceContents.objects [0].filename = objectFilename;
//add contents.json file
string contentsJsonPath = Path.Combine(folderFullPath, "Contents.json");
File.WriteAllText (contentsJsonPath, JsonUtility.ToJson (resourceContents, true));
}
static void AddReferenceObjectsSetAssetToStreamingAssets(ARReferenceObjectsSetAsset aros, string pathToBuiltProject)
{
List<ARReferenceObjectAsset> processedObjects = new List<ARReferenceObjectAsset> ();
ARResourceGroupContents groupContents = new ARResourceGroupContents();
groupContents.info = new ARResourceGroupInfo ();
groupContents.info.author = "xcode";
groupContents.info.version = 1;
//On iOS, StreamingAssets end up at /Data/Raw
string folderToCreate = "Data/Raw/ARReferenceObjects/" + aros.resourceGroupName + ".arresourcegroup";
string folderFullPath = Path.Combine (pathToBuiltProject, folderToCreate);
Directory.CreateDirectory (folderFullPath);
foreach (ARReferenceObjectAsset arro in aros.referenceObjectAssets) {
if (!processedObjects.Contains (arro)) {
processedObjects.Add (arro); //get rid of dupes
AddReferenceObjectAssetToStreamingAssets(arro, folderFullPath, folderToCreate);
}
}
groupContents.resources = new ARResourceGroupResource[processedObjects.Count];
int index = 0;
foreach (ARReferenceObjectAsset arro in processedObjects) {
groupContents.resources [index] = new ARResourceGroupResource ();
groupContents.resources [index].filename = arro.objectName + ".arreferenceobject";
index++;
}
string contentsJsonPath = Path.Combine(folderFullPath, "Contents.json");
File.WriteAllText (contentsJsonPath, JsonUtility.ToJson (groupContents, true));
}
#if ARREFERENCEOBJECT_XCODE_ASSET_CATALOG
static void AddReferenceObjectAssetToResourceGroup(ARReferenceObjectAsset arro, string parentFolderFullPath, string projectRelativePath, PBXProject project)
{
ARReferenceObjectResourceContents resourceContents = new ARReferenceObjectResourceContents ();
resourceContents.info = new ARResourceInfo ();
resourceContents.info.author = "unity";
resourceContents.info.version = 1;
resourceContents.objects = new ARResourceFilename[1];
resourceContents.objects [0] = new ARResourceFilename ();
resourceContents.objects [0].idiom = "universal";
//add folder for reference image
string folderToCreate = arro.objectName + ".arreferenceobject";
string folderFullPath = Path.Combine (parentFolderFullPath, folderToCreate);
string projectRelativeFolder = Path.Combine (projectRelativePath, folderToCreate);
Directory.CreateDirectory (folderFullPath);
project.AddFolderReference (folderFullPath, projectRelativeFolder);
//copy file from texture asset
string objectPath = AssetDatabase.GetAssetPath(arro.referenceObject);
string objectFilename = Path.GetFileName (objectPath);
var dstPath = Path.Combine(folderFullPath, objectFilename);
File.Copy(objectPath, dstPath, true);
project.AddFile (dstPath, Path.Combine (projectRelativeFolder, objectFilename));
resourceContents.objects [0].filename = objectFilename;
//add contents.json file
string contentsJsonPath = Path.Combine(folderFullPath, "Contents.json");
File.WriteAllText (contentsJsonPath, JsonUtility.ToJson (resourceContents, true));
project.AddFile (contentsJsonPath, Path.Combine (projectRelativeFolder, "Contents.json"));
}
static void AddReferenceObjectsSetAssetToAssetCatalog(ARReferenceObjectsSetAsset aros, string pathToBuiltProject, PBXProject project)
{
List<ARReferenceObjectAsset> processedObjects = new List<ARReferenceObjectAsset> ();
ARResourceGroupContents groupContents = new ARResourceGroupContents();
groupContents.info = new ARResourceGroupInfo ();
groupContents.info.author = "xcode";
groupContents.info.version = 1;
string folderToCreate = "Unity-iPhone/Images.xcassets/" + aros.resourceGroupName + ".arresourcegroup";
string folderFullPath = Path.Combine (pathToBuiltProject, folderToCreate);
Directory.CreateDirectory (folderFullPath);
project.AddFolderReference (folderFullPath, folderToCreate);
foreach (ARReferenceObjectAsset arro in aros.referenceObjectAssets) {
if (!processedObjects.Contains (arro)) {
processedObjects.Add (arro); //get rid of dupes
AddReferenceObjectAssetToResourceGroup(arro, folderFullPath, folderToCreate, project);
}
}
groupContents.resources = new ARResourceGroupResource[processedObjects.Count];
int index = 0;
foreach (ARReferenceObjectAsset arro in processedObjects) {
groupContents.resources [index] = new ARResourceGroupResource ();
groupContents.resources [index].filename = arro.objectName + ".arreferenceobject";
index++;
}
string contentsJsonPath = Path.Combine(folderFullPath, "Contents.json");
File.WriteAllText (contentsJsonPath, JsonUtility.ToJson (groupContents, true));
project.AddFile (contentsJsonPath, Path.Combine (folderToCreate, "Contents.json"));
}
#endif //ARREFERENCEOBJECT_XCODE_ASSET_CATALOG
#endif //UNITY_IOS
private static void OnPostprocessBuildIOS(string pathToBuiltProject)
{
// We use UnityEditor.iOS.Xcode API which only exists in iOS editor module
#if UNITY_IOS
string projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
UnityEditor.iOS.Xcode.PBXProject proj = new UnityEditor.iOS.Xcode.PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
proj.AddFrameworkToProject(proj.TargetGuidByName("Unity-iPhone"), "ARKit.framework", false);
string target = proj.TargetGuidByName("Unity-iPhone");
Directory.CreateDirectory(Path.Combine(pathToBuiltProject, "Libraries/Unity"));
// Check UnityARKitPluginSettings
UnityARKitPluginSettings ps = LoadSettings();
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 (ps.AppRequiresARKit) {
// Add "arkit" plist entry
capsArray.AddString(arkitStr);
}
const string shareString = "UIFileSharingEnabled";
rootDict.SetBoolean(shareString, true);
File.WriteAllText(plistPath, plist.WriteToString());
foreach(ARReferenceImagesSet ar in imageSets)
{
AddReferenceImagesSetToAssetCatalog(ar, pathToBuiltProject, proj);
}
foreach(ARReferenceObjectsSetAsset objSet in objectSets)
{
AddReferenceObjectsSetAssetToStreamingAssets(objSet, pathToBuiltProject);
}
//TODO: remove this when XCode actool is able to handles ARResources despite deployment target
if (imageSets.Count > 0)
{
proj.SetBuildProperty(target, "IPHONEOS_DEPLOYMENT_TARGET", "11.3");
}
// Add or replace define for facetracking
UpdateDefinesInFile(pathToBuiltProject + "/Classes/Preprocessor.h", new Dictionary<string, bool>() {
{ "ARKIT_USES_FACETRACKING", ps.m_ARKitUsesFacetracking }
});
string[] filesToCopy = new string[]
{
};
for(int i = 0 ; i < filesToCopy.Length ; ++i)
{
var srcPath = Path.Combine("../PluginSource/source", filesToCopy[i]);
var dstLocalPath = "Libraries/" + filesToCopy[i];
var dstPath = Path.Combine(pathToBuiltProject, dstLocalPath);
File.Copy(srcPath, dstPath, true);
proj.AddFileToBuild(target, proj.AddFile(dstLocalPath, dstLocalPath));
}
File.WriteAllText(projPath, proj.WriteToString());
#endif // #if UNITY_IOS
}
}