XRGeneralBuildProcessor.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using UnityEditor.Android;
  8. using UnityEditor.Build;
  9. using UnityEditor.Build.Reporting;
  10. using UnityEngine;
  11. using UnityEngine.Rendering;
  12. using UnityEngine.XR.Management;
  13. [assembly: InternalsVisibleTo("Unity.XR.Management.EditorTests")]
  14. namespace UnityEditor.XR.Management
  15. {
  16. class XRGeneralBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport, IPostGenerateGradleAndroidProject
  17. {
  18. class PreInitInfo
  19. {
  20. public PreInitInfo(IXRLoaderPreInit loader, BuildTarget buildTarget, BuildTargetGroup buildTargetGroup)
  21. {
  22. this.loader = loader;
  23. this.buildTarget = buildTarget;
  24. this.buildTargetGroup = buildTargetGroup;
  25. }
  26. public IXRLoaderPreInit loader;
  27. public BuildTarget buildTarget;
  28. public BuildTargetGroup buildTargetGroup;
  29. }
  30. static private PreInitInfo preInitInfo = null;
  31. public int callbackOrder
  32. {
  33. get { return 0; }
  34. }
  35. void CleanOldSettings()
  36. {
  37. BuildHelpers.CleanOldSettings<XRGeneralSettings>();
  38. }
  39. public void OnPreprocessBuild(BuildReport report)
  40. {
  41. // Always remember to cleanup preloaded assets after build to make sure we don't
  42. // dirty later builds with assets that may not be needed or are out of date.
  43. CleanOldSettings();
  44. XRGeneralSettingsPerBuildTarget buildTargetSettings = null;
  45. EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out buildTargetSettings);
  46. if (buildTargetSettings == null)
  47. return;
  48. XRGeneralSettings settings = buildTargetSettings.SettingsForBuildTarget(report.summary.platformGroup);
  49. if (settings == null)
  50. return;
  51. // store off some info about the first loader in the list for PreInit boot.config purposes
  52. preInitInfo = null;
  53. XRManagerSettings loaderManager = settings.AssignedSettings;
  54. if (loaderManager != null)
  55. {
  56. List<XRLoader> loaders = loaderManager.loaders;
  57. if (loaders.Count >= 1)
  58. {
  59. preInitInfo = new PreInitInfo(loaders[0] as IXRLoaderPreInit, report.summary.platform, report.summary.platformGroup);
  60. }
  61. }
  62. if (loaderManager != null)
  63. {
  64. // 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.
  65. // 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!
  66. GraphicsDeviceType[] deviceTypes = PlayerSettings.GetGraphicsAPIs(report.summary.platform);
  67. if (deviceTypes.Length > 0)
  68. {
  69. VerifyGraphicsAPICompatibility(loaderManager, deviceTypes[0]);
  70. }
  71. else
  72. {
  73. Debug.LogWarning("No Graphics APIs have been configured in Player Settings.");
  74. }
  75. }
  76. UnityEngine.Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();
  77. if (!preloadedAssets.Contains(settings))
  78. {
  79. var assets = preloadedAssets.ToList();
  80. assets.Add(settings);
  81. PlayerSettings.SetPreloadedAssets(assets.ToArray());
  82. }
  83. }
  84. public static void VerifyGraphicsAPICompatibility(XRManagerSettings loaderManager, GraphicsDeviceType selectedDeviceType)
  85. {
  86. HashSet<GraphicsDeviceType> allLoaderGraphicsDeviceTypes = new HashSet<GraphicsDeviceType>();
  87. foreach (var loader in loaderManager.loaders)
  88. {
  89. List<GraphicsDeviceType> supporteDeviceTypes = loader.GetSupportedGraphicsDeviceTypes(true);
  90. // 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
  91. // Therefore we revert to the previous behavior of building the app regardless of gfx api settings.
  92. if (supporteDeviceTypes.Count == 0)
  93. {
  94. allLoaderGraphicsDeviceTypes.Clear();
  95. break;
  96. }
  97. foreach (var supportedGraphicsDeviceType in supporteDeviceTypes)
  98. {
  99. allLoaderGraphicsDeviceTypes.Add(supportedGraphicsDeviceType);
  100. }
  101. }
  102. if (allLoaderGraphicsDeviceTypes.Count > 0 && !allLoaderGraphicsDeviceTypes.Contains(selectedDeviceType))
  103. {
  104. StringBuilder stringBuilder = new StringBuilder();
  105. stringBuilder.AppendFormat(
  106. "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",
  107. selectedDeviceType);
  108. foreach (var loader in loaderManager.loaders)
  109. {
  110. stringBuilder.AppendLine(loader.name + " supports:");
  111. foreach (var supportedGraphicsDeviceType in loader.GetSupportedGraphicsDeviceTypes(true))
  112. {
  113. stringBuilder.AppendLine("\t -" + supportedGraphicsDeviceType);
  114. }
  115. }
  116. throw new BuildFailedException(stringBuilder.ToString());
  117. }
  118. }
  119. public void OnPostprocessBuild(BuildReport report)
  120. {
  121. // Always remember to cleanup preloaded assets after build to make sure we don't
  122. // dirty later builds with assets that may not be needed or are out of date.
  123. CleanOldSettings();
  124. if (preInitInfo == null)
  125. return;
  126. // Android build post-processing is handled in OnPostGenerateGradleAndroidProject
  127. if (report.summary.platform != BuildTarget.Android)
  128. {
  129. foreach (BuildFile file in report.files)
  130. {
  131. if (file.role == CommonRoles.bootConfig)
  132. {
  133. try
  134. {
  135. var loader = preInitInfo.loader;
  136. if (loader != null)
  137. {
  138. string preInitLibraryName = loader.GetPreInitLibraryName(preInitInfo.buildTarget,
  139. preInitInfo.buildTargetGroup);
  140. preInitInfo = null;
  141. UnityEditor.XR.BootOptions.SetXRSDKPreInitLibrary(file.path,
  142. preInitLibraryName);
  143. }
  144. }
  145. catch (Exception e)
  146. {
  147. throw new UnityEditor.Build.BuildFailedException(e);
  148. }
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. public void OnPostGenerateGradleAndroidProject(string path)
  155. {
  156. if (preInitInfo == null)
  157. return;
  158. // android builds move the files to a different location than is in the BuildReport, so we have to manually find the boot.config
  159. string[] paths = { "src", "main", "assets", "bin", "Data", "boot.config" };
  160. string fullPath = System.IO.Path.Combine(path, String.Join(Path.DirectorySeparatorChar.ToString(), paths));
  161. try
  162. {
  163. var loader = preInitInfo.loader;
  164. if (loader != null)
  165. {
  166. string preInitLibraryName = loader.GetPreInitLibraryName(preInitInfo.buildTarget,
  167. preInitInfo.buildTargetGroup);
  168. preInitInfo = null;
  169. UnityEditor.XR.BootOptions.SetXRSDKPreInitLibrary(fullPath, preInitLibraryName);
  170. }
  171. }
  172. catch (Exception e)
  173. {
  174. throw new UnityEditor.Build.BuildFailedException(e);
  175. }
  176. }
  177. }
  178. }