//==========================================================
//
// 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
{
///
/// build开始时添加平台选择,以便选择对应的AndroidManifest.xml
///
public class GxrPreprocessBuild : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
///
/// 是否需要选择配置文件
/// 之前是在打包的时候弹出对话框让选择运行平台,
/// 现在改为Config中进行配置,选择之后实时更新配置文件,
/// 不需要在打包时来弹出对话框选择了
///
private bool _needSelectManifest = false;
///
/// 是否在编译Android平台
///
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}");
}
///
/// 选中AndroidManifest.xml文件
///
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();
}
}
}
}
///
/// 设置可调整窗口大小
/// [兼容性]Mate20手机,应用启动后闪屏,提示该应用不支持分屏。
/// 配置PlayerSettings.Android.resizableWindow后,会添加android:resizeableWindow属性。
///
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
}
///
/// 取消帧率间隔优化
/// 在使用NativeRender情况下打开该功能会导致显示异常
///
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
}
///
/// 取消使用Gpu计算蒙皮
/// 在使用NativeRender情况下,如果使用Gpu去计算蒙皮,会抢占资源导致NativeRender效果卡顿
///
private void DisableGpuSkinning()
{
if (_isBuildingAndroid)
{
GxrReflectionUtils.SetPropertyValue(typeof(PlayerSettings), "gpuSkinning", false);
}
}
///
/// 设置屏幕旋转为LandscapeLeft
///
private void SetOrientationLandscapeLeft()
{
// PlayerSettings.defaultInterfaceOrientation = UIOrientation.LandscapeLeft;
if (_isBuildingAndroid)
{
GxrReflectionUtils.SetPropertyValue(typeof(PlayerSettings), "defaultInterfaceOrientation", UIOrientation.LandscapeLeft);
}
}
}
}