78 lines
2.9 KiB
C#
78 lines
2.9 KiB
C#
|
|
using System.Collections;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.IO;
|
|||
|
|
using UnityEditor;
|
|||
|
|
using UnityEditor.Build;
|
|||
|
|
using UnityEngine;
|
|||
|
|
using UnityEngine.SceneManagement;
|
|||
|
|
|
|||
|
|
namespace GeneralTools
|
|||
|
|
{
|
|||
|
|
public class OnPreprocessBuildDoing : IPreprocessBuild, IPostprocessBuild
|
|||
|
|
{
|
|||
|
|
public int callbackOrder { get { return 0; } }
|
|||
|
|
/// <summary>
|
|||
|
|
/// 打包前
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="target"></param>
|
|||
|
|
/// <param name="path"></param>
|
|||
|
|
public void OnPreprocessBuild(BuildTarget target, string path)
|
|||
|
|
{
|
|||
|
|
CustomPlayerSettings();
|
|||
|
|
CheckCertificatePath();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 打包后
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="target"></param>
|
|||
|
|
/// <param name="path"></param>
|
|||
|
|
public void OnPostprocessBuild(BuildTarget target, string path)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// //代码编译完成时调用
|
|||
|
|
/// </summary>
|
|||
|
|
[UnityEditor.Callbacks.DidReloadScripts]
|
|||
|
|
static void OnScriptsEditOver()
|
|||
|
|
{
|
|||
|
|
//注册打包发布的事件;unity在打包发布的时候会判断buildPlayerHandler 是不是为null,为空就执行默认打包方法,不为空就执行注册的事件
|
|||
|
|
BuildPlayerWindow.RegisterBuildPlayerHandler(OverridesBuildPlayer);
|
|||
|
|
}
|
|||
|
|
public static void OverridesBuildPlayer(BuildPlayerOptions BPOption)
|
|||
|
|
{
|
|||
|
|
//添加自己的逻辑
|
|||
|
|
CertificateSetting setting = Resources.Load<CertificateSetting>("Setting/CertificateSetting");
|
|||
|
|
if (setting == null)
|
|||
|
|
{
|
|||
|
|
Debug.LogError("未找到CertificateSetting,请生成");
|
|||
|
|
if (EditorUtility.DisplayDialog("提示", "未找到CertificateSetting,请生成", "生成"))
|
|||
|
|
{
|
|||
|
|
Menu.SetCertificate();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Debug.Log("开始打包,当前项目名称:" + (string.IsNullOrWhiteSpace(setting.productID) ? Application.productName : setting.productID));
|
|||
|
|
BuildPlayerWindow.DefaultBuildMethods.BuildPlayer(BPOption);//调用unity默认的打包方法。取消打包,不用写其他代码
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private static void CustomPlayerSettings()
|
|||
|
|
{
|
|||
|
|
PlayerSettings.runInBackground = true;
|
|||
|
|
PlayerSettings.displayResolutionDialog = ResolutionDialogSetting.Disabled;
|
|||
|
|
PlayerSettings.usePlayerLog = false;
|
|||
|
|
PlayerSettings.forceSingleInstance = true;
|
|||
|
|
PlayerSettings.SplashScreen.show = false;
|
|||
|
|
}
|
|||
|
|
private static void CheckCertificatePath()
|
|||
|
|
{
|
|||
|
|
string path = DevicePath.CertificateProgramPath;
|
|||
|
|
if (!File.Exists(path))
|
|||
|
|
{
|
|||
|
|
Debug.Log("未找到证书文件,生成默认证书");
|
|||
|
|
CertificateManager.CreateOneCertificate();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|