码迷,mamicode.com
首页 > 编程语言 > 详细

Jenkins自动构建Unity

时间:2019-04-21 18:48:50      阅读:770      评论:0      收藏:0      [点我收藏+]

标签:磁盘   start   pip   ret   路径   账号   getc   private   pre   

1.下载安装Jenkins

  链接:https://jenkins.io/

2.安装推荐plugins

  NOTE:安装失败的话,后面进入管理plugins的高级选项中,将更新站点设置为:http://mirror.xmission.com/jenkins/updates/current/update-center.json。

  2.1.安装Unity3d plugin。

3.创建新任务

  3.1.在Configure中新增build step,选择Unity,设置命令行参数-quit -batchmode -projectPath "E:/Project Files/Unity Project/2018.3/jenkins" -executeMethod Jenkins.AutoBuild -logFile build.log Version-$Version。

  3.2.build environment中勾选Add timestamps to the console output,为每个命令输出添加时间戳,方便后期查找。

  3.3.设置SVN版本控制

技术图片

  3.4.新增命令行参数,其中参数以$Version的形式存在

技术图片

  3.5.丢弃旧的构建,保证磁盘不会出现占满的情况。

4.添加Unity Editor脚本。

public class Jenkins : Editor
{
    private static string[] SCENES = FindEnabledEditorScenes();

    private static string APP_NAME = "Jenkins";
    private static string TARGET_DIR = "E:/Project Files/Unity Project/2018.3/jenkins/Build";
    private static string JDK_DIR = "F:/Program Files/Java/jdk1.8.0_25";
    private static string ANDROID_SDK_DIR = "F:/AndroidSDK";

    private static string[] FindEnabledEditorScenes()
    {
        List<string> EditorScenes = new List<string>();
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
        {
            if (!scene.enabled) continue;
            EditorScenes.Add(scene.path);
        }
        return EditorScenes.ToArray();
    }

    [MenuItem("Jenkins/Auto Build")]
    public static void AutoBuild()
    {
        //Jenkins使用system账号,读取注册表路径为HKEY_USER;手动打开Unity使用user账号,读取注册表路径为HKEY_CURRENT_USER;
        EditorPrefs.SetString("jdkPath", JDK_DIR);
        EditorPrefs.SetString("AndroidSdkRoot", ANDROID_SDK_DIR);

        string version = GetJenkinsParameter("Version");

        string suffix = string.Empty;
#if UNITY_STANDALONE_WIN
        suffix = ".exe";
#elif UNITY_ANDROID
        suffix = ".apk";
#elif UNITY_IOS
        suffix = ".ipa";
#endif

        string targetFilePath = TARGET_DIR + "/" + APP_NAME + "_" + version + suffix;

        GenericBuild(SCENES, targetFilePath, EditorUserBuildSettings.activeBuildTarget, BuildOptions.None);
    }

    public static void GenericBuild(string[] scenes, string target_dir, BuildTarget build_target, BuildOptions build_options)
    {
        BuildReport report = BuildPipeline.BuildPlayer(scenes, target_dir, build_target, build_options);
        Debug.Log(report);
    }

    private static string GetJenkinsParameter(string paramName)
    {
        foreach (var command in Environment.GetCommandLineArgs())
        {
            if (command.StartsWith(paramName) == true)
            {
                return command.Split(‘-‘)[1];
            }
        }
        return string.Empty;
    }
}

Jenkins自动构建Unity

标签:磁盘   start   pip   ret   路径   账号   getc   private   pre   

原文地址:https://www.cnblogs.com/smallrainf/p/10746152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!