💡

Unity CLIの使い方

2021/12/16に公開

ビルドシステムつくったりで、必要なコマンドオプションを調べることはありつつ、
どんなことできるかあんまり知らなかったなと思ったので、ざっくり調べてみた.

環境

  • PC: mac
  • OS: macOS Montery
  • Unity: 2021.1.28f1

Usage

まずはInstall Locationを確認

/Applications/Unity/2021.1.28f1/Unity.app/Contents/MacOS/Unity

help

help optionはないっぽい. GUI文化だ.
使い方はwebのdocを見るほかなさそう

version

定番のversion表示

$ /Applications/Unity/2021.1.28f1/Unity.app/Contents/MacOS/Unity -version
2021.1.28f1

license

CI組み込もうとするとたまにつかうやつ.
GUIでログイン済みならSkipでOK.

$ /Applications/Unity/Unity.app/Contents/MacOS/Unity \
  -quit \
  -batchmode \
  -serial SB-XXXX-XXXX-XXXX-XXXX-XXXX \
  -username 'JoeBloggs@example.com' \
  -password 'MyPassw0rd'

返却

$ /Applications/Unity/2021.1.28f1/Unity.app/Contents/MacOS/Unity \
  -quit \
  -batchmode \
  -returnlicense

create project

-createProject <Project> でプロジェクトが作れるっぽい
1分くらいかかる感じだった. プロジェクトの様子が見えないので、手元ではGUIで作る方が楽ちんではあるかも.
作成自動化作業とかがある場合は、便利そう!

あとは CLIとして使う時には、 -batchmode -quit 常時つける感じがよさそう.
-logfile - はLogの標準出力だった。

$ /Applications/Unity/2021.1.28f1/Unity.app/Contents/MacOS/Unity \
  -batchmode \
  -quit \
  -createProject GreatSampleUnity \
  -logfile -
...
[Performance] Application.Shutdown.Virtualization_Shutdown                  :        1 samples, Peak.  3.37 us (1.0x), Avg.  3.37 us, Total. 3.375 us (0.0%)
Exiting batchmode successfully now!
[Package Manager] Server::Kill -- Server was shutdown

.sln も作られるのは地味にありがたい. Scenesフォルダは作られないみたいだった

$ ls -lha GreatSampleUnity
total 8
drwxr-xr-x  10 mattak  staff   320B 12 15 23:37 .
drwxr-xr-x  14 mattak  staff   448B 12 15 23:37 ..
drwxr-xr-x   2 mattak  staff    64B 12 15 23:29 Assets
-rw-r--r--   1 mattak  staff   388B 12 15 23:30 GreatSampleUnity.sln
drwxr-xr-x  33 mattak  staff   1.0K 12 15 23:39 Library
drwxr-xr-x   5 mattak  staff   160B 12 15 23:38 Logs
drwxr-xr-x   4 mattak  staff   128B 12 15 23:29 Packages
drwxr-xr-x  21 mattak  staff   672B 12 15 23:38 ProjectSettings
drwxr-xr-x   5 mattak  staff   160B 12 15 23:40 Temp
drwxr-xr-x   3 mattak  staff    96B 12 15 23:29 UserSettings
$ ls -lha GreatSampleUnity/Assets
total 0
drwxr-xr-x   2 mattak  staff    64B 12 15 23:43 .
drwxr-xr-x  10 mattak  staff   320B 12 15 23:37 ..

build

まずビルドスクリプトを書く (雑)

namespace Sample
{
    public static class Builder
    {
        public static void Build()
        {
            var scenes = EditorBuildSettings.scenes.Select(x => x.path).ToArray();
            var options = new BuildPlayerOptions
            {
                target = BuildTarget.WebGL,
                targetGroup = BuildTargetGroup.WebGL,
                locationPathName = "Build",
                scenes = scenes,
            };

            if (!Directory.Exists("Build"))
            {
                Directory.CreateDirectory("Build");
            }

            BuildPipeline.BuildPlayer(options);
        }
    }
}

あとは -executeMethod <NamespaceName.ClassName.MethodName> で行ける.
-projectPath での指定も忘れずに

$ /Applications/Unity/2021.1.28f1/Unity.app/Contents/MacOS/Unity \
  -quit \
  -batchmode \
  -projectPath . \
  -executeMethod Sample.Builder.Build


Aborting batchmode due to fatal error:
It looks like another Unity instance is running with this project open.

Multiple Unity instances cannot open the same project.

Project: /path/to/GreatSampleUnity


zsh: abort      /Applications/Unity/2021.1.28f1/Unity.app/Contents/MacOS/Unity -quit   .

すでにlocalでUnityのGUIモードを起動中だと↑のようなエラーになるので注意.

$ /Applications/Unity/2021.1.28f1/Unity.app/Contents/MacOS/Unity \
  -quit \
  -batchmode \
  -projectPath . \
  -logfile - \
  -executeMethod Sample.Builder.Build

logfileは付けておかないと動いてるのかどうか怪しくなってprocess killしたくなるので付けておくのが吉.

ちゃんと出力されている.

$ ls -lha Build
total 16
drwxr-xr-x   5 mattak  staff   160B 12 16 00:00 .
drwxr-xr-x  14 mattak  staff   448B 12 16 00:00 ..
drwxr-xr-x   6 mattak  staff   192B 12 16 00:00 Build
drwxr-xr-x  12 mattak  staff   384B 12 16 00:00 TemplateData
-rw-r--r--   1 mattak  staff   4.8K 12 16 00:00 index.html

test

-runTests, -testResults -testPlatform あたりがそのオプションだった.

$ /Applications/Unity/2021.1.28f1/Unity.app/Contents/MacOS/Unity \
  -batchmode \
  -projectPath . \
  -logfile - \
  -runTests \
  -testResults ./results.xml \
  -testPlatform EditMode

exit codeが成功時は0/失敗時はそれ以外になるようだけど、unity cliはたまにバグって信用ならないので、xmlも見るのが吉.

成功

$ cat results.xml
<?xml version="1.0" encoding="utf-8"?>
<test-run id="2" testcasecount="0" result="Passed" total="0" passed="0" failed="0" inconclusive="0" skipped="0" asserts="0" engine-version="3.5.0.0" clr-version="4.0.30319.42000" start-time="0001-01-01 00:00:00Z" end-time="0001-01-01 00:00:00Z" duration="0.003">
  <test-suite type="TestSuite" id="1000" name="GreatSampleUnity" fullname="GreatSampleUnity" runstate="Runnable" testcasecount="0" result="Passed" start-time="2021-12-15 15:17:46Z" end-time="2021-12-15 15:17:46Z" duration="0.003000" total="0" passed="0" failed="0" inconclusive="0" skipped="0" asserts="0"><properties></properties></test-suite>
</test-run>

失敗

$ cat results.xml
<?xml version="1.0" encoding="utf-8"?>
<test-run id="2" testcasecount="1" result="Failed(Child)" total="1" passed="0" failed="1" inconclusive="0" skipped="0" asserts="0" engine-version="3.5.0.0" clr-version="4.0.30319.42000" start-time="2021-12-15 15:18:37Z" end-time="2021-12-15 15:18:37Z" duration="0.1446956">
  <test-suite type="TestSuite" id="1008" name="GreatSampleUnity" fullname="GreatSampleUnity" runstate="Runnable" testcasecount="1" result="Failed" site="Child" start-time="2021-12-15 15:18:37Z" end-time="2021-12-15 15:18:37Z" duration="0.144696" total="1" passed="0" failed="1" inconclusive="0" skipped="0" asserts="0">
    <properties />
    <failure>
      <message><![CDATA[One or more child tests had errors]]></message>
    </failure>
    <test-suite type="Assembly" id="1011" name="EditorTests.dll" fullname="/Users/mattak/github/mattak/GreatSampleUnity/Library/ScriptAssemblies/EditorTests.dll" runstate="Runnable" testcasecount="1" result="Failed" site="Child" start-time="2021-12-15 15:18:37Z" end-time="2021-12-15 15:18:37Z" duration="0.113328" total="1" passed="0" failed="1" inconclusive="0" skipped="0" asserts="0">
      <properties>
        <property name="_PID" value="34059" />
        <property name="_APPDOMAIN" value="Unity Child Domain" />
        <property name="platform" value="EditMode" />
      </properties>
      <failure>
        <message><![CDATA[One or more child tests had errors]]></message>
      </failure>
      <test-suite type="TestSuite" id="1012" name="Sample" fullname="Sample" runstate="Runnable" testcasecount="1" result="Failed" site="Child" start-time="2021-12-15 15:18:37Z" end-time="2021-12-15 15:18:37Z" duration="0.110925" total="1" passed="0" failed="1" inconclusive="0" skipped="0" asserts="0">
        <properties />
        <failure>
          <message><![CDATA[One or more child tests had errors]]></message>
        </failure>
        <test-suite type="TestFixture" id="1009" name="SampleTest" fullname="Sample.SampleTest" classname="Sample.SampleTest" runstate="Runnable" testcasecount="1" result="Failed" site="Child" start-time="2021-12-15 15:18:37Z" end-time="2021-12-15 15:18:37Z" duration="0.104491" total="1" passed="0" failed="1" inconclusive="0" skipped="0" asserts="0">
          <properties />
          <failure>
            <message><![CDATA[One or more child tests had errors]]></message>
          </failure>
          <test-case id="1010" name="RunTest" fullname="Sample.SampleTest.RunTest" methodname="RunTest" classname="Sample.SampleTest" runstate="Runnable" seed="1089166866" result="Failed" start-time="2021-12-15 15:18:37Z" end-time="2021-12-15 15:18:37Z" duration="0.067252" asserts="0">
            <properties />
            <failure>
              <message><![CDATA[  1+1=2
  Expected: 2
  But was:  3
]]></message>
              <stack-trace><![CDATA[at Sample.SampleTest.RunTest () [0x00000] in /Users/mattak/github/mattak/GreatSampleUnity/Assets/EditorTests/SampleTest.cs:13
]]></stack-trace>
            </failure>
          </test-case>
        </test-suite>
      </test-suite>
    </test-suite>
  </test-suite>
</test-run>

Discussion