SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
5.8 KiB

4 years ago
  1. #if UNITY_IOS
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEditor.Build;
  6. using UnityEditor.Build.Reporting;
  7. using UnityEditor.iOS.Xcode;
  8. using UnityEngine;
  9. using UnityEngine.XR.ARSubsystems;
  10. namespace UnityEditor.XR.ARKit
  11. {
  12. /// <summary>
  13. /// Looks at all XRReferenceImageLibraries in the project and generates
  14. /// an AR Resource Group for each library, then inserts them into a new
  15. /// Xcode asset catalog called "ARReferenceImages".
  16. /// </summary>
  17. static class ARKitReferenceImageLibraryBuildProcessor
  18. {
  19. static IEnumerable<ARResourceGroup> ResourceGroups()
  20. {
  21. // Create a resource group for each reference image library
  22. foreach (var library in ARKitBuildProcessor.AssetsOfType<XRReferenceImageLibrary>())
  23. {
  24. var resourceGroup = new ARResourceGroup(library.name + "_" + library.guid.ToUUIDString());
  25. // Create a resource group for each library
  26. foreach (var referenceImage in library)
  27. {
  28. try
  29. {
  30. resourceGroup.AddResource(new ARReferenceImage(referenceImage));
  31. }
  32. catch (ARReferenceImage.InvalidWidthException)
  33. {
  34. throw new BuildFailedException(string.Format("ARKit requires dimensions for all images. Reference image at index {0} named '{1}' in library '{2}' requires a non-zero width.",
  35. library.indexOf(referenceImage), referenceImage.name, AssetDatabase.GetAssetPath(library)));
  36. }
  37. catch (ARReferenceImage.MissingTextureException)
  38. {
  39. throw new BuildFailedException(string.Format("Reference image at index {0} named '{1}' in library '{2}' is missing a texture.",
  40. library.indexOf(referenceImage), referenceImage.name, AssetDatabase.GetAssetPath(library)));
  41. }
  42. catch (ARReferenceImage.BadTexturePathException)
  43. {
  44. throw new BuildFailedException(string.Format("Could not resolve texture path for reference image at index {0} named '{1}' in library '{2}'.",
  45. library.indexOf(referenceImage), referenceImage.name, AssetDatabase.GetAssetPath(library)));
  46. }
  47. catch (ARReferenceImage.LoadTextureException e)
  48. {
  49. throw new BuildFailedException(string.Format("Could not load texture at path {0} for reference image at index {1} named '{2}' in library '{3}'.",
  50. e.path, library.indexOf(referenceImage), referenceImage.name, AssetDatabase.GetAssetPath(library)));
  51. }
  52. catch (ARReferenceImage.TextureNotExportableException)
  53. {
  54. throw new BuildFailedException(string.Format(
  55. "Reference image at index {0} named '{1}' in library '{2}' could not be exported. " +
  56. "ARKit can directly use a texture's source asset if it is a JPG or PNG. " +
  57. "For all other formats, the texture must be exported to PNG, which requires the texture to be readable and uncompressed. " +
  58. "Change the Texture Import Settings or use a JPG or PNG.",
  59. library.indexOf(referenceImage), referenceImage.name, AssetDatabase.GetAssetPath(library)));
  60. }
  61. catch
  62. {
  63. Debug.LogErrorFormat("Failed to generate reference image at index {0} named '{1}' in library '{2}'.",
  64. library.indexOf(referenceImage), referenceImage.name, AssetDatabase.GetAssetPath(library));
  65. throw;
  66. }
  67. }
  68. yield return resourceGroup;
  69. }
  70. }
  71. // Fail the build if any of the reference images are invalid
  72. class Preprocessor : IPreprocessBuildWithReport
  73. {
  74. public int callbackOrder { get { return 0; } }
  75. public void OnPreprocessBuild(BuildReport report)
  76. {
  77. if (report.summary.platform != BuildTarget.iOS)
  78. return;
  79. foreach (var resourceGroup in ResourceGroups())
  80. {
  81. // Generating a resource group will throw exceptions
  82. // if any of the reference images are invalid.
  83. }
  84. }
  85. }
  86. class Postprocessor : IPostprocessBuildWithReport
  87. {
  88. public int callbackOrder { get { return 0; } }
  89. public void OnPostprocessBuild(BuildReport report)
  90. {
  91. if (report.summary.platform != BuildTarget.iOS)
  92. return;
  93. var buildOutputPath = report.summary.outputPath;
  94. // Read in the PBXProject
  95. var project = new PBXProject();
  96. var pbxProjectPath = PBXProject.GetPBXProjectPath(buildOutputPath);
  97. project.ReadFromString(File.ReadAllText(pbxProjectPath));
  98. // Create a new asset catalog
  99. var assetCatalog = new XcodeAssetCatalog("ARReferenceImages");
  100. // Generate resource groups and add each one to the asset catalog
  101. foreach (var resourceGroup in ResourceGroups())
  102. {
  103. assetCatalog.AddResourceGroup(resourceGroup);
  104. }
  105. // Create the asset catalog on disk
  106. assetCatalog.WriteAndAddToPBXProject(project, buildOutputPath);
  107. // Write out the updated Xcode project file
  108. File.WriteAllText(pbxProjectPath, project.WriteToString());
  109. }
  110. }
  111. }
  112. }
  113. #endif