unity运行环境_不看必后悔系列:十分钟教你搭建Jenkins导出Unity安卓环境教程

65771a77f5308694b0bf72785992b5f1.png

前言

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

准备工具

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

搭建步骤

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

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

f893cd84b8bba464a0ec7371e45add16.png

415f4b8ac8a92d375e7b971e0a3c25b7.png

构建项目

740ef20c41c461636b45db563e8d4942.png

850cb62a022a9b301ac46cc0c7ca0cbb.png

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

55b2e10b9e85b214f484611e9cce3341.png

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

@echo off
echo "Start build apk..."
C:"Program Files"Unity1748f1UnityEditorUnity.exe -projectPath D:studygitoschinajenkinsTestHelloUnity -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 buildn------------------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 versionn------------------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构建

fb0a9f86f1e750bf83e7c6d8b219a6d1.png

本地生成apk

1857df95317849edd0717148536221da.png

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值