I am looking for a way to enable/disable the following flag via code which is normally set via Project Settings → Burst AOT Settings:
I am also open to using Reflection to change that flag.
I am aware of
BurstCompiler.Options.EnableBurstCompilation
but this seems to change the Burst compilation in the Unity Editor, which I do not want to change here.
Background to my questions is that I want to create a class implementing IPreprocessBuildWithReport which enables and disables Burst together with some other build related configuration.
If you’re willing to use reflection, you would probably be happy with using an assembly definition reference on Unity.Burst.Editor. You’d use InternalsVisibleTo to get at BurstPlatformAotSettings, and / or create a public wrapper around BurstPlatformAotSettings, if that’s more your style.
using Unity.Burst.Editor;
using UnityEditor;
namespace Exposure.Burst.Editor
{
internal static class SampleScript
{
[MenuItem("Sample/Toggle Burst Compile on Active Target (InternalsVisibleTo)")]
private static void ToggleBurstCompileOnActiveTargetInternalsVisibleTo()
{
var buildTarget = EditorUserBuildSettings.activeBuildTarget;
var settings = BurstPlatformAotSettings.GetOrCreateSettings(buildTarget);
settings.EnableBurstCompilation ^= true;
settings.Save(buildTarget);
}
[MenuItem("Sample/Toggle Burst Compile on Active Target (accessor)")]
private static void ToggleBurstCompileOnActiveTargetAccessor()
{
var buildTarget = EditorUserBuildSettings.activeBuildTarget;
var settings = ExposedBurstPlatformSettings.GetSettings(buildTarget);
settings.EnableBurstCompilation ^= true;
settings.Save();
}
}
}
#if UNITY_EDITOR
using UnityEditor;
namespace Unity.Burst.Editor
{
public static class ExposedBurstPlatformSettings
{
public static BurstPlatformAotSettingsEditContext GetSettings(BuildTarget buildTarget)
{
return new BurstPlatformAotSettingsEditContext(buildTarget, BurstPlatformAotSettings.GetOrCreateSettings(buildTarget));
}
}
public class BurstPlatformAotSettingsEditContext
{
private readonly BuildTarget _buildTarget;
private readonly BurstPlatformAotSettings _settings;
internal BurstPlatformAotSettingsEditContext(BuildTarget buildTarget, BurstPlatformAotSettings settings)
{
_buildTarget = buildTarget;
_settings = settings;
}
public void Save()
{
_settings.Save(_buildTarget);
}
public bool EnableBurstCompilation
{
get => _settings.EnableBurstCompilation;
set => _settings.EnableBurstCompilation = value;
}
public bool EnableOptimisations
{
get => _settings.EnableOptimisations;
set => _settings.EnableOptimisations = value;
}
public bool EnableDebugInAllBuilds
{
get => _settings.EnableDebugInAllBuilds;
set => _settings.EnableDebugInAllBuilds = value;
}
public bool EnableArmv9SecurityFeatures
{
get => _settings.EnableArmv9SecurityFeatures;
set => _settings.EnableArmv9SecurityFeatures = value;
}
public OptimizeFor OptimizeFor
{
get => _settings.OptimizeFor;
set => _settings.OptimizeFor = value;
}
}
}
#endif
(Snippets were written for Burst 1.8.9)
(Drop-in assemblies attached for ease of trying out)
9425477–1321190–Exposure.Burst.Editor.7z (1.62 KB)
These settings are often stored in EditorPrefs. The manual mentions the location for these. Monitor what key is changed when you toggle this flag. Then you know the key and can manually toggle the EditorPrefs value for this key anytime you want - however there is no guarantee that the flag will come into effect instantly.
That’s useful general knowledge, but it’s not applicable here, as the Burst settings mentioned here are saved to target-specific files in ProjectSettings.
@Spy-Master
Works great, thanks for sharing your solution.