Automated Unity WebGL Deployment

I have an old Unity project that I haven’t looked at in awhile - this one. If memory serves; assembling a WebGL version is slow and boring - so I’m making my eager little Jenkins server do it.

Really I’m a bit bored with sbt-launch malarkey and I wanted to see if I could get this rolling. Broadly the steps that I followed went like this;

  1. Install Unity (5.3.3) with WebGL modules
    • 5.3.3’s WebGL stuff has improved since I last checked
  2. Build the whole tootin thing by hand (on your workstation)
    • This never works on the first try - better to do it locally
    • A custom build script was involved but many snags later it lives!
  3. Validate WebGL player
    • file:// isn’t enough boyo; you need http:// to play your game
    • … actually ; NW.js can open the local page as if it’s a real-boy
      • … y’know, this suggests some interesting options with desktop applications and Unity
  4. Build by hand (again) on the CI server
    • I needed to reinstall Unity5.3.3 to get the trimmings I wanted
    • I also needed to make a build script BB8ild (script is below)
    • my CI server is slow so I wanted to iron out slow problems before I started picking at it here
    • still need to set it up for CI stuff
  5. Configure Jenkins to assemble and post the project
    • This was more complex than I had anticipated
    • I needed to cd around and run a variety of tasks (script is below)
    • In the end - I cloned and pushed the whole site
    • … and hoped for the best

Jenkins Task

Some paths were changed for security-via-obscurity; this won’t literally work but if run line-by-line and changed as you go you’ll get there.

cd mySpaceShips
/Applications/Unity/Unity.app/Contents/MacOS/Unity -batchmode -nographics -quit -executeMethod BB8ild.PerformBuild
cd build/
rm -dfr peter-lavalle.github.io/
hg clone git+ssh://git@github.com:peter-lavalle/peter-lavalle.github.io.git
rm -dfr peter-lavalle.github.io/assets/mySpaceShips/
mkdir -p peter-lavalle.github.io/assets/mySpaceShips/
mv ./web-gl/* ./peter-lavalle.github.io/assets/mySpaceShips
cd peter-lavalle.github.io/
hg add .
hg commit -m "$BUILD_TAG"
hg push
cd ..
rm -dfr peter-lavalle.github.io/
rm -dfr web-gl/
cd ..
rm -dfr build/
rm -dfr Library/

Unity BB8ild build script

This’ll work as-is

using UnityEditor;

class BB8ild
{
	[MenuItem("Build/Build WebGL")]
	static void PerformBuild()
	{
		var sceneSettings = EditorBuildSettings.scenes;
		string[] scenePaths = new string[sceneSettings.Length];

		for (int i = 0; i < scenePaths.Length; ++i)
		{
			scenePaths[i] = sceneSettings[i].path;
		}
		BuildPipeline.BuildPlayer(scenePaths, "build/web-gl", BuildTarget.WebGL, BuildOptions.None);
	}
}
comments powered by Disqus
Peter LaValle avatar
Peter LaValle
Any links probably include affiliate ids for that sweet sweet kickback - and some programs require that I tell you. The contents of this blog are likely unrelated - as they include games, paints, and build tools.