【Unity】Jenkins自动打包入门小结

目录

一、下载并安装Jenkins

二、使用Jenkins

三、扩展功能

3.1 PS:关于构建后操作,例如构建后发消息给QQ邮箱参考:

3.2 需要修改jenkins输出工作目录(默认是C盘的.jenkins)

四、常见报错


一、下载并安装Jenkins

Unity和Jenkins真是绝配,将打包彻底一键化! - wuzhang - 博客园 (参考资料)

Jenkins download and deployment  (官网下载地址)

二、使用Jenkins

(1)登录Jenkins

http://localhost:8080/login?from=%2F

(2)使用Jenkins构建第一个任务:打包Android、Window应用程序。

参考文章:Jenkins基于unity的自动化打包_很帅的友人的博客-CSDN博客_jenkins unity打包

注意:上方2个参数Platform是选项,BuildPath是String,添加参数的时候可以选类型的,如下图。

在Jenkins中用'$'符号+参数名称来获取其值,如上方BuildPath的默认值为:

E:\jenkins\workspace\第一个Jenkins任务\$Platform\MatchGame ,其中有$Platform ,它是获取了Platform参数的值,在下面你会看到打包路径会有2个文件夹分别是:Android和Window64,关于IOS为什么不打包因为我是Window系统电脑。

2022年7月24日更新补充 :
若没有找到Invoke Unity3d Editor选项,则需要下载一个Jenkins插件:Unity3d

 

如果没有搜索出来,可将unity3d改Unity3d,这个搜索有时候会失灵。

问题二:为什么没有Unity选项?需要全到Jenkins全局配置里设置Unity目录才行。

-quit -batchmode -projectPath D:\下载文件\游戏蛮牛源码\一个消消乐工程 -executeMethod UnityProjectBuilder.CommandLineBuild -logFile JenkinsBuildUnity.log Platform-$Platform BuildPath-$BuildPath

下面解释上方那一串关键的字符串:

D:\下载文件\游戏蛮牛源码\一个消消乐工程 是工程根目录,要改成你要打包的工程根目录

UnityProjectBuilder.CommandLineBuild 是存放于Editor文件夹的一个脚本上的静态方法,如下C#脚本:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

public class UnityProjectBuilder
{

    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();
    }

    /// <summary>
        /// 此方法是从jienkins上接受  数据的 方法
        /// </summary>
    static void CommandLineBuild()
    {
        try
        {

            Debug.Log("Command line build\n------------------\n------------------");
            string[] scenes = GetBuildScenes();
            //string path = @"E:\Unity游戏包\Android\消消乐游戏";//这里的路径是打包的路径, 定义
            string path = GetJenkinsParameter("BuildPath");
            Debug.Log(path);
            for (int i = 0; i < scenes.Length; ++i)
            {
                Debug.Log(string.Format("Scene[{0}]: \"{1}\"", i, scenes[i]));
            }
            // ProjectPackageEditor.BuildByJenkins(GetJenkinsParameter("Platform"), GetJenkinsParameter("AppID"), GetJenkinsParameter("Version"), GetJenkinsParameter("IPAddress"));
            Debug.Log("Starting Build!");
            Debug.Log(GetJenkinsParameter("Platform"));

            string platform = GetJenkinsParameter("Platform");
            if (platform == "Android")
            {
                BuildPipeline.BuildPlayer(scenes, path + ".apk", BuildTarget.Android, BuildOptions.None);
            }
            else if (platform == "IOS")
            {
                //BuildPipeline.BuildPlayer(scenes, path, BuildTarget.iOS, BuildOptions.AcceptExternalModificationsToPlayer);
            }
            else if(platform == "Window64")
            {
                BuildPipeline.BuildPlayer(scenes, path + ".exe", BuildTarget.StandaloneWindows64, BuildOptions.None);
            }
        }
        catch (Exception err)
        {
            Console.WriteLine("方法F中捕捉到:" + err.Message);
            throw;//重新抛出当前正在由catch块处理的异常err
        }
        finally
        {
            Debug.Log("---------->  I am copying!   <--------------");
        }
    }


    /// <summary>
    ///解释jekins 传输的参数
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    static string GetJenkinsParameter(string name)
    {
       /*下面是解析这个字符串,用空格分隔,每一个arg是空格分隔后的字符串,然后继续解析出Platform参数和BuildPath参数,
注意:$BuildPath是我们Jenkins上定义的参数,它会传入一个字符串路径
-quit -batchmode -projectPath D:\下载文件\游戏蛮牛源码\一个消消乐工程 -executeMethod UnityProjectBuilder.CommandLineBuild -logFile JenkinsBuildUnity.log Platform-$Platform BuildPath-$BuildPath
*/
        foreach (string arg in Environment.GetCommandLineArgs())
        {
            Debug.Log("arg:" + arg);
            if (arg.StartsWith(name))
            {
                return arg.Split("-"[0])[1];
            }
        }
        return null;
    }
}

JenkinsBuildUnity.log 是构建日志文件名带后缀,它会存放于当前任务(第一个Jenkins任务)文件夹下

其中,这个第一个Jenkins任务文件夹是创建任务时自动创建的,其他2个文件夹是我打包出的Android应用程序包和Window应用程序包所在文件夹。

最后构建我们的工程吧!

等等,先把需要构建的Unity关掉,不然构建会失败!

若构建失败,可以看构建日志上方说到的JenkinsBuildUnity.log

构建成功后,就会多出相应的文件夹里面就存放着应用程序包了!

三、扩展功能

3.1 PS:关于构建后操作,例如构建后发消息给QQ邮箱参考:

Unity和Jenkins真是绝配,将打包彻底一键化! - wuzhang - 博客园

需要开启QQ的SMTP,怎么开启可百度获知,因为我开通不了手机问题,所以就没有学这部分。

3.2 需要修改jenkins输出工作目录(默认是C盘的.jenkins)

 这样子它的日志文件才会输出到对应的我们构建时指定的打包地方。

修改后需要重启Jenkins,并且重新进Jenkins网址时会要求去从到新的工作目录取到密码,填入,以及新的一些插件的安装,比如Unity那个也是,全部都要重新来一遍。

四、常见报错

打包apk没看到.apk文件,即使它是成功的,查看.log日志发现
* What went wrong:
Execution failed for task ':launcher:mergeReleaseResources'.
> 8 exceptions were raised by workers:
  com.android.builder.internal.aapt.v2.Aapt2InternalException: AAPT2 aapt2-3.4.0-5326820-windows Daemon #2: Daemon startup failed
  This should not happen under normal circumstances, please file an issue if it does.
  com.android.builder.internal.aapt.v2.Aapt2InternalException: AAPT2 aapt2-3.4.0-5326820-

解决方法:全局参数配置gradle目录路径(与Unity内一致即可)

GRADLE_USER_HOME 【必须是这个名字】
D:/Unity 2019.4.0f1/Unity/Editor/Data/PlaybackEngines/AndroidPlayer/Tools/gradle

若还有报错说明,SDK和APK的路径也要配置,可以去我的电脑查看高级配置 查看路径的别名和内容,都照样填写。

除此之外还有

 

Unity 2019.4 默认使用 3.4.0 版本的gradle。

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity AssetBundle自动打包依赖包是指利用Unity引擎中的AssetBundle系统,将游戏中需要的资源文件进行打包,并自动包含其所依赖的其他资源文件。 首先,我们需要使用Unity编辑器中的AssetBundle功能,对需要打包的资源进行管理和设置。通过选择对应的资源文件,在其Inspector面板中设置AssetBundle属性,包括设置该资源所属的AssetBundle名称、变体名称以及是否包含所有依赖关系等。 在设置好所有资源的AssetBundle属性后,我们需要编写脚本来自动打包这些资源。Unity提供了相关的API来实现此功能。我们可以使用Unity的BuildPipeline类中的BuildAssetBundles方法来自动打包资源。该方法需要传入打包目标路径、打包选项等参数,然后会根据资源的AssetBundle属性自动将资源及其依赖项打包到指定路径中。 打包完成后,我们可以将生成的AssetBundle部署到游戏的服务器或者客户端中,通过代码动态加载和使用这些资源。在运行时,我们可以使用AssetBundle.LoadFromFile或者AssetBundle.LoadFromMemory等方法来加载指定的AssetBundle文件,并根据需要加载其中的资源对象,然后进行相应的操作和使用。 通过AssetBundle系统的自动打包功能,我们可以实现游戏资源的灵活管理和优化,减小游戏安装包的大小,并根据具体需求动态加载和卸载资源,提高游戏的性能和效率。同时,通过合理设置资源的AssetBundle属性,我们还可以实现资源的增量更新等功能,提升游戏的可玩性和扩展性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值