Build Unity with Command Line – 使用命令行建置 Unity

发表于2017-03-16
评论0 4.2k浏览
  本文要给大家介绍的是如何透过命令行,执行 Unity 版本建置功能。命令行,可见度很高的软体工具之一,一直以来都存在于电脑作业系统中,在 Mac OS X、Windows 及 Linux 裡都可以找到这个软体工具。透过命令行,可以执行并组织出特定的功能,从基本的软体安装、版本控制指令到自动化版本建置、自动化专案更新及其他使用者介面所无法达到的功能。

  文章中所实作的作业系统为 Mac OS X。

终端机位置
1
2
3
4
5
6
7
8
Mac OS X
应用程式/实用工具/终端机
 
Windows
开始/所有程式/附属应用程式/命令提示字元
 
Linux
应用程式/附属应用程式/终端机
Unity 执行档路径
1
2
3
4
5
6
7
8
Mac OS X
/Applications/Unity/Unity.app/Contents/MacOS/Unity
  
Windows
"C:Program FilesUnityEditorUnity.exe"
  
Linux
/opt/Unity/Editor/Unity
Unity 命令行参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-batchmode
//在 batch mode 下执行 Unity
//需要注意,Unity 只允许同时间存在一个执行程序
  
-quit
//在命令行结束执行时,关闭 Unity Editor
//需要注意使用这个功能,会导致无法在 Unity Editor 中查看错误讯息
  
-projectPath
//Unity 专案路径
  
-logFile
//建置日志路径
  
-executeMethod
//开启 Unity 时,执行类别中的静态方法
//可利用于 CI、Unit Tests、版本建置、资料处理...等。
//要注意类别脚本需要放置在 Editor 资料夹中
客製化命令行参数
  有时候我们会需要一些客製化功能,来扩充并改善自动化建置的流程,像是版本输出路径、Android Keystore 设定、Android Keyalias 设定…等等。
  虽然 Unity 所提供的命令行参数并没有这些功能,但我们可以很方便地进行功能扩充。
这边就简单的实作如何客製化输出路径。
1
2
3
4
5
1
2
-destination
//输出路径
 

解析客製化命令行参数
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
private static string _destinationPath;
private static void CustomizedCommandLine()
{
    Dictionary<string, action<string="">> cmdActions = new Dictionary<string, action<string="">>
    {
        {
            "-destinationPath", delegate(string argument)
            {
                _destinationPath = argument;
            }
        }
    };
  
    Action<string> actionCache;
    string[] cmdArguments = Environment.GetCommandLineArgs();
  
    for (int count = 0; count < cmdArguments.Length; count++)
    {
        if (cmdActions.ContainsKey(cmdArguments[count]))
        {
            actionCache = cmdActions[cmdArguments[count]];
            actionCache(cmdArguments[count + 1]);
        }
    }
  
    if (string.IsNullOrEmpty(_destinationPath))
    {
        _destinationPath = Path.GetDirectoryName(Application.dataPath);
    }
}string>string,>string,>
完整 BuildTool
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
66
67
68
69
70
71
72
73
74
75
76
77
78
using UnityEditor;
using UnityEngine;
using System;
using System.IO;
using System.Collections.Generic;
  
public class BuildTool
{
    [MenuItem("BuildTool/Build")]
    private static void Build()
    {
        CustomizedCommandLine();
  
        string destinationPath = Path.Combine(_destinationPath, PlayerSettings.productName);
        destinationPath += GetExtension();
  
        BuildPipeline.BuildPlayer(EditorBuildSettings.scenes, destinationPath, EditorUserBuildSettings.activeBuildTarget, BuildOptions.None);
    }
  
  
    private static string _destinationPath;
    private static void CustomizedCommandLine()
    {
        Dictionary<string, action<string="">> cmdActions = new Dictionary<string, action<string="">>
        {
            {
                "-destinationPath", delegate(string argument)
                {
                    _destinationPath = argument;
                }
            }
        };
  
        Action<string> actionCache;
        string[] cmdArguments = Environment.GetCommandLineArgs();
  
        for (int count = 0; count < cmdArguments.Length; count++)
        {
            if (cmdActions.ContainsKey(cmdArguments[count]))
            {
                actionCache = cmdActions[cmdArguments[count]];
                actionCache(cmdArguments[count + 1]);
            }
        }
  
        if (string.IsNullOrEmpty(_destinationPath))
        {
            _destinationPath = Path.GetDirectoryName(Application.dataPath);
        }
    }
  
  
    private static string GetExtension()
    {
        string extension = "";
  
        switch (EditorUserBuildSettings.activeBuildTarget)
        {
            case BuildTarget.StandaloneOSXIntel:
            case BuildTarget.StandaloneOSXIntel64:
            case BuildTarget.StandaloneOSXUniversal:
                extension = ".app";
                break;
            case BuildTarget.StandaloneWindows:
            case BuildTarget.StandaloneWindows64:
                extension = ".exe";
                break;
            case BuildTarget.Android:
                extension = ".apk";
                break;
            case BuildTarget.iOS:
                extension = ".ipa";
                break;
        }
  
        return extension;
    }
}string>string,>string,>
Shell Script
build_unity_with_command_line.sh
1
2
3
4
5
6
7
8
#!/bin/bash
  
UNITY_PATH=/Applications/Unity/Unity.app/Contents/MacOS/Unity
PROJECT_PATH=/Users/ted/SideProjects/UnityCommandLineBuild
BUILD_LOG_PATH=${PROJECT_PATH}/build.log
DESTINATION_PATH=/Users/ted/Desktop/
  
$UNITY_PATH -quit -batchmode -projectPath ${PROJECT_PATH} -executeMethod BuildTool.Build -logFile ${BUILD_LOG_PATH} -destinationPath ${DESTINATION_PATH}

执行 Shell Script

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

0个评论