Skip to content

Commit 5aa02c4

Browse files
fix(mac): propagate AltTester define symbols into Unity 6 Build Profile
Unity 6 Build Profiles can override scripting define symbols. On macOS Unity 6 this caused AltTester runtime scripts to be excluded, leaving missing-script errors on AltTesterPrefab and no server listening. For AltTester builds, mirror the current Standalone define symbols into the BuildProfile's scripting defines before building, then restore afterwards.
1 parent 642e8e8 commit 5aa02c4

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

sample/Assets/Editor/MacBuilderUnity6.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using AltTester.AltTesterUnitySDK.Editor;
88
using System;
99
using System.Linq;
10+
using System.Collections.Generic;
1011
using AltTester.AltTesterUnitySDK.Commands;
1112
using AltTester.AltTesterSDK.Driver;
1213
using TMPro;
@@ -60,6 +61,30 @@ private static void BuildPlayer(string defaultBuildPath, bool setupForAltTester
6061

6162
Debug.Log($"Using Build Profile: {buildProfile.name}");
6263

64+
// Unity 6 Build Profiles can override scripting defines. If we only mutate
65+
// PlayerSettings, AltTester runtime scripts may be excluded from the build
66+
// (leading to missing-script errors and no AltTester server listening).
67+
// For AltTester builds, temporarily mirror the current Standalone define
68+
// symbols onto the BuildProfile's scripting defines.
69+
string[] originalProfileDefines = Array.Empty<string>();
70+
if (setupForAltTester)
71+
{
72+
originalProfileDefines = GetBuildProfileDefines(buildProfile);
73+
74+
// Capture the current Standalone define symbols after SetupAltTester()
75+
// has added AltTester + IMMUTABLE_E2E_TESTING.
76+
string defineSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(BuildTargetGroup.Standalone);
77+
var desired = defineSymbols
78+
.Split(';')
79+
.Select(s => s.Trim())
80+
.Where(s => !string.IsNullOrEmpty(s))
81+
.Distinct()
82+
.ToArray();
83+
84+
SetBuildProfileDefines(buildProfile, MergeDefines(originalProfileDefines, desired));
85+
Debug.Log($"Applied {desired.Length} define symbols to BuildProfile for AltTester build.");
86+
}
87+
6388
BuildPlayerWithProfileOptions options = new BuildPlayerWithProfileOptions
6489
{
6590
buildProfile = buildProfile,
@@ -82,6 +107,11 @@ private static void BuildPlayer(string defaultBuildPath, bool setupForAltTester
82107
EditorApplication.Exit(1);
83108
}
84109

110+
if (setupForAltTester)
111+
{
112+
// Restore original profile defines to avoid dirtying the repo locally.
113+
SetBuildProfileDefines(buildProfile, originalProfileDefines);
114+
}
85115

86116
if (setupForAltTester)
87117
{
@@ -171,6 +201,59 @@ private static void SetupAltTester(string[] scenes)
171201
AltBuilder.InsertAltInScene(scenes[0], instrumentationSettings);
172202
}
173203

204+
private static string[] GetBuildProfileDefines(BuildProfile buildProfile)
205+
{
206+
var so = new SerializedObject(buildProfile);
207+
var prop = so.FindProperty("m_ScriptingDefines");
208+
if (prop == null || !prop.isArray) return Array.Empty<string>();
209+
var list = new List<string>(prop.arraySize);
210+
for (int i = 0; i < prop.arraySize; i++)
211+
{
212+
var el = prop.GetArrayElementAtIndex(i);
213+
if (el != null && !string.IsNullOrEmpty(el.stringValue))
214+
{
215+
list.Add(el.stringValue);
216+
}
217+
}
218+
return list.ToArray();
219+
}
220+
221+
private static string[] MergeDefines(IEnumerable<string> a, IEnumerable<string> b)
222+
{
223+
var set = new HashSet<string>(StringComparer.Ordinal);
224+
foreach (var s in a ?? Array.Empty<string>()) if (!string.IsNullOrEmpty(s)) set.Add(s);
225+
foreach (var s in b ?? Array.Empty<string>()) if (!string.IsNullOrEmpty(s)) set.Add(s);
226+
return set.ToArray();
227+
}
228+
229+
private static void SetBuildProfileDefines(BuildProfile buildProfile, IEnumerable<string> defines)
230+
{
231+
var so = new SerializedObject(buildProfile);
232+
var prop = so.FindProperty("m_ScriptingDefines");
233+
if (prop == null || !prop.isArray)
234+
{
235+
Debug.LogWarning("BuildProfile does not expose m_ScriptingDefines; cannot apply defines.");
236+
return;
237+
}
238+
239+
prop.ClearArray();
240+
int idx = 0;
241+
if (defines != null)
242+
{
243+
foreach (var d in defines)
244+
{
245+
if (string.IsNullOrEmpty(d)) continue;
246+
prop.InsertArrayElementAtIndex(idx);
247+
prop.GetArrayElementAtIndex(idx).stringValue = d;
248+
idx++;
249+
}
250+
}
251+
252+
so.ApplyModifiedPropertiesWithoutUndo();
253+
EditorUtility.SetDirty(buildProfile);
254+
AssetDatabase.SaveAssets();
255+
}
256+
174257
public static void RemoveAltFromScene(string scene)
175258
{
176259
Debug.Log("Removing AltTesterPrefab from the [" + scene + "] scene.");

0 commit comments

Comments
 (0)