Unity 自动创建文件夹打包到指定的目录并更新发布版本号
为什么要写这个?
每次打包都要执行
- 打开打包面板
- 创建文件夹
- 打包
Emmmmmm 觉得太麻烦了
实现代码
using System;
using System.IO;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
/// <summary>
/// 导出和发布
/// </summary>
public static class ExportBuild
{
/// <summary>
/// 发布 APK
/// </summary>
[MenuItem("Tools/Build Apk", false, 20)]
private static void BuildAssetBundleForAndroid()
{
DirectoryInfo dataDir = new DirectoryInfo(Application.dataPath);
string apkPath = dataDir.Parent.FullName + "/Apks/" +
DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".apk";
BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, apkPath, BuildTarget.Android,
BuildOptions.None);
Debug.Log(apkPath);
}
/// <summary>
/// 发布 AS Release 版本
/// </summary>
[MenuItem("Tools/Build AS Project Release", false, 20)]
private static void ExportToAndroidStudioToRelease()
{
var apkPath = GetBuildPath();
EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Gradle;
EditorUserBuildSettings.development = false;
EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, apkPath, BuildTarget.Android,
BuildOptions.None);
Debug.Log(apkPath);
}
/// <summary>
/// 获取发布路径
/// </summary>
/// <returns></returns>
private static string GetBuildPath()
{
DirectoryInfo dataDir = new DirectoryInfo(Application.dataPath);
string workPathName = "Build";
switch (EditorUserBuildSettings.activeBuildTarget)
{
case BuildTarget.StandaloneOSX:
case BuildTarget.iOS:
workPathName = "Xcode";
break;
case BuildTarget.Android:
workPathName = "ASWork";
break;
}
string apkPath = $"{dataDir.Parent.FullName}/{workPathName}/{DateTime.Now:yyyy-MM-dd-HH-mm-ss}";
DirectoryInfo asWorkDir = new DirectoryInfo(apkPath);
if (asWorkDir.Exists)
{
asWorkDir.Delete(true);
}
else
{
asWorkDir.Create();
}
return apkPath;
}
/// <summary>
/// 发布 AS Debug 版本
/// </summary>
[MenuItem("Tools/Build AS Project Debug", false, 20)]
private static void ExportToAndroidStudioToDevelop()
{
string apkPath = GetBuildPath();
EditorUserBuildSettings.androidBuildSystem = AndroidBuildSystem.Gradle;
EditorUserBuildSettings.development = true;
EditorUserBuildSettings.exportAsGoogleAndroidProject = true;
BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, apkPath, BuildTarget.Android,
BuildOptions.None);
Debug.Log(apkPath);
}
/// <summary>
/// 发布 Xcode Debug 版本
/// </summary>
[MenuItem("Tools/Build Xcode Project Debug", false, 30)]
private static void ExportToXcodeToDevelop()
{
string xcodePath = GetBuildPath();
EditorUserBuildSettings.development = true;
BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, xcodePath, BuildTarget.iOS,
BuildOptions.None);
Debug.Log(xcodePath);
}
/// <summary>
/// 发布 Xcode Release 版本
/// </summary>
[MenuItem("Tools/Build Xcode Project Release", false, 30)]
private static void ExportToXcodeToRelease()
{
string xcodePath = GetBuildPath();
EditorUserBuildSettings.development = false;
BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, xcodePath, BuildTarget.iOS,
BuildOptions.None);
Debug.Log(xcodePath);
}
/// <summary>
/// 设置 发布版本号更新
/// </summary>
/// <param name="target"></param>
/// <param name="path"></param>
[PostProcessBuild(999)]
public static void OnPostProcessBuild(BuildTarget target, string path)
{
if (target == BuildTarget.Android)
{
// Update Build Version Code
PlayerSettings.Android.bundleVersionCode =
Convert.ToInt32(PlayerSettings.Android.bundleVersionCode) + 1;
}
if (target == BuildTarget.iOS)
{
// Update Build Version Code
PlayerSettings.iOS.buildNumber =
(Convert.ToInt32(PlayerSettings.iOS.buildNumber) + 1).ToString();
}
}
}
|