Jenkins基于Unity的自动化打包
介绍
Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。主要功能包括:1、持续的软件版本发布/测试项目。2、监控外部调用执行的工作。这么说比较官方,说白了,它就是一种集承了多种常用的插件于一身的工具平台,通过这个平台你能很方便的管控你的项目!它的强大之处在于它能直接调用外部的shell指令和bat,那么今天我们一起去解开一点点它的什么面纱,为什么说是一点点呢?因为它太强大了,太深了!
Unity打包脚本
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
public class PerformBuild
{
private static string m_AppName = PlayerSettings.productName;
private static string m_AndroidPath = Application.dataPath + "/../BuildTarget/Android/";
private static string m_IOSPath = Application.dataPath + "/../BuildTarget/IOS/";
private static string m_WindowsPath = Application.dataPath + "/../BuildTarget/Windows/";
private static string m_WebGLPath = Application.dataPath + "/../BuildTarget/WebGL/";
static string[] FindEnableEditorScenes()
{
List<string> editorScenes = new List<string>();
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
{
if (!scene.enabled) continue;
editorScenes.Add(scene.path);
}
return editorScenes.ToArray();
}
static string GetBuildPathWebGL()
{
if (!System.IO.Directory.Exists(m_WebGLPath))
{
System.IO.Directory.CreateDirectory(m_WebGLPath);
}
return m_WebGLPath;
}
static string GetBuildPathStandaloneWindows64()
{
if (!System.IO.Directory.Exists(m_WindowsPath))
{
System.IO.Directory.CreateDirectory(m_WindowsPath);
}
return m_WindowsPath;
}
static string GetBuildPathAndroid()
{
string dirPath = m_AndroidPath + Application.productName + ".apk";
if (!System.IO.Directory.Exists(m_AndroidPath))
{
System.IO.Directory.CreateDirectory(m_AndroidPath);
}
return dirPath;
}
#region 打包机调用打包PC版本
[UnityEditor.MenuItem("Tools/PerformBuild/Command Line Build StandaloneWindows64")]
public static void CommandLineBuildPC()
{
Debug.Log("Command line build android version\n----------------\n------------------");
string[] scenes = FindEnableEditorScenes();
string path = GetBuildPathStandaloneWindows64();
//清空生成的文件夹
DeleteDir(m_WindowsPath);
if (scenes == null || scenes.Length == 0 || path == null)
{
Debug.LogError("please add scene to build setting...");
return;
}
Debug.Log($"Path: \"{path}\"");
for (int i = 0; i < scenes.Length; ++i)
{
Debug.Log($"Scene[{i}]: \"{scenes[i]}\"");
}
Debug.Log("Starting StandaloneWindows64 Build!");
string name = $"/{m_AppName}.exe";
string savePath = m_WindowsPath + name;
BuildPipeline.BuildPlayer(scenes, savePath, BuildTarget.StandaloneWindows64, BuildOptions.None);
}
#endregion
[UnityEditor.MenuItem("Tools/PerformBuild/Command Line Build WepGL")]
static void CommandLineBuildWebGL()
{
Debug.Log("Command line build\n----------------\n------------------");
string[] scenes = FindEnableEditorScenes();
string path = GetBuildPathWebGL();
//清空生成的文件夹
DeleteDir(m_WebGLPath);
if (scenes == null || scenes.Length == 0 || path == null)
{
Debug.LogError("please add scene to build setting...");
return;
}
Debug.Log($"Path:\"{path}\"");
for (int i = 0; i < scenes.Length; i++)
{
Debug.Log($"Scene[{i}]: \"{scenes[i]}\"");
}
Debug.Log("Starting Build!");
BuildPipeline.BuildPlayer(scenes, path, BuildTarget.WebGL, BuildOptions.None);
}
[UnityEditor.MenuItem("Tools/PerformBuild/Command Line Build Android")]
static void CommandlineBuildAndroid()
{
Debug.Log("Command line build android version\n----------------\n------------------");
string[] scenes = FindEnableEditorScenes();
string path = GetBuildPathAndroid();
//清空生成的文件夹
DeleteDir(m_AndroidPath);
if (scenes == null || scenes.Length == 0 || path == null)
{
Debug.LogError("please add scene to build setting...");
return;
}
Debug.Log($"Path:\"{path}\"");
for (int i = 0; i < scenes.Length; i++)
{
Debug.Log($"Scene[{i}]: \"{scenes[i]}\"");
}
Debug.Log("Starting Android Build!");
BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.None);
}
public static void DeleteDir(string scrPath)
{
try
{
DirectoryInfo dir = new DirectoryInfo(scrPath);
FileSystemInfo[] fileInfo = dir.GetFileSystemInfos();
foreach (FileSystemInfo info in fileInfo)
{
if (info is DirectoryInfo)
{
DirectoryInfo subdir = new DirectoryInfo(info.FullName);
subdir.Delete(true);
}
else
{
File.Delete(info.FullName);
}
}
}
catch (Exception e)
{
Debug.LogError(e);
throw;
}
}
}
Windows批处理脚本
@echo off
set UNITY_PATH="D:\AllUnity\2019.4.10f1\Editor\Unity.exe"
set UNITY_PROJECT_PATH="G:\2023AllUnityProject\TestProject\GalleryFramework"
set UNITY_METHOD_NAME="PerformBuild.CommandlineBuildAndroid"
%UNITY_PATH% -quit -batchmode -projectPath %UNITY_PROJECT_PATH% -executeMethod %UNITY_METHOD_NAME% -logFile "log.txt"
if not %errorlevel%==0 ( goto fail ) else ( goto success )
:success
echo Success
goto end
:fail
echo Fail
goto end
:end
echo Build end
pause
参考资料
【CI - Jenkins基于Unity的自动化打包】 https://www.bilibili.com/video/BV1rA411K7sQ/?share_source=copy_web&vd_source=6e65b6e7bf178fc72e7aeef850ddab09