GxrPreprocessBuild.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //==========================================================
  2. //
  3. // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd.
  4. // All rights reserved.
  5. //
  6. //==========================================================
  7. using System;
  8. using System.IO;
  9. using UnityEditor;
  10. using UnityEditor.Build;
  11. using UnityEditor.Build.Reporting;
  12. using UnityEngine;
  13. namespace GxrSdk.Editor
  14. {
  15. /// <summary>
  16. /// build开始时添加平台选择,以便选择对应的AndroidManifest.xml
  17. /// </summary>
  18. public class GxrPreprocessBuild : IPreprocessBuildWithReport, IPostprocessBuildWithReport
  19. {
  20. /// <summary>
  21. /// 是否需要选择配置文件
  22. /// 之前是在打包的时候弹出对话框让选择运行平台,
  23. /// 现在改为Config中进行配置,选择之后实时更新配置文件,
  24. /// 不需要在打包时来弹出对话框选择了
  25. /// </summary>
  26. private bool _needSelectManifest = false;
  27. /// <summary>
  28. /// 是否在编译Android平台
  29. /// </summary>
  30. private bool _isBuildingAndroid = true;
  31. private DateTime _startTime;
  32. public int callbackOrder
  33. {
  34. get { return 0; }
  35. }
  36. public void OnPreprocessBuild(BuildReport report)
  37. {
  38. Debug.Log("[OnPreprocessBuild]");
  39. _startTime = DateTime.Now;
  40. _isBuildingAndroid = report.summary.platform == BuildTarget.Android;
  41. SetResizableWindow();
  42. DisableOptimizedFramePacing();
  43. DisableGpuSkinning();
  44. SetOrientationLandscapeLeft();
  45. SelectManifest();
  46. }
  47. public void OnPostprocessBuild(BuildReport report)
  48. {
  49. Debug.Log("[OnPostprocessBuild]");
  50. TimeSpan deltaTime = DateTime.Now - _startTime;
  51. string hours = deltaTime.Hours.ToString("00");
  52. string minutes = deltaTime.Minutes.ToString("00");
  53. string seconds = deltaTime.Seconds.ToString("00");
  54. Debug.Log($"build time: {hours}:{minutes}:{seconds}");
  55. }
  56. /// <summary>
  57. /// 选中AndroidManifest.xml文件
  58. /// </summary>
  59. private void SelectManifest()
  60. {
  61. if (_needSelectManifest)
  62. {
  63. if (_isBuildingAndroid)
  64. {
  65. bool result = EditorUtility.DisplayDialog("选择运行平台", "该应用要运行于手机端还是主控端?", "手机", "主控");
  66. string fileName = (result) ? ("AndroidManifestPhone.xml") : ("AndroidManifestAndroidBox.xml");
  67. string srcPath = Application.dataPath + "/Plugins/Android/" + fileName;
  68. string destPath = Application.dataPath + "/Plugins/Android/AndroidManifest.xml";
  69. // 根据选择运行平台把AndroidManifest.xml替换成对应的配置文件
  70. if (File.Exists(srcPath))
  71. {
  72. File.Delete(destPath);
  73. File.Copy(srcPath, destPath);
  74. AssetDatabase.Refresh();
  75. }
  76. }
  77. }
  78. }
  79. /// <summary>
  80. /// 设置可调整窗口大小
  81. /// [兼容性]Mate20手机,应用启动后闪屏,提示该应用不支持分屏。
  82. /// 配置PlayerSettings.Android.resizableWindow后,会添加android:resizeableWindow属性。
  83. /// </summary>
  84. private void SetResizableWindow()
  85. {
  86. #if UNITY_2020_3_OR_NEWER || (UNITY_2019_4_OR_NEWER && !UNITY_2020_1_OR_NEWER)
  87. if (_isBuildingAndroid)
  88. {
  89. // PlayerSettings.Android.resizableWindow = true;
  90. GxrReflectionUtils.SetPropertyValue(typeof(PlayerSettings.Android), "resizableWindow", true);
  91. }
  92. #endif
  93. }
  94. /// <summary>
  95. /// 取消帧率间隔优化
  96. /// 在使用NativeRender情况下打开该功能会导致显示异常
  97. /// </summary>
  98. private void DisableOptimizedFramePacing()
  99. {
  100. #if UNITY_2021_2_OR_NEWER || (UNITY_2019_4_OR_NEWER && !UNITY_2020_1_OR_NEWER) || (UNITY_2020_3_OR_NEWER && !UNITY_2021_1_OR_NEWER)
  101. if (_isBuildingAndroid)
  102. {
  103. // PlayerSettings.Android.optimizedFramePacing = false;
  104. GxrReflectionUtils.SetPropertyValue(typeof(PlayerSettings.Android), "optimizedFramePacing", false);
  105. }
  106. #endif
  107. }
  108. /// <summary>
  109. /// 取消使用Gpu计算蒙皮
  110. /// 在使用NativeRender情况下,如果使用Gpu去计算蒙皮,会抢占资源导致NativeRender效果卡顿
  111. /// </summary>
  112. private void DisableGpuSkinning()
  113. {
  114. if (_isBuildingAndroid)
  115. {
  116. GxrReflectionUtils.SetPropertyValue(typeof(PlayerSettings), "gpuSkinning", false);
  117. }
  118. }
  119. /// <summary>
  120. /// 设置屏幕旋转为LandscapeLeft
  121. /// </summary>
  122. private void SetOrientationLandscapeLeft()
  123. {
  124. // PlayerSettings.defaultInterfaceOrientation = UIOrientation.LandscapeLeft;
  125. if (_isBuildingAndroid)
  126. {
  127. GxrReflectionUtils.SetPropertyValue(typeof(PlayerSettings), "defaultInterfaceOrientation", UIOrientation.LandscapeLeft);
  128. }
  129. }
  130. }
  131. }