PreProcessBuild.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #if UNITY_2018_1_OR_NEWER
  2. #define UNITY_SUPPORTS_BUILD_REPORT
  3. #endif
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using UnityEngine;
  7. using UnityEditor;
  8. using UnityEngine.Rendering;
  9. using UnityEditor.Build;
  10. #if UNITY_SUPPORTS_BUILD_REPORT
  11. using UnityEditor.Build.Reporting;
  12. #endif
  13. //-----------------------------------------------------------------------------
  14. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  15. //-----------------------------------------------------------------------------
  16. namespace RenderHeads.Media.AVProMovieCapture.Editor
  17. {
  18. public class PreProcessBuild :
  19. #if UNITY_SUPPORTS_BUILD_REPORT
  20. IPreprocessBuildWithReport
  21. #else
  22. IPreprocessBuild
  23. #endif
  24. {
  25. public int callbackOrder { get { return 0; } }
  26. #if UNITY_SUPPORTS_BUILD_REPORT
  27. public void OnPreprocessBuild(BuildReport report)
  28. {
  29. OnPreprocessBuild(report.summary.platform, report.summary.outputPath);
  30. }
  31. #endif
  32. public void OnPreprocessBuild(BuildTarget target, string path)
  33. {
  34. if (target == BuildTarget.Android)
  35. {
  36. #if UNITY_2020_2_OR_NEWER
  37. const string AVPMC_ANDROID_VULKAN_PRETRANSFORM = "AVPMC_ANDROID_VULKAN_PRETRANSFORM";
  38. PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, out string[] definesArray);
  39. List<string> defines = new List<string>(definesArray);
  40. if (PlayerSettings.vulkanEnablePreTransform == true && !defines.Contains(AVPMC_ANDROID_VULKAN_PRETRANSFORM))
  41. {
  42. Debug.Log("Adding AVPMC_ANDROID_VULKAN_PRETRANSFORM");
  43. defines.Add(AVPMC_ANDROID_VULKAN_PRETRANSFORM);
  44. PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, defines.ToArray());
  45. }
  46. else if (PlayerSettings.vulkanEnablePreTransform == false && defines.Contains(AVPMC_ANDROID_VULKAN_PRETRANSFORM))
  47. {
  48. Debug.Log("Removing AVPMC_ANDROID_VULKAN_PRETRANSFORM");
  49. defines.Remove(AVPMC_ANDROID_VULKAN_PRETRANSFORM);
  50. PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, defines.ToArray());
  51. }
  52. #endif
  53. }
  54. else
  55. if (target == BuildTarget.iOS)
  56. {
  57. int indexMetal = GetGraphicsApiIndex(target, GraphicsDeviceType.Metal);
  58. int indexOpenGLES2 = GetGraphicsApiIndex(target, GraphicsDeviceType.OpenGLES2);
  59. int indexOpenGLES3 = GetGraphicsApiIndex(target, GraphicsDeviceType.OpenGLES3);
  60. if (indexMetal < 0)
  61. {
  62. string message = "Metal graphics API is required by AVPro Movie Capture.";
  63. message += "\n\nPlease go to Player Settings > Auto Graphics API and add Metal to the top of the list.";
  64. ShowAbortDialog(message);
  65. }
  66. if (indexOpenGLES2 >= 0 && indexMetal >= 0 && indexOpenGLES2 < indexMetal)
  67. {
  68. string message = "OpenGLES 2.0 graphics API is not supported by AVPro Movie Capture.";
  69. message += "\n\nPlease go to Player Settings > Auto Graphics API and add Metal to the top of the list.";
  70. ShowAbortDialog(message);
  71. }
  72. if (indexOpenGLES3 >= 0 && indexMetal >= 0 && indexOpenGLES3 < indexMetal)
  73. {
  74. string message = "OpenGLES 3.0 graphics API is not supported by AVPro Movie Capture.";
  75. message += "\n\nPlease go to Player Settings > Auto Graphics API and add Metal to the top of the list.";
  76. ShowAbortDialog(message);
  77. }
  78. }
  79. }
  80. static void ShowAbortDialog(string message)
  81. {
  82. if (!EditorUtility.DisplayDialog("Continue Build?", message, "Continue", "Cancel"))
  83. {
  84. throw new BuildFailedException(message);
  85. }
  86. }
  87. static int GetGraphicsApiIndex(BuildTarget target, GraphicsDeviceType api)
  88. {
  89. int result = -1;
  90. GraphicsDeviceType[] devices = UnityEditor.PlayerSettings.GetGraphicsAPIs(target);
  91. for (int i = 0; i < devices.Length; i++)
  92. {
  93. if (devices[i] == api)
  94. {
  95. result = i;
  96. break;
  97. }
  98. }
  99. return result;
  100. }
  101. }
  102. }