You probably landed here because you are a Unity3D developer like I am. More or less experienced with the “normal” handling of the Unity Editor. And now you want (or have to) go a step further and make Unity running on your server, e.g. for automatic regular builds with Jenkins or another continuous integration tool.
If the situation sounds familiar to you, go and read on, hopefully my experiences will help you to get your system running more quickly. If you think what the heck the first sentences were all about: Here is some cat content for you!
So, let’s see what our concrete situation was like:
- Unity3D mobile game
- Running Jenkins build server
- Windows Standalone build of the game works
(with options -batchmode -nographics -buildWindowsPlayer)
Not bad for a start, the builds ran daily and allowed us to always test the current version of our game. But only a standalone version unfortunately. We wanted more!
For a mobile game you want to test the game on a mobile device, of course. And you don’t want to have to build a player manually each time a tester/an artist/a game designer/your mum/… wants to check out the current version of your game.
Not a big problem I thought in the beginning. We had the standalone build already set up and I created a script to build a desired build target from the command line by using the -executeMethod command line parameter. The command which should give us a nice little Android player of the current version each night looked as simple as this:
"%UNITY_PATH%Unity.exe" -batchmode -nographics -projectPath "%WORKSPACE%/Source/Unity" -executeMethod FreudBot.Unity.Editor.Build.BuildPlayer.PerformBuild -buildpath="%WORKSPACE%/Build/Android/FreudBot.apk" -buildtarget=Android –quit
buildpath and buildtarget are two parameters of the
PerformBuild method of my
BuildPlayer script so it knows which target to build and where to store the output.
Well, almost nothing works out the first time you run it on the build server, and this build step was no exception. Unity complained that “Building Player from editor scripts requires Unity PRO”. And this is where the odyssey began…
Just activating the license
Not a big problem, I thought. We already purchased a Unity PRO license to enjoy the features which facilitate your professional development life (or which were left out from the free version to make you pay when you get more professional).
Our build server is really a headless one, a Virtual Server from a big hosting company. So no graphics card, that’s why we had to use the -nographics switch to run Unity from the command line. If such a switch exists, it should also be possible to activate Unity PRO without a graphics card, shouldn’t it? Particularly because you can’t start the Unity Editor and use the menu for it, as Unity complains “Fatal Error! Failed to initialize unity graphics.” when you try to start the Editor without a graphics card.
[singlepic id=23 w= h= float= center]
A web search didn’t bring up a solution, so I contacted the Unity support. There is a way to activate the license on a computer which doesn’t have internet access, but you still have to start the editor to activate it. But that’s not possible without a graphics card. I was stuck.
Trying it the other way around
Ok, if Unity needs a graphics card to be activated, maybe graphics acceleration can be enabled on our Virtual Server. This is possible in some virtual machines and I hoped it would take only a toggle check to give graphics acceleration to our system. Unfortunately it was not possible at all and upgrading to a root server with a graphics card was no option (Hey, we are just a small Indie studio!).
So our Virtual Server doesn’t have graphics acceleration and can’t get it from the server provider. But Unity insists on requiring a graphics card to start the editor to activate the license. Tricky!
The idea with the graphics acceleration for the virtual machine wasn’t far away from the actual solution though. Unity doesn’t need a graphics card, it just has to think there is one! And you can already start the editor and force it to use OpenGL. Plus there are OpenGL drivers which use a software renderer!
Mesa3D to the rescue!
Actually there are just a few in the end and really only one free open source library which seems to be still active: Mesa3D
[singlepic id=25 float=left]
It looks like it is very up-to-date (Last release 10.0.2 from 09.01.2014), so I decided to go for it. Just have to find the correct DLLs to put them on our Windows server. Ah, there it is, “Precompiled Libraries”. But this would be far too easy, wouldn’t it? Of course it would, that’s why the page only tells me “In general, precompiled Mesa libraries are not available.” Maybe the guys from Mesa3D could need a continuous integration system, too?
I won’t tell you all the details that finally got me to the two DLLs I needed (maybe in another post), it was a hard fight with command line compilers and a nasty bug in the current version of Mesa3D. But I’ll give you my well-earned prize for your own headless server:
Putting it all together
Now I had everything (in the end “everything” was just those two DLLs) to get our build server building daily Android players:
- Put the DLLs (opengl32.dll and osmesa.dll) into your Unity3D Editor folder
C:\Program Files (x86)\Unity\Editor)
- Startup Unity Editor with the command line switch -force-opengl :
"C:\Program Files (x86)\Unity\Editor\Unity.exe" -force-openglThe log should contain:
Version: OpenGL 2.1 [2.1 Mesa 10.0.2 (git-)]
[singlepic id=24 w= h= float=]
- Activate your Unity PRO license as if nothing happened.
- Run your Android build once from the Editor. You will have to set your Android SDK path which is also not possible from command line.
- Enjoy “Building Player from editor scripts” from the command line on your headless server without any graphics card!
It was a long and hard trip to get it running, but in the end it was absolutely worth it. Our build server creates daily versions of our game which we can access directly from our mobile devices, install them and play. I think we haven’t make up the lost time yet, but we will definitively during this project and can concentrate completely on the development instead of building the players.
Here are some ideas for improvements for all the companies I came across during my odyssey:
- Unity: Activating the license shouldn’t require the UI of the editor.
- Server hosting companies: Emulating graphics acceleration in virtual machines should be possible. Maybe forward the idea to the virtual machine software developers.
- Mesa3D: Jenkins is a great continuous integration tool and can be set up real quickly. This would also make sure the releases compile on every system, which version 10.0.2 didn’t without a small source code correction.
Nonetheless I am thankful for all those companies who provide great software and hardware for no or little money and allow even small studios to work professionally!
Hope this post will save you some time! If you have any questions just post a reply, I will try to answer each of them.
Last but not least: If you have an answer to the question how one can build a Unity Android player without administrator rights, please send me a mail 🙂 This was an odyssey on its own but had no happy ending so far…