在 Unity 中自动化打包流程需要编写自定义的编辑器脚本。以下是一个简化的示例代码,演示如何在 Unity 编辑器中导出 Android 工程,并使用导出的工程生成 AAR 文件。这个过程涉及两个主要步骤:
- 导出 Android 工程。
- 使用生成的 Android 工程打包 AAR。
请确保您已经在 Unity 中安装了必要的 Android 开发工具,并且这些工具在系统路径中可用。此外,您需要 Android SDK 和相关构建工具的支持。
using UnityEditor;
using UnityEditor.Build.Reporting;
using UnityEditor.Build.Player;
using UnityEngine;
using System.Diagnostics;
using System.IO;
public class AndroidBuildAutomation : MonoBehaviour
{
[MenuItem("Build/Export Android Project and Build AAR")]
public static void ExportAndBuildAAR()
{
string exportPath = "Your/Export/Path"; // 替换为您的导出路径
string aarOutputPath = "Your/Output/Path"; // 替换为AAR导出的路径
// Step 1: Export Android Project
ExportAndroidProject(exportPath);
// Step 2: Build AAR from Exported Project
BuildAAR(exportPath, aarOutputPath);
}
private static void ExportAndroidProject(string exportPath)
{
// 确保导出路径存在
if (!Directory.Exists(exportPath))
{
Directory.CreateDirectory(exportPath);
}
// 设置构建选项
BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions
{
scenes = new[] { "Assets/Scenes/MainScene.unity" },
locationPathName = exportPath,
target = BuildTarget.Android,
options = BuildOptions.AcceptExternalModificationsToPlayer
};
// 开始构建并导出工程
BuildReport report = BuildPipeline.BuildPlayer(buildPlayerOptions);
BuildSummary summary = report.summary;
if (summary.result == BuildResult.Succeeded)
{
UnityEngine.Debug.Log("Android Project Exported Successfully.");
}
else
{
UnityEngine.Debug.LogError("Failed to Export Android Project.");
}
}
private static void BuildAAR(string projectPath, string aarOutputPath)
{
string gradlePath = "Your/Gradle/Path"; // 替换为您的Gradle路径
// 设置需要执行的命令
Process process = new Process();
process.StartInfo.FileName = gradlePath;
process.StartInfo.WorkingDirectory = projectPath; // 设置为Android工程的根目录
process.StartInfo.Arguments = "assembleRelease"; // 运行assembleRelease构建AAR
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.OutputDataReceived += (sender, args) => UnityEngine.Debug.Log(args.Data);
process.ErrorDataReceived += (sender, args) => UnityEngine.Debug.LogError(args.Data);
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
if (process.ExitCode == 0)
{
UnityEngine.Debug.Log("AAR Built Successfully.");
// 移动生成的AAR到目标输出路径
string aarFileName = "YourAARFileName-release.aar"; // 替换为生成的AAR文件名
string aarSourcePath = Path.Combine(projectPath, "build/outputs/aar", aarFileName);
string aarDestinationPath = Path.Combine(aarOutputPath, aarFileName);
if (File.Exists(aarSourcePath))
{
File.Copy(aarSourcePath, aarDestinationPath, true);
UnityEngine.Debug.Log($"AAR moved to {aarDestinationPath}");
}
else
{
UnityEngine.Debug.LogError("AAR file not found!");
}
}
else
{
UnityEngine.Debug.LogError("Failed to build AAR.");
}
}
}
注意事项
-
路径设置: 根据您的实际环境设置 exportPath, aarOutputPath, gradlePath, 和 aarFileName。
-
Gradle: 确保您的系统已经安装了 Gradle,并且路径是正确的。
-
Unity 和 Android SDK/NDK 配置: 确保 Unity 的 Android 构建工具配置正确。
-
权限问题: 确保 Unity 和外部工具对指定的目录有写入权限。
这个脚本是在 Unity 的编辑器模式下执行的,并假设您已经设置了适当的 Android 环境。如有需要,您可能需要调整路径以及项目的具体配置信息。