PreProcessBuild.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. namespace RenderHeads.Media.AVProVideo.Editor
  14. {
  15. public class PreProcessBuild :
  16. #if UNITY_SUPPORTS_BUILD_REPORT
  17. IPreprocessBuildWithReport
  18. #else
  19. IPreprocessBuild
  20. #endif
  21. {
  22. public int callbackOrder { get { return 0; } }
  23. #if UNITY_SUPPORTS_BUILD_REPORT
  24. public void OnPreprocessBuild(BuildReport report)
  25. {
  26. OnPreprocessBuild(report.summary.platform, report.summary.outputPath);
  27. }
  28. #endif
  29. public void OnPreprocessBuild(BuildTarget target, string path)
  30. {
  31. if (IsTargetMacOS(target) || target == BuildTarget.iOS || target == BuildTarget.tvOS)
  32. {
  33. int indexMetal = GetGraphicsApiIndex(target, GraphicsDeviceType.Metal);
  34. int indexOpenGLCore = GetGraphicsApiIndex(target, GraphicsDeviceType.OpenGLCore);
  35. int indexOpenGLES2 = GetGraphicsApiIndex(target, GraphicsDeviceType.OpenGLES2);
  36. int indexOpenGLES3 = GetGraphicsApiIndex(target, GraphicsDeviceType.OpenGLES3);
  37. if (indexMetal < 0)
  38. {
  39. string message = "Metal graphics API is required by AVPro Video.";
  40. message += "\n\nPlease go to Player Settings > Auto Graphics API and add Metal to the top of the list.";
  41. ShowAbortDialog(message);
  42. }
  43. if (indexOpenGLCore >= 0 && indexMetal >=0 && indexOpenGLCore < indexMetal)
  44. {
  45. string message = "OpenGL graphics API is not supported by AVPro Video.";
  46. message += "\n\nVideo will play but no video frames will be displayed.";
  47. message += "\n\nPlease go to Player Settings > Auto Graphics API and add Metal to the top of the list.";
  48. ShowAbortDialog(message);
  49. }
  50. if (indexOpenGLES2 >= 0 && indexMetal >=0 && indexOpenGLES2 < indexMetal)
  51. {
  52. string message = "OpenGLES2 graphics API is not supported by AVPro Video.";
  53. message += "\n\nVideo will play but no video frames will be displayed.";
  54. message += "\n\nPlease go to Player Settings > Auto Graphics API and add Metal to the top of the list.";
  55. ShowAbortDialog(message);
  56. }
  57. if (indexOpenGLES3 >= 0 && indexMetal >=0 && indexOpenGLES3 < indexMetal)
  58. {
  59. string message = "OpenGLES3 graphics API is not supported by AVPro Video.";
  60. message += "\n\nVideo will play but no video frames will be displayed.";
  61. message += "\n\nPlease go to Player Settings > Auto Graphics API and add Metal to the top of the list.";
  62. ShowAbortDialog(message);
  63. }
  64. }
  65. int indexVulkan = GetGraphicsApiIndex(target, GraphicsDeviceType.Vulkan);
  66. if (indexVulkan >= 0)
  67. {
  68. string message = "Vulkan graphics API is not supported by AVPro Video.";
  69. if (target == BuildTarget.Android)
  70. {
  71. message += "\n\nPlease go to Player Settings > Android > Auto Graphics API and remove Vulkan from the list.\nOnly OpenGL ES 2.0 and 3.0 are supported on Android.";
  72. }
  73. else
  74. {
  75. message += "\n\nPlease go to Player Settings > Auto Graphics API and remove Vulkan from the list.";
  76. }
  77. ShowAbortDialog(message);
  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 bool IsTargetMacOS(BuildTarget target)
  88. {
  89. #if UNITY_2017_3_OR_NEWER
  90. return (target == BuildTarget.StandaloneOSX);
  91. #else
  92. return (target == BuildTarget.StandaloneOSXUniversal || target == BuildTarget.StandaloneOSXIntel);
  93. #endif
  94. }
  95. static int GetGraphicsApiIndex(BuildTarget target, GraphicsDeviceType api)
  96. {
  97. int result = -1;
  98. GraphicsDeviceType[] devices = UnityEditor.PlayerSettings.GetGraphicsAPIs(target);
  99. for (int i = 0; i < devices.Length; i++)
  100. {
  101. if (devices[i] == api)
  102. {
  103. result = i;
  104. break;
  105. }
  106. }
  107. return result;
  108. }
  109. }
  110. }