【狂云歌之unity_vr】unity项目持续集成dailybuild以及多平台打包管理-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

【狂云歌之unity_vr】unity项目持续集成dailybuild以及多平台打包管理

简介: # 【狂云歌之unity_vr】unity项目持续集成dailybuild以及多平台打包管理 ![unityvr](http://upload-images.jianshu.io/upload_images/2990112-ccf04f1f24821870.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ## 前言 &e

【狂云歌之unity_vr】unity项目持续集成dailybuild以及多平台打包管理

unityvr

前言

 持续集成的意义就不多说了。unity通常打包一般就直接build&run,但是在实际项目中,往往直接在服务器build包,所以命令行打包必不可少,这里一方面分享unity打包做持续集成,一方面分享使用unity管理多平台打包,例如一个vrapp需要支持gear版本,支持小米版本,支持cardboard版本等等~懂的人就知道这里具有一定的管理维护成本。

 我们做vr相关的app,需要支持gear、cardboard、小米、vivo、大朋、暴风、Idealens、pico、nibiru、酷开等一大堆平台,曾经还有lg和htcvive、oculus平台,未来还会有更多的平台,所以关于unity项目的多平台管理是很重要的,在这方面我们也在探索,积累了一点经验。这里介绍的主要是基于unity中c#写的打包和多平台管理,如果将其中一部分功能使用python和其他配置文件来实现也是可以的,只是用c#直接做会方便许多。

jenkins

jenkins

https://jenkins.io/index.html

 jenkins不用多说,懂的人都了解是干什么的,来源于hudson,可以比较容易的搭起一个持续集成服务器,支持svn和git等版本管理。支持bash,所以可以用bash、python等大部分脚本来写打包脚本和前后的处理。

unity命令行打包

 unity如何进行命令行打包呢,其实unity是支持以命令行方式启动的,但是需要关闭editor支持执行命令,如下:

${unity可执行文件路径} -projectPath ${项目路径} 
-executeMethod CloudBuild.PerformBuildAndroidCloudAlphaRelease 
-batchmode -quit -logFile ${放log的路径} 
-ForceExitEditor

 整体比较容易理解,其中CloudBuild.PerformBuildAndroidCloudAlphaRelease是一个类的静态方法,然后在这个方法中写打包相关逻辑即可。

EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
string[] scenes = { "Assets/Scenes/Init.unity", "Assets/Scenes/Main.unity" };
BuildPipeline.BuildPlayer(scenes, buildpath, BuildTarget.Android, BuildOptions.None);

 核心逻辑就这么多,就会开始打一个android包并且生成到buildpath下面

多平台打包管理

准备工作

 对unity插件不熟悉的可以看下 开发unity插件——一次搞定unity编辑器常用功能

 我这里准备了一个全局的配置文件,当然这个可以使用外部配置文件来管理配置,是一个道理的。这里定义了两个平台版本一个是alpha一个是beta,使用宏来区分不同平台的版本号。

using UnityEngine;
using System.Collections;

public class GlobalConfig {
#if CLOUD_ALPHA
    public const int ClientVersionCode = 1;
    public const string ClientVersion = "1.0";
#elif CLOUD_BETA
    public const int ClientVersionCode = 2;
    public const string ClientVersion = "1.1";
#endif
}

 文件目录组织大概如下,其中CloudBuild是编辑器工具,包含菜单项和打包功能,两个平台分别依赖不同的so文件和manifest文件。

dir

 制作了一个menu,主要包含的功能是可以在alpha和beta平台之间切换,可以打alpha平台的apk包,当然想打beta平台的包只需要简单修改。

menu

manifest管理

 写个简单的脚本进行manifest替换就好,对于alpha平台和beta平台各有自己的manifest文件,在切换平台的时候将对应的manifest复制替换。

/// <summary>
///  使用相应的androidmanifest
/// </summary>
static void UseAndroidManifest(string filename)
{
    string src_filename = string.Format("AndroidManifest-{0}.xml", filename);
    string dst_filename = "AndroidManifest.xml";
    string path = Application.dataPath + "/Plugins/Android/";
    File.Copy(path + src_filename, path + dst_filename, true);
    AssetDatabase.Refresh();//因为修改了manifest文件,所以刷新unity的assets
}

依赖包管理

 为什么要做依赖包管理呢?因为在使用不同平台sdk的时候,可能会引入很多sdk,每个sdk里包含自己的so、jar、aar包等,如果什么都不管理,直接打包的话,那么这些依赖的文件都会打进所有的apk包,简单来说就会增加包的体积,更严重的情况下,这些不同平台sdk里的依赖库可能还会有冲突,如果打进同一个apk包,后果不堪设想~

 做依赖包管理主要依赖unity自己的assetimport管理如下图,那么只要在需要的时候勾选不需要的时候取消勾选就好了,我们要做的就是用代码来自动实现这个功能。

assetsimport

 先准备好各个平台的依赖包路径

static string[] Plugins_Alpha = new string[] {
    "Assets/Plugins/Android/libs/armeabi-v7a/alpha.so",
};

static string[] Plugins_Beta = new string[] {
    "Assets/Plugins/Android/libs/armeabi-v7a/beta.so",
};

 然后在切换不同平台的时候对这些依赖包的import做处理,这块Asset属于plugin,所以使用pluginimporter来管理勾选的问题。

static void ChangePluginToAlpha()
{
    SetEnablePluginImport(Plugins_Alpha, true);
    SetEnablePluginImport(Plugins_Beta, false);
}

static void ChangePluginToBeta()
{
    SetEnablePluginImport(Plugins_Alpha, false);
    SetEnablePluginImport(Plugins_Beta, true);
}

static void SetEnablePluginImport(string[] plugins, bool enable = true)
{
    foreach(var path in plugins)
    {
        PluginImporter vrlib = AssetImporter.GetAtPath(path) as PluginImporter;
        vrlib.SetCompatibleWithPlatform(BuildTarget.Android, enable);
    }
}

 非常简单一看就可以懂,然后试一下就明白了。

平台切换

 平台切换功能主要是在editor里调试各个平台功能的时候使用的菜单项,功能也很简单,就做了下面几件事情

  • 切平台和宏定义
  • 切playersetting参数
  • 切buildscene配置
  • 切manifest和依赖包
  • 保存及打开对应平台的场景(如果场景不是复用的)

 代码示例如下,因为我们做vr相关的app,所以在gear平台时vrsupport为true,其他平台时为false,如果不同平台的scene不一样,那么在最后问用户是否保存当前场景,然后打开对应平台的场景。

/// <summary>
/// 切换alpha平台
/// </summary>
[UnityEditor.MenuItem("CloudBuild/SwitchToAlpha", priority = 50)]
static void SwitchToAlpha()
{
    PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, DEFINES_ALPHA);
    PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS, DEFINES_ALPHA);
    PlayerSettings.virtualRealitySupported = true;
    EditorBuildSettings.scenes = new EditorBuildSettingsScene[] {
        new EditorBuildSettingsScene("Assets/Scenes/Init.unity", true),
        new EditorBuildSettingsScene("Assets/Scenes/Main.unity", true)
    };//场景
    UseAndroidManifest("Alpha");
    ChangePluginToAlpha();
    EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
    EditorSceneManager.OpenScene("Assets/Scenes/Main.unity");
}

打包

 那么最后打包脚本如下,粗看信息量可能比较大,实际只做了几件事情

  • 准备好要打包的scene
  • 将当前editor的状态保存一下,以便打完包恢复,这是为了开发使用方便而已,否则在开发机上打个包就发现editor的很多属性变了有时很尴尬
  • 处理android的签名问题
  • 打包
  • 最后如果是windows,一般是开发机,直接打开build好的apk所在文件夹,方便使用
/// <summary>
/// </summary>
[UnityEditor.MenuItem("CloudBuild/CloudAlpha-Release")]
static void PerformBuildAndroidCloudAlphaRelease()
{
    EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
    string[] scenes = { "Assets/Scenes/Init.unity", "Assets/Scenes/Main.unity" };
    string path = GetBuildPathAndroid();
    if (scenes == null || scenes.Length == 0 || path == null)
    {
        Debug.LogError("error scene is null");
        return;
    }
    string tempid = PlayerSettings.bundleIdentifier;
    string name = PlayerSettings.productName;
    bool virtualRealitySupported = PlayerSettings.virtualRealitySupported;
    PlayerSettings.virtualRealitySupported = true;
    PlayerSettings.bundleIdentifier = "net.itsong.vralpha";
    PlayerSettings.productName = PRODUCT_NAME;
    PlayerSettings.Android.keystoreName = "";
    PlayerSettings.Android.keyaliasName = "";
    PlayerSettings.Android.keyaliasPass = "";
    PlayerSettings.Android.keystorePass = "";
    PlayerSettings.Android.bundleVersionCode = GlobalConfig.ClientVersionCode;
    PlayerSettings.bundleVersion = GlobalConfig.ClientVersion;
    string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
    UseAndroidManifest("Alpha");
    ChangePluginToAlpha();
    PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, DEFINES_ALPHA + DEFINES_RELEASE);
    string buildpath = path + string.Format("cloudvr_alpha_release_{0}.apk", DateTime.Now.ToString("MMddHHmm", DateTimeFormatInfo.InvariantInfo));
    BuildPipeline.BuildPlayer(scenes, buildpath, BuildTarget.Android, BuildOptions.None);
    PlayerSettings.virtualRealitySupported = virtualRealitySupported;
    PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, defines);
    PlayerSettings.bundleIdentifier = tempid;
    PlayerSettings.productName = name;
    PlayerSettings.Android.keystoreName = "";
    PlayerSettings.Android.keyaliasName = "";
    PlayerSettings.Android.keyaliasPass = "";
    PlayerSettings.Android.keystorePass = "";
    string dir = path.Replace('/', '\\');
#if UNITY_EDITOR_WIN
    System.Diagnostics.Process.Start("explorer.exe", "\"" + dir + "\"");
#endif
}

 完整的CloudBuild文件如下:

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
using System;
using System.Globalization;
using UnityEditor.SceneManagement;
/// <summary>
/// </summary>
partial class CloudBuild
{
    const string PRODUCT_NAME = "狂云歌VR";
    const string DEFINES_ALPHA  = "CROSS_PLATFORM_INPUT;MOBILE_INPUT;CLOUD_ALPHA;";
    const string DEFINES_BETA = "CROSS_PLATFORM_INPUT;MOBILE_INPUT;CLOUD_BETA;";
    const string DEFINES_RELEASE = "CLOUD_RELEASE";

    static string[] Plugins_Alpha = new string[] {
        "Assets/Plugins/Android/libs/armeabi-v7a/alpha.so",
    };

    static string[] Plugins_Beta = new string[] {
        "Assets/Plugins/Android/libs/armeabi-v7a/beta.so",
    };

    // Build the Android APK and place into main project folder
    static string GetBuildPathAndroid()
    {
        string dirPath = Application.dataPath + "/../build/android/";
        if (!System.IO.Directory.Exists(dirPath))
        {
            System.IO.Directory.CreateDirectory(dirPath);
        }
        return dirPath;
    }

    /// <summary>
    /// </summary>
    [UnityEditor.MenuItem("CloudBuild/CloudAlpha-Release")]
    static void PerformBuildAndroidCloudAlphaRelease()
    {
        EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
        string[] scenes = { "Assets/Scenes/Init.unity", "Assets/Scenes/Main.unity" };
        string path = GetBuildPathAndroid();
        if (scenes == null || scenes.Length == 0 || path == null)
        {
            Debug.LogError("error scene is null");
            return;
        }
        string tempid = PlayerSettings.bundleIdentifier;
        string name = PlayerSettings.productName;
        bool virtualRealitySupported = PlayerSettings.virtualRealitySupported;
        PlayerSettings.virtualRealitySupported = true;
        PlayerSettings.bundleIdentifier = "net.itsong.vralpha";
        PlayerSettings.productName = PRODUCT_NAME;
        PlayerSettings.Android.keystoreName = "";
        PlayerSettings.Android.keyaliasName = "";
        PlayerSettings.Android.keyaliasPass = "";
        PlayerSettings.Android.keystorePass = "";
        PlayerSettings.Android.bundleVersionCode = GlobalConfig.ClientVersionCode;
        PlayerSettings.bundleVersion = GlobalConfig.ClientVersion;
        string defines = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android);
        UseAndroidManifest("Alpha");
        ChangePluginToAlpha();
        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, DEFINES_ALPHA + DEFINES_RELEASE);
        string buildpath = path + string.Format("cloudvr_alpha_release_{0}.apk", DateTime.Now.ToString("MMddHHmm", DateTimeFormatInfo.InvariantInfo));
        BuildPipeline.BuildPlayer(scenes, buildpath, BuildTarget.Android, BuildOptions.None);
        PlayerSettings.virtualRealitySupported = virtualRealitySupported;
        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, defines);
        PlayerSettings.bundleIdentifier = tempid;
        PlayerSettings.productName = name;
        PlayerSettings.Android.keystoreName = "";
        PlayerSettings.Android.keyaliasName = "";
        PlayerSettings.Android.keyaliasPass = "";
        PlayerSettings.Android.keystorePass = "";
        string dir = path.Replace('/', '\\');
#if UNITY_EDITOR_WIN
        System.Diagnostics.Process.Start("explorer.exe", "\"" + dir + "\"");
#endif
    }

    /// <summary>
    /// 切换alpha平台
    /// </summary>
    [UnityEditor.MenuItem("CloudBuild/SwitchToAlpha", priority = 50)]
    static void SwitchToAlpha()
    {
        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, DEFINES_ALPHA);
        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS, DEFINES_ALPHA);
        PlayerSettings.virtualRealitySupported = true;
        EditorBuildSettings.scenes = new EditorBuildSettingsScene[] {
            new EditorBuildSettingsScene("Assets/Scenes/Init.unity", true),
            new EditorBuildSettingsScene("Assets/Scenes/Main.unity", true)
        };//场景
        UseAndroidManifest("Alpha");
        ChangePluginToAlpha();
        EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
        EditorSceneManager.OpenScene("Assets/Scenes/Main.unity");
    }

    /// <summary>
    /// 切换beta平台
    /// </summary>
    [UnityEditor.MenuItem("CloudBuild/SwitchToBeta", priority = 50)]
    static void SwitchToBeta()
    {
        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.Android, DEFINES_BETA);
        PlayerSettings.SetScriptingDefineSymbolsForGroup(BuildTargetGroup.iOS, DEFINES_BETA);
        PlayerSettings.virtualRealitySupported = true;
        EditorBuildSettings.scenes = new EditorBuildSettingsScene[] {
            new EditorBuildSettingsScene("Assets/Scenes/Init.unity", true),
            new EditorBuildSettingsScene("Assets/Scenes/Main.unity", true)
        };//场景
        UseAndroidManifest("Beta");
        ChangePluginToBeta();
        EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo();
        EditorSceneManager.OpenScene("Assets/Scenes/Main.unity");
    }

    /// <summary>
    ///  使用相应的androidmanifest
    /// </summary>
    static void UseAndroidManifest(string filename)
    {
        string src_filename = string.Format("AndroidManifest-{0}.xml", filename);
        string dst_filename = "AndroidManifest.xml";
        string path = Application.dataPath + "/Plugins/Android/";
        File.Copy(path + src_filename, path + dst_filename, true);

        PlayerSettings.Android.bundleVersionCode = GlobalConfig.ClientVersionCode;
        PlayerSettings.bundleVersion = GlobalConfig.ClientVersion;
        AssetDatabase.Refresh();
    }

    static void ChangePluginToAlpha()
    {
        SetEnablePluginImport(Plugins_Alpha, true);
        SetEnablePluginImport(Plugins_Beta, false);
    }

    static void ChangePluginToBeta()
    {
        SetEnablePluginImport(Plugins_Alpha, false);
        SetEnablePluginImport(Plugins_Beta, true);
    }

    static void SetEnablePluginImport(string[] plugins, bool enable = true)
    {
        foreach(var path in plugins)
        {
            PluginImporter vrlib = AssetImporter.GetAtPath(path) as PluginImporter;
            vrlib.SetCompatibleWithPlatform(BuildTarget.Android, enable);
        }
    }
}

后续

 这里没写build ios ipa包的过程,ios的build过程会稍微长一些,要先build好xcode project然后再通过xcode的命令行去打包,所以前半部分与android是可以复用的,只要稍加修改就可以支持ios的build。另外我们现在做vr相关的app,大部分都是android版本,所以apk的管理比较实用。

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章