当前位置:   article > 正文

Unity3D -- 命令行导出Android && iOS工程_unity 命令行编译安卓 输出目录

unity 命令行编译安卓 输出目录

首先给一个官方的文档: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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

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

using UnityEditor;
/// <summary>
/// 这里是项目用命令行打包调用的代码
/// </summary>
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");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65

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

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

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/112044
推荐阅读
相关标签
  

闽ICP备14008679号