-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathScriptingBackendOverride.cs
More file actions
70 lines (62 loc) · 2.91 KB
/
Copy pathScriptingBackendOverride.cs
File metadata and controls
70 lines (62 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#nullable enable
using System;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
namespace Immutable.Audience.Samples.SampleApp.Editor
{
// Lets the test runner pick the scripting backend per-build via the
// AUDIENCE_SCRIPTING_BACKEND env var ("IL2CPP" or "Mono2x"). Unity has
// no built-in CLI flag for this, so we hook the build pre-process and
// patch PlayerSettings before the player is compiled.
//
// Stripping is also flipped to the realistic per-backend default:
// IL2CPP → High (the aggressive linker config studios ship under).
// Mono → Disabled (Mono studios rarely strip; High under Mono can
// strip Net.Http SSL chain code paths).
//
// Supported targets: Standalone, Android, iOS.
//
// Usage:
// AUDIENCE_SCRIPTING_BACKEND=Mono2x Unity -batchmode -runTests ...
// AUDIENCE_SCRIPTING_BACKEND=IL2CPP Unity -batchmode -buildTarget Android ...
// AUDIENCE_SCRIPTING_BACKEND=IL2CPP Unity -batchmode -buildTarget iOS ...
//
// Unset means "respect ProjectSettings.asset as-is".
internal sealed class ScriptingBackendOverride : IPreprocessBuildWithReport
{
private const string EnvVar = "AUDIENCE_SCRIPTING_BACKEND";
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
var requested = Environment.GetEnvironmentVariable(EnvVar);
if (string.IsNullOrEmpty(requested)) return;
ScriptingImplementation backend = requested switch
{
"IL2CPP" => ScriptingImplementation.IL2CPP,
"Mono2x" => ScriptingImplementation.Mono2x,
_ => throw new BuildFailedException(
$"{EnvVar} must be 'IL2CPP' or 'Mono2x'; got '{requested}'"),
};
var group = report.summary.platformGroup;
if (group != BuildTargetGroup.Standalone && group != BuildTargetGroup.Android && group != BuildTargetGroup.iOS)
return;
var currentBackend = PlayerSettings.GetScriptingBackend(group);
if (currentBackend != backend)
{
PlayerSettings.SetScriptingBackend(group, backend);
Debug.Log($"[{nameof(ScriptingBackendOverride)}] {group} backend {currentBackend} → {backend}.");
}
var stripping = backend == ScriptingImplementation.IL2CPP
? ManagedStrippingLevel.High
: ManagedStrippingLevel.Disabled;
var currentStripping = PlayerSettings.GetManagedStrippingLevel(group);
if (currentStripping != stripping)
{
PlayerSettings.SetManagedStrippingLevel(group, stripping);
Debug.Log($"[{nameof(ScriptingBackendOverride)}] {group} managedStrippingLevel {currentStripping} → {stripping}.");
}
}
}
}