DEV Community

loading...

How to force Unity to target x64 or ARM64 for macOS builds

clpsplug profile image Collapsed PLUG ・2 min read

Unity 2020.2 adds support for Apple Silicon binaries (both native & Universal 2.) This means we need to decide which CPU type to use when building our game, either by hand or by code. How do you do this by code?

TL;DR

The API for switching the CPU type is not directly exposed as of 2.1f1, but you can use this workaround:

using UnityEditor;

// All but the last argument must be as they appear
EditorUserBuildSettings.SetPlatformSettings(
    "Standalone",
    "OSXUniversal",
    "Architecture",
    "x64" // Possible values: "x64" "ARM64" "x64ARM64"
);
Enter fullscreen mode Exit fullscreen mode

Why

My project is built via Jenkins, which means the builds are kicked from the command line arguments.

Apparently, when you update to Unity 2020.2, the CPU type defaults to Universal 2 (x64 + ARM64.) However, it's often the case that you want not to use ARM64 because of the libraries you use. And it was the case for me.

How?

So I dug into my project files to find out that ${PROJECT_ROOT}/Library/EditorUserBuildSettings.asset remembers my build settings. This was bad news because this file was ignored by Git and I had three separate repository clones - one for development, the rest for building (to build dev/prod in parallel.) I needed to somehow replicate this file from the code!

I opened this asset file thinking this was a YAML file to find it being a binary file, but a large portion of it could be read as ASCII. I found this in the last of the file:

Standalone....iPhone....OSXUniversal....Architecture....x64....CreateXcodeProject....false
Enter fullscreen mode Exit fullscreen mode

(this portion contained a lot of non-printing chars, which was replaced with ..... The number of periods is not to scale.)

Unfortunately, Unity does not expose these variables as an API, but there is a method you can tap into these raw values. The linked documentation is for one of the overloads, but you also have this one:

EditorUserBuildSettings.SetPlatformSettings(
    buildTargetGroupStr, builTargetStr, settingName, settingValue
);
Enter fullscreen mode Exit fullscreen mode

As for buildTargetGroupStr etc., you need to set it as it appears in the .asset file - making the method look like the first code block.

Put that code in one of your IPreprocessBuildWithReport implementation, and you should be good to go.

Do keep in mind though, if you have this code in your project, you will not be able to specify your CPU type from the Build Settings window (it'll be ignored!)


Corrections are welcome, I wrote this late at night :P

Discussion (0)

pic
Editor guide