十分钟教你搭建Jenkins导出Unity安卓环境教程

前言

想配置一个不同的打包工具,那种可以随时想要一个运行包就可以自己打包看效果,这就要用到本篇给大家分享的搭建Jenkins打包导出Unity安卓环境的教程。

准备工具

准备Jenkins环境,可以选择去Jenkins官网下载,我这里准备了一个Jenkins的war包。

搭建步骤

首要前提本地Unity已经安装好UnityAndroid的打包环境,确保能够正确的用Unity导出Androidapk包,这里就不过多介绍Unity如何导出Android环境了,自行解决。

命令行启动Jenkins,java-jarjenkins.war,会看到如下运行即代表启动成功,可以在浏览器输入http://localhost:8080/来测试,会看到进入Jenkins网站。

扫描二维码关注公众号,回复: 13762346 查看本文章

构建项目

注意windows路径和命令行的路径的转变,上图是Unity打包apk的批处理的路径,这个批处理如下图,并且要放到Unity的工程下。

注意:如果批处理路径中不能带有空格,如果有空格的话可以加上双引号,确保这个批处理能够正确的导出apk,批处理代码如下,并且注意用utf8保存:

@echo off
echo "Start build apk..."
C:\"Program Files"\Unity1748f1\Unity\Editor\Unity.exe -projectPath D:\study\gitoschina\jenkinsTest\HelloUnity -quit -batchmode -executeMethod PerformBuild.CommandLineBuildAndroid -logFile build.log
REM %1 -projectPath %2 -quit -batchmode -executeMethod APKBuild.Build -logFile build.log
if not %errorlevel%==0 ( goto fail ) else ( goto success )
:success
echo Build APK OK
goto end
:fail
echo Build APK Fail
goto end
:end
pause

工程Editor目录下添加自动构建的代码:

using UnityEditor;
using System.IO;
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
class PerformBuild
{
    static string[] GetBuildScenes()
    {
        List<string> names = new List<string>();
        foreach (EditorBuildSettingsScene e in EditorBuildSettings.scenes)
        {
            if (e == null)
                continue;
            if (e.enabled)
                names.Add(e.path);
        }
        return names.ToArray();
    }
    static string GetBuildPath()
    {
        string dirPath = Application.dataPath + "/../build/iPhone";
        if (!System.IO.Directory.Exists(dirPath))
        {
            System.IO.Directory.CreateDirectory(dirPath);
        }
        return dirPath;
    }
    [UnityEditor.MenuItem("Tools/PerformBuild/Test Command Line Build iPhone Step")]
    static void CommandLineBuild()
    {
        Debug.Log("Command line build\n------------------\n------------------");
        string[] scenes = GetBuildScenes();
        string path = GetBuildPath();
        if (scenes == null || scenes.Length == 0 || path == null)
            return;
        Debug.Log(string.Format("Path: \"{0}\"", path));
        for (int i = 0; i < scenes.Length; ++i)
        {
            Debug.Log(string.Format("Scene[{0}]: \"{1}\"", i, scenes[i]));
        }
        Debug.Log("Starting Build!");
        BuildPipeline.BuildPlayer(scenes, path, BuildTarget.iOS, BuildOptions.None);
    }
    static string GetBuildPathAndroid()
    {
        string dirPath = Application.dataPath.Replace("/Assets", "") + "/../build/android/"+Application.productName+".apk";
        if (!System.IO.Directory.Exists(dirPath))
        {
            System.IO.Directory.CreateDirectory(dirPath);
        }
        return dirPath;
    }
    [UnityEditor.MenuItem("Tools/PerformBuild/Test Command Line Build Step Android")]
    static void CommandLineBuildAndroid()
    {
        Debug.Log("Command line build android version\n------------------\n------------------");
        string[] scenes = GetBuildScenes();
        string path = GetBuildPathAndroid();
        if (scenes == null || scenes.Length == 0 || path == null)
        {
            Debug.LogError("Please add scene to buildsetting...");
            return;
        }
        Debug.Log(string.Format("Path: \"{0}\"", path));
        for (int i = 0; i < scenes.Length; ++i)
        {
            Debug.Log(string.Format("Scene[{0}]: \"{1}\"", i, scenes[i]));
        }
        Debug.Log("Starting Android Build!");
        BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.None);
    }
}

点击Jenkins构建

本地生成apk

以上就是搭建Jenkins打包导出Unity安卓环境的全部内容,希望能对大家有所帮助。

猜你喜欢

转载自blog.csdn.net/bycw666/article/details/123632416