How to build Unity WebGl via command line specifying Scene name?

Viewed 1248

I've a WebGl Unity application with some scenes. I need to do a build for each scene, suppose SceneA, SceneB, SceneC...

So, to do build I wrote:

"C:\Program Files\Unity\Editor\Unity.exe" -buildTarget WebGL ... SceneName ?

Questions are:

  • How to specify SceneName ?
  • And how to specify destination directory ?

Thanks

2 Answers

Are you asking how to specify which scenes are used when making a build through the command line? If so, here's how I would go about it.

For your first question,

How to specify SceneName ?

I am unaware if you can directly specify scene names in the command line, but you can run specific functions using the command line that can change which scenes are built for the project.

Here is a snippet you would need but can change for your use case

using UnityEditor;
class MyEditorScript
{
     static void PerformBuild ()
     {
         string[] scenes = { "Assets/MyScene.unity" };
         BuildPipeline.BuildPlayer(scenes, ...);
     }
}

The above snippet is quite simple, you specify each scene directly then add it to your build settings. You can pass in parameters to get different results or make multiple functions, whichever is easiest for you. Here is how you would call this function in the command line

"C:\Program Files\Unity\Editor\Unity.exe" -batchmode -executeMethod MyEditorScript.PerformBuild

I use -batchmode here so if any prompts are brought up, Unity will silence them so the process can be much more automated.

As for your second question,

And how to specify destination directory ?

I am not sure if you want to tie the directory where you save and the scenes you build with, but it might make everything easier. There is a build setting called BuildPlayerOptions.locationPathName, which allows you to specify where the build is save

static void WebGLProductionBuild()
{
    // Build the player.\
    BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
    buildPlayerOptions.scenes = new[] { "Assets/Scene1.unity", "Assets/Scene2.unity" };
    buildPlayerOptions.locationPathName = "yourLocationNameHere";
    buildPlayerOptions.target = BuildTarget.WebGL;
    buildPlayerOptions.options = BuildOptions.None; // set whatever you want here
    BuildPipeline.BuildPlayer(buildPlayerOptions);  // apply the setting changes
}

To call the above method is exactly the same except for the function call as the above line

"C:\Program Files\Unity\Editor\Unity.exe" -batchmode -executeMethod MyEditorScript.WebGLProductionBuild

I do not believe what you want to achieve can completely be done from command line, but this answer is pretty close. I am also not exactly sure if you wanted the scenes and the new path bundled together. If you are looking to build each individual scene and specify a new path, you can do so by making a new function with a switch statement or just make multiple functions to call.

Unity already provides answer to a similar question here.

You can create a method to make build validations, change settings and build different players. These methods have to be inside a script file located in an Editor folder, for example Assets/Editor/Builders/Builder.cs. The class also doesn’t need to extend any Unity class - the only requirement is that static functions are used.

using UnityEditor;
using System;

class Builder
{
    static void Build ()
    {
        string[] arguments = Environment.GetCommandLineArgs();
        // ... your code here, validations, flag changes, etc.
        // foreach(string arg in arguments)
        // Filter...
        
        string sceneName = argument[1];
        string destinationDirectoryPath = arguments[2];
        

        // Build the player.\
        BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
        buildPlayerOptions.scenes = new[] { sceneName };
        buildPlayerOptions.locationPathName = destinationDirectoryPath;
        //buildPlayerOptions.target = "Your Target";
        BuildPipeline.BuildPlayer(buildPlayerOptions);

    }
}

You can then call the builder function using these command:

  • /Path/To/Unity -batchmode -executeMethod Builder.Build SceneName DestinationDirectoryPath
Related