OrangeSkeleton
OrangeSkeleton

Reputation: 31

Export Google Android Project from Unity3D player using command line

Looking at the documentation for Unity Command Line options and Unity API reference manuals, I was not able to find a way to export the Unity project to an Android Studio Project. http://docs.unity3d.com/Manual/CommandLineArguments.html

there is an API call for build an android project BuildTarget.Android but I am looking at exporting it as an Google Android Project, which can be done manually from the build settings.

pic

Upvotes: 3

Views: 2452

Answers (1)

Alex Sorokoletov
Alex Sorokoletov

Reputation: 3101

You have to create following script file in Unity 3d, put it in the Editor subfolder of your assets.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;


public class CIEditorScript
{
    static string[] SCENES = FindEnabledEditorScenes ();

    static string APP_NAME = "LuauUnity";
    static string TARGET_DIR = "target";

    [MenuItem ("Custom/CI/Build iOS")]
    static void PerformIOSBuild ()
    {
        GenericBuild (SCENES, TARGET_DIR + "/ios/", BuildTarget.iOS, BuildOptions.None);
    }

    [MenuItem ("Custom/CI/Build Android")]
    static void PerformAndroidBuild ()
    {
        GenericBuild (SCENES, TARGET_DIR + "/android/", BuildTarget.Android, BuildOptions.AcceptExternalModificationsToPlayer);
    }

    private static string[] FindEnabledEditorScenes ()
    {
        List<string> EditorScenes = new List<string> ();
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes) {
            if (!scene.enabled)
                continue;
            EditorScenes.Add (scene.path);
        }
        return EditorScenes.ToArray ();
    }

    static void GenericBuild (string[] scenes, string target_dir, BuildTarget build_target, BuildOptions build_options)
    {
        EditorUserBuildSettings.SwitchActiveBuildTarget (build_target);
        string res = BuildPipeline.BuildPlayer (scenes, target_dir, build_target, build_options);
        if (res.Length > 0) {
            throw new Exception ("BuildPlayer failure: " + res);
        }
    }
}

Gist here: https://gist.github.com/alexsorokoletov/531738ce8e5681437f6d

After that you can run following command line (for Android)

$UNITYCMD -batchmode -nographics -silent-crashes -projectpath $PROJECTPATH -logfile $U3DLOGFILE -executeMethod CIEditorScript.PerformAndroidBuild -quit

or for iOS

$UNITYCMD -batchmode -nographics -silent-crashes -projectpath $PROJECTPATH -logfile $U3DLOGFILE -executeMethod CIEditorScript.PerformIOSBuild -quit

where:

$UNITYCMD is Unity app file or executable file,

$PROJECTPATH is path to the Unity project

$U3DLOGFILE is a path to the Unity build log file

Upvotes: 2

Related Questions