Unity3D -- 命令行导出Android && iOS工程

首先给一个官方的文档:Command line arguments
具体的参数请去官网文档查看
这里直接来Python代码,里面有一些绝对路径,要麻烦自己替换成自己打包机子的路径

_unityPath:需要打包的unity版本的路径
_projectPath:需要打包的Unity工程
PlatformBuild.BuildIOS:Editor中的打包代码

下面代码是传入版本号和构建号来打包

#! /usr/bin/python3
# -*- coding: utf-8 -*-

import os
import argparse

def exportXcode(version, build):
    _unityPath = "/Applications/Unity/Hub/Editor/2019.3.15f1/Unity.app/Contents/MacOS/Unity"
    _projectPath = "../../../../BountyHunter_Unity"
    _platform = "iOS"

    _exeStr = _unityPath + " -quit -batchmode -projectPath " + _projectPath + " -executeMethod PlatformBuild.BuildIOS " + version + " " + build + " -buildTarget " + _platform + " -logFile /Users/dengly/Documents/Code.indie/BountyHunter/client/BountyHunter_Unity/projects/ios/log.txt"

    print("begin build iOS")
    os.system(_exeStr);
    print("end build iOS")

if __name__=='__main__':
    parser = argparse.ArgumentParser(description='## 自动化打包工具##', add_help=True)
    parser.add_argument('-v', '--version',
                        dest='v', action='store',
                        required=True,
                        help='发布包的版本号')
    
    parser.add_argument('-b', '--build',
                        dest='b', action='store',
                        required=True,
                        help='发布包的build号')
    args = parser.parse_args()
    exportXcode(args.v, args.b)

下面是打包Android和iOS的打包代码,keystore和ApplicationIdentifier的设置请根据自己的项目来,version和build是上面Python脚本中传入的version和build
PlatformBuild.BuildIOS代码如下:

using UnityEditor;
/// 
/// 这里是项目用命令行打包调用的代码
/// 
public class PlatformBuild
{
    public static void BuildAndroid()
    {
        PlayerSettings.Android.keystoreName = "";
        PlayerSettings.Android.keystorePass = "";
        PlayerSettings.Android.keyaliasName = "";
        PlayerSettings.Android.keyaliasPass = "";
        string[] strs = System.Environment.GetCommandLineArgs();
        string version = strs[7].ToString();
        string build = strs[8].ToString();
        PlayerSettings.bundleVersion = version;
        PlayerSettings.iOS.buildNumber = build;
        PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.Android, "");

        BuildUnityPlayer_Android();
    }

    public static void BuildIOS()
    {
        string[] strs = System.Environment.GetCommandLineArgs();
        string version = strs[7].ToString();
        string build = strs[8].ToString();
        PlayerSettings.bundleVersion = version;
        PlayerSettings.iOS.buildNumber = build;
        PlayerSettings.SetApplicationIdentifier(BuildTargetGroup.iOS, "");
        BuildUnityPlayer_IOS();
    } 

	public static void BuildUnityPlayer_Android()
    {
        Postprocessor.importHePostProcess = true;
        BuildAndroidPlayer();
    }
    
    public static void BuildUnityPlayer_IOS()
    {
        Postprocessor.importHePostProcess = true;
        BuildIOSPlayer();
    }
    
	private static void BuildIOSPlayer()
    {
#if UNITY_EDITOR_OSX
        BuildTarget buildTarget = BuildTarget.iOS;
        string projectPath = FileUtility.PathCombine(GetProjectPath(buildTarget), PlayerSettings.productName);

        BuildPipeline.BuildPlayer(EditorUtils.GetBuildScenes().ToArray(), projectPath, BuildTarget.iOS, EditorUtils.GetBuildOptions());
        UnityEngine.Debug.Log(" ios BuildPlayer");
#endif
    }

    private static void BuildAndroidPlayer()
    {
        BuildTarget buildTarget = BuildTarget.Android;
        string projectPath = GetProjectPath(buildTarget);

        BuildPipeline.BuildPlayer(EditorUtils.GetBuildScenes().ToArray(), projectPath, BuildTarget.Android, EditorUtils.GetBuildOptions());
        UnityEngine.Debug.Log(" android BuildPlayer");
    }
}

BuildIOSPlayer和BuildAndroidPlayer就是具体的打包代码了,这个在其他地方可以很方便的获取怎么做的,这里就不把全部代码贴出来了,上面部分我相信你已经知道怎么处理的了。

提示:可千万别直接贴代码哦,因为PlatformBuild的代码跟项目相关,要自己设置的哦

你可能感兴趣的:(Unity3D)