Jenkins基于Unity的自动化打包

本文介绍了如何使用Jenkins结合Unity进行自动化打包,包括使用Windows批处理脚本实现Android、iOS、WebGL和StandaloneWindows的打包过程,以及在Unity中编写打包命令行函数。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值