PreProcessBuild.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. if (target != BuildTarget.Android)
  69. {
  70. string message = "Vulkan graphics API is not supported by AVPro Video.";
  71. message += "\n\nPlease go to Player Settings > Auto Graphics API and remove Vulkan from the list.";
  72. ShowAbortDialog(message);
  73. }
  74. }
  75. }
  76. static void ShowAbortDialog(string message)
  77. {
  78. if (!EditorUtility.DisplayDialog("Continue Build?", message, "Continue", "Cancel"))
  79. {
  80. throw new BuildFailedException(message);
  81. }
  82. }
  83. static bool IsTargetMacOS(BuildTarget target)
  84. {
  85. #if UNITY_2017_3_OR_NEWER
  86. return (target == BuildTarget.StandaloneOSX);
  87. #else
  88. return (target == BuildTarget.StandaloneOSXUniversal || target == BuildTarget.StandaloneOSXIntel);
  89. #endif
  90. }
  91. static int GetGraphicsApiIndex(BuildTarget target, GraphicsDeviceType api)
  92. {
  93. int result = -1;
  94. GraphicsDeviceType[] devices = UnityEditor.PlayerSettings.GetGraphicsAPIs(target);
  95. for (int i = 0; i < devices.Length; i++)
  96. {
  97. if (devices[i] == api)
  98. {
  99. result = i;
  100. break;
  101. }
  102. }
  103. return result;
  104. }
  105. }
  106. }