1
\$\begingroup\$

I'd like to build and package all the ports of my Unity game through a scripting system. Linux and Windows work fine through the Unity command line options, but there is none for WebGL.

For WebGL, the only existing solution seems to be its BuildPipeline. There is a gist with some sample code that has been referenced quit a few times. I changed its content to look like this:

BuildPipeline.BuildPlayer(scenes, "./WebGL-Test", BuildTarget.WebGL, BuildOptions.None);

and call it with unity-editor-beta -executeMethod WebGLBuilder.build (Btw, is there a way to call this method from within Unity? It doesn't help I have to restart Unity each time I want to test if the script works). It will fail with the following error:

Assets/Scripts/WebGLBuilder.cs(18,3): error CS0103: The name `BuildPipeline' does not exist in the current context
Error building Player because scripts had compiler errors

I also tried to use the BuildPlayerOptions class instead but no success. How do I get WebGL to build automatically?

\$\endgroup\$
2
\$\begingroup\$

According to an answer on Unity Answers, you need to put the scripts used for building into a folder named Editor (specifically, ./Assets/Scripts/Editor). Here's a full working build script:

using UnityEditor;
using UnityEngine;

class BuildSrcipt
{
    static string[] scenes = { "Assets/Scenes/MyScene.unity" };
    static string name = "MyGame";

    [MenuItem("Build/Build WebGL")]
    static void BuildWebGL()
    {
        BuildPipeline.BuildPlayer(scenes, "./" + name + "_Web/" + name, BuildTarget.WebGL, BuildOptions.None);
    }

    [MenuItem("Build/Build Windows")]
    static void BuildWindows()
    {
        BuildPipeline.BuildPlayer(scenes, "./" + name + "_Windows/" + name, BuildTarget.StandaloneWindows64, BuildOptions.None);
    }

    [MenuItem("Build/Build Linux")]
    static void BuildLinux()
    {
        BuildPipeline.BuildPlayer(scenes, "./" + name + "_Linux/" + name, BuildTarget.StandaloneLinux64, BuildOptions.None);
    }

    [MenuItem("Build/Build All")]
    static void BuildAll()
    {
        BuildLinux();
        BuildWindows();
        BuildWebGL();
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.