- Home /
How can I "Switch Platform" from a script on Mac?
I have a cross-platform game that I want to build automatically for iOS and Android. My build machine is a Mac Mini, but I would like to be able to build on multiple machines using Jenkins. At this point, I have scripts set up that make the Android build nicely, but there is a lot of interactive preparation that I was hoping to avoid through automation.
The big hangup right now is this: whenever I have a "clean workspace", the editor seems to try and build "PC, Mac & Linux Standalone". This leads to compilation errors regarding conditionally-compiled blocks (`#if UNITY_IPHONE` and so on).
If I manually fire up Unity once in that folder and use the "Switch Platform" button from the Build Settings panel, all subsequent automated builds work just fine.
Is there any way to do the same thing as "Switch Platform" from the command line? I am using something like the AutoBuilder script from http://wiki.unity3d.com/index.php?title=AutoBuilder with "executeMethod" from the command line. The script has a line to switch target, but maybe it is not effective:
EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
BuildPipeline.BuildPlayer(scenes, path, BuildTarget.Android, BuildOptions.None);
The command line I am using is:
Unity -batchmode -quit -executeMethod Builder.Android -projectPath ./proj -logfile unity_build_log.txt
Do I need to do anything more than that?
We have the same problem, this is a real short co$$anonymous$$g of unity and should be addressed ASAP by the dev $$anonymous$$m.
I have discovered that if I transplant the entire Library directory from a manually-switched project into the new workspace, the automated build functions correctly.
The first build after the transplant takes longer, apparently due to needing to re-load assets.
I have not been able to narrow down which file or files in Library indicate to Unity what platform to use.
A colleague suggested to me that there might be compile errors when the default "Standalone" platform is selected, which would prevent Unity from even getting to the SwitchActiveBuildTarget
line.
If I have some spare time, I will test this theory, and see if some judicious use of #if UNITY_STANDALONE
solves the issue.
Answer by a.macleod · Mar 11, 2014 at 08:32 PM
I tested my colleague's theory and it seems to be correct.
In order to use a script in Assets/Editor
the game must be able to compile everything for whatever platform is currently selected. This means that if you want to do an automated build from a clean checkout, the game must compile for Standalone.
In my case, since we do not actually care about building for Standalone, this means I can take some liberties with #if UNITY_STANDALONE
to just disable sections of code that would otherwise have caused compilation errors. The compilation phase leading up to invocation of the argument passed to -executeMethod
is entirely in Editor-space, so UNITY_EDITOR
is also defined.
By combining this knowledge and the usage of smcs.rsp
as described in the Global Custom Defaults section of https://docs.unity3d.com/Documentation/Manual/PlatformDependentCompilation.html I was able to get my automated build up and running without excess human intervention.