123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- //==========================================================
- //
- // Copyright (c) Guangzhou Shixiang Technology Co.,Ltd.
- // All rights reserved.
- //
- //==========================================================
- using System;
- using System.IO;
- using UnityEditor;
- using UnityEditor.Build;
- using UnityEditor.Build.Reporting;
- using UnityEngine;
- namespace GxrSdk.Editor
- {
- /// <summary>
- /// build开始时添加平台选择,以便选择对应的AndroidManifest.xml
- /// </summary>
- public class GxrPreprocessBuild : IPreprocessBuildWithReport, IPostprocessBuildWithReport
- {
- /// <summary>
- /// 是否需要选择配置文件
- /// 之前是在打包的时候弹出对话框让选择运行平台,
- /// 现在改为Config中进行配置,选择之后实时更新配置文件,
- /// 不需要在打包时来弹出对话框选择了
- /// </summary>
- private bool _needSelectManifest = false;
- /// <summary>
- /// 是否在编译Android平台
- /// </summary>
- private bool _isBuildingAndroid = true;
- private DateTime _startTime;
- public int callbackOrder
- {
- get { return 0; }
- }
- public void OnPreprocessBuild(BuildReport report)
- {
- Debug.Log("[OnPreprocessBuild]");
- _startTime = DateTime.Now;
- _isBuildingAndroid = report.summary.platform == BuildTarget.Android;
- SetResizableWindow();
- DisableOptimizedFramePacing();
- DisableGpuSkinning();
- SetOrientationLandscapeLeft();
- SelectManifest();
- }
- public void OnPostprocessBuild(BuildReport report)
- {
- Debug.Log("[OnPostprocessBuild]");
- TimeSpan deltaTime = DateTime.Now - _startTime;
- string hours = deltaTime.Hours.ToString("00");
- string minutes = deltaTime.Minutes.ToString("00");
- string seconds = deltaTime.Seconds.ToString("00");
- Debug.Log($"build time: {hours}:{minutes}:{seconds}");
- }
- /// <summary>
- /// 选中AndroidManifest.xml文件
- /// </summary>
- private void SelectManifest()
- {
- if (_needSelectManifest)
- {
- if (_isBuildingAndroid)
- {
- bool result = EditorUtility.DisplayDialog("选择运行平台", "该应用要运行于手机端还是主控端?", "手机", "主控");
- string fileName = (result) ? ("AndroidManifestPhone.xml") : ("AndroidManifestAndroidBox.xml");
- string srcPath = Application.dataPath + "/Plugins/Android/" + fileName;
- string destPath = Application.dataPath + "/Plugins/Android/AndroidManifest.xml";
- // 根据选择运行平台把AndroidManifest.xml替换成对应的配置文件
- if (File.Exists(srcPath))
- {
- File.Delete(destPath);
- File.Copy(srcPath, destPath);
- AssetDatabase.Refresh();
- }
- }
- }
- }
- /// <summary>
- /// 设置可调整窗口大小
- /// [兼容性]Mate20手机,应用启动后闪屏,提示该应用不支持分屏。
- /// 配置PlayerSettings.Android.resizableWindow后,会添加android:resizeableWindow属性。
- /// </summary>
- private void SetResizableWindow()
- {
- #if UNITY_2020_3_OR_NEWER || (UNITY_2019_4_OR_NEWER && !UNITY_2020_1_OR_NEWER)
- if (_isBuildingAndroid)
- {
- // PlayerSettings.Android.resizableWindow = true;
- GxrReflectionUtils.SetPropertyValue(typeof(PlayerSettings.Android), "resizableWindow", true);
- }
- #endif
- }
- /// <summary>
- /// 取消帧率间隔优化
- /// 在使用NativeRender情况下打开该功能会导致显示异常
- /// </summary>
- private void DisableOptimizedFramePacing()
- {
- #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)
- if (_isBuildingAndroid)
- {
- // PlayerSettings.Android.optimizedFramePacing = false;
- GxrReflectionUtils.SetPropertyValue(typeof(PlayerSettings.Android), "optimizedFramePacing", false);
- }
- #endif
- }
- /// <summary>
- /// 取消使用Gpu计算蒙皮
- /// 在使用NativeRender情况下,如果使用Gpu去计算蒙皮,会抢占资源导致NativeRender效果卡顿
- /// </summary>
- private void DisableGpuSkinning()
- {
- if (_isBuildingAndroid)
- {
- GxrReflectionUtils.SetPropertyValue(typeof(PlayerSettings), "gpuSkinning", false);
- }
- }
- /// <summary>
- /// 设置屏幕旋转为LandscapeLeft
- /// </summary>
- private void SetOrientationLandscapeLeft()
- {
- // PlayerSettings.defaultInterfaceOrientation = UIOrientation.LandscapeLeft;
- if (_isBuildingAndroid)
- {
- GxrReflectionUtils.SetPropertyValue(typeof(PlayerSettings), "defaultInterfaceOrientation", UIOrientation.LandscapeLeft);
- }
- }
- }
- }
|